\r
private static final long serialVersionUID = 7360740940824360338L;\r
private static final boolean debug = false;\r
+ private static String XML_SCHEMA_REF = "xmlns";\r
private ILogger logger;\r
private Resource root;\r
+ private String schemaURI;\r
private Deque<Element> current = new ArrayDeque<Element>();\r
private Map<String, XMLElementParser> parsers = new HashMap<String, XMLElementParser>();\r
private WriteGraph graph;\r
- \r
private Map<Class<? extends XMLElementParser>, XMLElementParser> namedParsers = new HashMap<Class<? extends XMLElementParser>, XMLElementParser>();\r
\r
- public XMLParser(WriteGraph graph) {\r
+ private Map<String, XMLParser> subParsers = new HashMap<String, XMLParser>();\r
+ \r
+ public XMLParser() {\r
+ \r
+ }\r
+ \r
+ public WriteGraph getGraph() {\r
+ return graph;\r
+ }\r
+ \r
+ public void setGraph(WriteGraph graph) {\r
this.graph = graph;\r
+ for (XMLParser p : subParsers.values())\r
+ p.setGraph(graph);\r
+ }\r
+ \r
+ public String getSchemaURI() {\r
+ return schemaURI;\r
+ }\r
+ \r
+ public void setSchemaURI(String schemaURI) {\r
+ this.schemaURI = schemaURI;\r
}\r
\r
public void add(XMLElementParser parser) {\r
namedParsers.put(parser.getClass(), parser);\r
}\r
\r
+ public void add(XMLParser parser) {\r
+ subParsers.put(parser.getSchemaURI(), parser);\r
+ }\r
\r
private List<Element> idReferenceElements = new ArrayList<Element>();\r
private void loadElement(Deque<Element> parents, Element element) throws SAXException{\r
Element parent = null;\r
if (parents.size() > 0) {\r
parent = parents.peek();\r
- if (parent.getParser() instanceof XMLElementNamedChildParser) {\r
+ // check for assigned subparser\r
+ if (parent.getXMLParser() != null && parent.getXMLParser() != this) {\r
+ element.setXMLParser(parent.getXMLParser());\r
+ parent.getXMLParser().loadElement(parents, element);\r
+ return;\r
+ }\r
+ if (parent.getElementParser() instanceof XMLElementNamedChildParser) {\r
// use parent's named child parser if it is supported\r
- Class<? extends XMLElementParser> parserClass = ((XMLElementNamedChildParser)parent.getParser()).getParser(parsers,parent,element);\r
+ Class<? extends XMLElementParser> parserClass = ((XMLElementNamedChildParser)parent.getElementParser()).getParser(parsers,parent,element);\r
if (parserClass != null) {\r
parser = namedParsers.get(parserClass);\r
if (parser == null) {\r
parser = parserClass.newInstance();\r
namedParsers.put(parserClass, parser);\r
} catch (IllegalAccessException | InstantiationException e) {\r
- String err = "Element parsers must have accessible default constructor";\r
+ String err = "Error processing " + parserClass.getName() + " : element parsers must have accessible default constructor";\r
logger.log(new Status(IStatus.ERROR, PLUGIN_ID, err));\r
throw new SAXException(err, e);\r
}\r
element.setData(parser.create(graph, element));\r
if (parser instanceof IDReferenceParser)\r
idReferenceElements.add(element);\r
- element.setParser(parser);\r
+ element.setElementParser(parser);\r
} catch (DatabaseException e) {\r
throw new SAXException(e);\r
}\r
} else {\r
- if (parent == null && parents.size() > 0)\r
- parent = parents.peek();\r
- String err = "Unknown element " + element.qName + ", parent " + (parent != null ? parent.getQName() : "None");\r
- logger.log(new Status(IStatus.ERROR, PLUGIN_ID, err));\r
- if (debug) System.err.println(err);\r
+ // check for schema reference attempt to locate subparser for it.\r
+ Attribute schemaRef = element.getAttribute(XML_SCHEMA_REF);\r
+ if (schemaRef != null && subParsers.containsKey(schemaRef.value)) {\r
+ XMLParser subParser = subParsers.get(schemaRef.value);\r
+ subParser.loadElement(parents, element);\r
+ element.setXMLParser(subParser);\r
+ } else {\r
+ if (parent == null && parents.size() > 0)\r
+ parent = parents.peek();\r
+ String err = "Unknown element " + element.qName + ", parent " + (parent != null ? parent.getQName() : "None");\r
+ logger.log(new Status(IStatus.ERROR, PLUGIN_ID, err));\r
+ if (debug) System.err.println(err);\r
+ }\r
}\r
}\r
\r
private Map<String, Element> idMap = new HashMap<String, Element>();\r
\r
private void handleElement(Deque<Element> parents, Element element) throws SAXException{\r
- XMLElementParser parser = element.getParser();\r
+ XMLElementParser parser = element.getElementParser();\r
if (parser != null) {\r
try {\r
parser.configure(graph, parents, element);\r
if (parents.size() > 0) {\r
Element parent = parents.peek();\r
- if (parent.getParser() != null) {\r
- if (!parent.getParser().connectChild(graph, parent, element))\r
+ if (parent.getElementParser() != null) {\r
+ if (!parent.getElementParser().connectChild(graph, parent, element))\r
if (!parser.connectParent(graph, parent, element)) {\r
String err = "Did not connect element " + element.qName + ", parent " + (parent != null ? parent.getQName() : "None");\r
logger.log(new Status(IStatus.ERROR, PLUGIN_ID, err));\r
Element existing = idMap.put(id, element);\r
if (existing != null) {\r
// report error + use id priorities to select the kept element.\r
- boolean useExt = existing.parser.idPriority() > element.parser.idPriority();\r
+ boolean useExt = existing.elementParser.idPriority() > element.elementParser.idPriority();\r
if (useExt)\r
idMap.put(id, existing);\r
String err = "Duplicate XML element id: " + id + " for " + element.getQName() + " and " + existing.getQName() + ", using " + (useExt ? existing.getQName() : element.getQName());\r
} else {\r
Element parent = parents.peek();\r
if (parent != null) { \r
- parser = parent.getParser();\r
+ parser = parent.getElementParser();\r
if (parser != null && parser instanceof UnrecognizedElementParser) {\r
try {\r
((UnrecognizedElementParser)parser).configureChild(graph, parents, parent, element);\r
}\r
\r
private void handleCharacters(Element element, String string) throws SAXException{\r
- XMLElementParser parser = element.getParser();\r
+ XMLElementParser parser = element.getElementParser();\r
if (parser != null) {\r
try {\r
parser.configure(graph, element, string);\r
return root;\r
}\r
\r
+ StringBuilder charactersValue;\r
+ \r
@Override\r
public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException {\r
Element e = new Element(uri,localName,name,attributes);\r
\r
loadElement(current,e);\r
current.push(e);\r
+ \r
+ charactersValue = new StringBuilder();\r
}\r
\r
@Override\r
e = current.pop();\r
}\r
if (e != null) {\r
+ if (charactersValue.length() > 0)\r
+ handleCharacters(e, charactersValue.toString());\r
handleElement(current,e);\r
}\r
if (current.isEmpty()) {\r
root = e.getData();\r
}\r
+ \r
+ charactersValue = new StringBuilder();\r
}\r
\r
@Override\r
public void characters(char[] ch, int start, int length)\r
throws SAXException {\r
- \r
- if (!current.isEmpty()) {\r
- String s = new String(ch,start,length);\r
- Element e = current.peek();\r
- handleCharacters(e, s);\r
- }\r
+ charactersValue.append(new String(ch,start,length));\r
+// if (!current.isEmpty()) {\r
+// String s = new String(ch,start,length);\r
+// Element e = current.peek();\r
+// handleCharacters(e, s);\r
+// }\r
\r
}\r
\r
ontShort +=".";\r
String parserPackagePostfix = "_elem";\r
String importerClassPostfix = "Importer";\r
+ String parserClassPostfix = "Parser";\r
elementPackageName = name+parserPackagePostfix;\r
\r
importParserDir= new File(converter.getParserDir().getAbsolutePath()+"/"+elementPackageName);\r
importParserDir.mkdirs();\r
\r
handle(schema);\r
- \r
- File importParserFile = new File(converter.getParserDir().getAbsolutePath()+"/"+name+importerClassPostfix+".java");\r
- PrintWriter mainWriter = createFile(importParserFile);\r
+ // Create Importer class\r
+ File importerFile = new File(converter.getParserDir().getAbsolutePath()+"/"+name+importerClassPostfix+".java");\r
+ PrintWriter mainWriter = createFile(importerFile);\r
mainWriter.println("package " + converter.getPluginName() +";");\r
mainWriter.println();\r
mainWriter.println("import java.io.File;");\r
mainWriter.println("import org.simantics.db.Session;");\r
mainWriter.println("import org.simantics.xml.sax.base.AbstractImporter;");\r
- mainWriter.println("import org.simantics.xml.sax.base.XMLParser;");\r
mainWriter.println();\r
mainWriter.println("public class " + name + importerClassPostfix+" extends AbstractImporter {");\r
mainWriter.println();\r
mainWriter.println(" public " + name + importerClassPostfix+"(Session session, File file) {");\r
- mainWriter.println(" super(session,file);");\r
+ mainWriter.println(" super(session, file, new "+name + parserClassPostfix+"());");\r
mainWriter.println(" }");\r
mainWriter.println();\r
- mainWriter.println(" @Override");\r
- mainWriter.println(" public void configure(XMLParser parser) {");\r
+// mainWriter.println(" @Override");\r
+// mainWriter.println(" public void configure(XMLParser parser) {");\r
+// for (String s : ruleClassNames) {\r
+// mainWriter.println(" parser.add(new "+s+"());");\r
+// }\r
+// mainWriter.println(" }");\r
+ mainWriter.println("}");\r
+ \r
+ mainWriter.println();\r
+ mainWriter.flush();\r
+ mainWriter.close();\r
+ \r
+ // Create Parser class\r
+ File parserFile = new File(converter.getParserDir().getAbsolutePath()+"/"+name+parserClassPostfix+".java");\r
+ mainWriter = createFile(parserFile);\r
+ mainWriter.println("package " + converter.getPluginName() +";");\r
+ mainWriter.println();\r
+ mainWriter.println("import org.simantics.xml.sax.base.XMLParser;");\r
+ mainWriter.println();\r
+ mainWriter.println("public class " + name + parserClassPostfix+" extends XMLParser {");\r
+ mainWriter.println();\r
+ mainWriter.println(" public " + name + parserClassPostfix+"() {");\r
+ if (schema.getTargetNamespace() != null)\r
+ mainWriter.println(" setSchemaURI(\""+schema.getTargetNamespace()+"\");");\r
for (String s : ruleClassNames) {\r
- mainWriter.println(" parser.add(new "+s+"());");\r
+ mainWriter.println(" add(new "+s+"());");\r
}\r
mainWriter.println(" }");\r
mainWriter.println("}");\r
if (!className.equals(refClassName))\r
fw.writer.println(" addParser("+refClassName+".class);");\r
\r
- fw.delayedWriter.println(" if (child.getParser() instanceof "+refClassName+"){");\r
+ fw.delayedWriter.println(" if (child.getElementParser() instanceof "+refClassName+"){");\r
fw.delayedWriter.println(" graph.claim(element.getData(), "+ontShort+getName(parent)+"_has"+ref + ", child.getData());");\r
if (useElementList(parent, indicator,element, reference, ref, refType)) {\r
\r
fw.writer.println(" addParser(\""+ ref +"\", "+getName(parent) +"_" +ref+".class);");\r
\r
fw.delayedWriter2.println(" public static class " + getName(parent) +"_" +ref+" extends org.simantics.xml.sax.base.ValueElementParser {");\r
- fw.delayedWriter2.println(" "+ getName(parent) +"_" +ref +"(){");\r
+ fw.delayedWriter2.println(" public "+ getName(parent) +"_" +ref +"(){");\r
fw.delayedWriter2.println(" super(\""+ref+"\"," +this.ontologyClassName+".URIs."+getName(parent) + "_has"+ref+", "+binding+");");\r
fw.delayedWriter2.println(" }");\r
fw.delayedWriter2.println(" }");\r
else\r
fw.writer.println(" addParser("+className+".class);");\r
\r
- fw.delayedWriter.println(" if (child.getParser() instanceof "+className+"){");\r
+ fw.delayedWriter.println(" if (child.getElementParser() instanceof "+className+"){");\r
fw.delayedWriter.println(" graph.claim(element.getData(), "+ontShort+getName(parent)+"_has"+ref + ", child.getData());");\r
if (useElementList(parent, indicator,element, reference, ref, new QName(obj.getName()))) {\r
\r
}\r
\r
private Inheritance getElementInheritance(SchemaObject topLevelElementObj) {\r
- if (topLevelElementObj.getElement() != null && topLevelElementObj.getElement().getName().equals("ByteString"))\r
- System.out.println();\r
Element topLevelElement = topLevelElementObj.getElement();\r
Inheritance inheritance = new Inheritance();\r
if (topLevelElement.getType() != null) {\r