Permalink
Browse files

IntrospectionParser to return Block[] instead of DefsFile.

Change the IntrospectionParser method to parse data to return a map
of Block arrays instead of a map of DefsFile. This allow to go back
to the previous behaviour of a DefsFile (which is not adding or
changing Block) and to override or add a Block before creating the
DefsFile instance.
  • Loading branch information...
respawner committed Aug 26, 2013
1 parent 66e469a commit 549a4982e297cabefc73cb097509d21c4cbc05eb
@@ -25,6 +25,7 @@
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -117,7 +118,7 @@ public static void main(String[] args) throws IOException {
*/
private static void runGeneratorOutputIntrospectionToFiles(final String[] files, final File outputDir) {
final File[] overriders;
final Map<String, DefsFile> introspected;
final Map<String, Block[]> introspected;
final List<DefsFile> all;
IntrospectionParser parser;
DefsParser secondParser;
@@ -126,7 +127,7 @@ private static void runGeneratorOutputIntrospectionToFiles(final String[] files,
DefsLineNumberReader in;
all = new ArrayList<DefsFile>();
introspected = new HashMap<String, DefsFile>();
introspected = new HashMap<String, Block[]>();
/*
* Load the all the .gir files into DefsFile objects, one per type.
@@ -171,9 +172,9 @@ public boolean accept(File dir, String name) {
* Along the way, this registers the type information.
*/
for (int i = 0; i < overriders.length; i++) {
for (File overrider : overriders) {
try {
in = new DefsLineNumberReader(new FileReader(overriders[i]), overriders[i].getName());
in = new DefsLineNumberReader(new FileReader(overrider), overrider.getName());
secondParser = new DefsParser(in);
blocks = secondParser.parseData();
@@ -187,11 +188,13 @@ public boolean accept(File dir, String name) {
all.add(new DefsFile(blocks));
} else {
for (Block block : blocks) {
final DefsFile toChange;
final FunctionBlock function;
Block[] toChange, changed;
String object;
boolean toAdd;
function = (FunctionBlock) block;
toAdd = true;
/*
* We are only interested in the isConstructorOf and
@@ -204,28 +207,82 @@ public boolean accept(File dir, String name) {
object = function.getIsConstructorOf();
}
/*
* We are unable to determine to which object this
* FunctionBlock should be associated. The .defs file
* is probably incorrect.
*/
if (object == null) {
System.out.println("Cannot find the object name that FunctionBlock "
+ function.getCName() + " belongs to.");
System.out.println("Please check the data in " + overrider + ".");
continue;
}
/*
* Look for the object and add the new Block.
*/
if (object != null) {
toChange = introspected.get(object);
toChange = introspected.get(object);
/*
* We are unable to find the object this FunctionBlock
* should be associated. This means that we did not
* parse any data from this object before.
*/
if (toChange == null) {
System.out.println("Cannot the object " + object
+ " that the FunctionBlock " + function.getCName() + " belongs to.");
System.out.println("Do we have enough data about it?");
continue;
}
/*
* The Block that we are trying to add override
* another existing Block so we need to replace the
* old one by the new one.
*/
for (int i = 0; i < toChange.length; i++) {
final String cNameCurrent, cNameNew;
if (toChange[i] instanceof FunctionBlock) {
cNameCurrent = ((FunctionBlock) toChange[i]).getCName();
cNameNew = function.getCName();
if (toChange != null) {
toChange.addFunctionBlock(function);
if ((cNameCurrent != null) && (cNameNew != null)
&& (cNameCurrent.equals(cNameNew))) {
toAdd = false;
toChange[i] = block;
break;
}
}
}
/*
* The Block that we are trying to add does not
* replace another one so we just add it to the array.
*/
if (toAdd) {
changed = Arrays.copyOf(toChange, toChange.length + 1);
changed[toChange.length] = block;
introspected.put(object, changed);
}
}
}
in.close();
} catch (IOException e) {
System.out.println("I/O problem when trying to parse " + overriders[i]);
System.out.println("I/O problem when trying to parse " + overrider);
System.out.println(e.getMessage());
System.out.println("[continuing next file]\n");
continue;
} catch (ImproperDefsFileException e) {
System.out.println("Couldn't get sufficient information from " + overriders[i] + ":");
System.out.println("Couldn't get sufficient information from " + overrider + ":");
System.out.println(e.getMessage());
System.out.println("[continuing next file]\n");
continue;
@@ -235,11 +292,19 @@ public boolean accept(File dir, String name) {
}
/*
* Add all data from introspection to the list of data to process by
* the code generator.
* Add all data from introspection to the list of data to be processed
* by the code generator.
*/
all.addAll(introspected.values());
for (Map.Entry<String, Block[]> data : introspected.entrySet()) {
try {
all.add(new DefsFile(data.getValue()));
} catch (ImproperDefsFileException e) {
System.out.println("Couldn't get sufficient information for " + data.getKey() + ":");
System.out.println(e.getMessage());
System.out.println("[continuing next file]\n");
}
}
/*
* Now, with the meta data completely loaded, we can generate the
@@ -45,9 +45,7 @@
import nu.xom.ParsingException;
import nu.xom.ValidityException;
import com.operationaldynamics.driver.DefsFile;
import com.operationaldynamics.driver.IgnoreIntrospectionException;
import com.operationaldynamics.driver.ImproperDefsFileException;
/**
* A .gir file parser: convert XML data into an array of Block objects
@@ -1042,26 +1040,26 @@ private static final VirtualBlock parseSignal(Element object, Element signal, St
}
/**
* Run the parser across the XML data and return a map of DefsFile objects
* representing the blocks found there.
* Run the parser across the XML data and return a map of Block arrays
* representing the information found there.
*
* @return a list of DefsFile objects.
* @return a map of Block arrays.
* @throws ParsingException
* if an error occurs while parsing the XML file.
* @throws ValidityException
* if the XML file does not seem valid.
* @throws IOException
* if the XML file cannot be read.
*/
public Map<String, DefsFile> parseData() throws ParsingException, IOException {
final Map<String, DefsFile> defs;
public Map<String, Block[]> parseData() throws ParsingException, IOException {
final Map<String, Block[]> result;
final Builder builder;
final Document document;
final Element repository;
final Elements includes, namespaces;
final String[] includesHeaders;
defs = new HashMap<String, DefsFile>();
result = new HashMap<String, Block[]>();
builder = new Builder();
/*
@@ -1252,15 +1250,12 @@ private static final VirtualBlock parseSignal(Element object, Element signal, St
}
}
try {
/*
* Generate the defs file definition for the given object.
*/
/*
* Add the Block array and identify it with the C name of the
* current thing.
*/
defs.put(cName, new DefsFile(blocks.toArray(new Block[blocks.size()])));
} catch (ImproperDefsFileException e) {
e.printStackTrace();
}
result.put(cName, blocks.toArray(new Block[blocks.size()]));
}
/*
@@ -1380,15 +1375,12 @@ private static final VirtualBlock parseSignal(Element object, Element signal, St
}
}
try {
/*
* Generate the defs file definition for the given object.
*/
/*
* Add the Block array and identify it with the C name of the
* current thing.
*/
defs.put(cName, new DefsFile(blocks.toArray(new Block[blocks.size()])));
} catch (ImproperDefsFileException e) {
e.printStackTrace();
}
result.put(cName, blocks.toArray(new Block[blocks.size()]));
}
/*
@@ -1474,15 +1466,12 @@ private static final VirtualBlock parseSignal(Element object, Element signal, St
getActualJavaName(cName, enumeration.getAttributeValue("name")),
characteristics, values));
try {
/*
* Generate the defs file definition for the given object.
*/
/*
* Add the Block array and identify it with the C name of the
* current thing.
*/
defs.put(cName, new DefsFile(blocks.toArray(new Block[blocks.size()])));
} catch (ImproperDefsFileException e) {
e.printStackTrace();
}
result.put(cName, blocks.toArray(new Block[blocks.size()]));
}
/*
@@ -1565,16 +1554,12 @@ private static final VirtualBlock parseSignal(Element object, Element signal, St
blocks.add(new FlagsBlock(getActualJavaName(cName, flag.getAttributeValue("name")),
characteristics, values));
try {
/*
* Generate the defs file definition for the given
* object.
*/
/*
* Add the Block array and identify it with the C name of
* the current thing.
*/
defs.put(cName, new DefsFile(blocks.toArray(new Block[blocks.size()])));
} catch (ImproperDefsFileException e) {
e.printStackTrace();
}
result.put(cName, blocks.toArray(new Block[blocks.size()]));
}
}
@@ -1708,15 +1693,12 @@ private static final VirtualBlock parseSignal(Element object, Element signal, St
}
}
try {
/*
* Generate the defs file definition for the given boxed.
*/
/*
* Add the Block array and identify it with the C name of the
* current thing.
*/
defs.put(cName, new DefsFile(blocks.toArray(new Block[blocks.size()])));
} catch (ImproperDefsFileException e) {
e.printStackTrace();
}
result.put(cName, blocks.toArray(new Block[blocks.size()]));
}
/*
@@ -1849,19 +1831,16 @@ private static final VirtualBlock parseSignal(Element object, Element signal, St
}
}
try {
/*
* Generate the defs file definition for the given boxed.
*/
/*
* Add the Block array and identify it with the C name of the
* current thing.
*/
defs.put(cName, new DefsFile(blocks.toArray(new Block[blocks.size()])));
} catch (ImproperDefsFileException e) {
e.printStackTrace();
}
result.put(cName, blocks.toArray(new Block[blocks.size()]));
}
}
return defs;
return result;
}
/**
Oops, something went wrong.

0 comments on commit 549a498

Please sign in to comment.