You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xerces.apache.org by Nikolai Grigoriev <ng...@gmail.com> on 2015/01/23 00:44:35 UTC
losing annotations when using XMLGrammarPreparser with DOMInputSource
Hello!
I am trying to develop an app that will use XML schema with some additional
annotations. Annotations come in various forms, including the attributes
added to XML element definitions. I am using a different namespace for
these attributes. I know this is not exactly canonical for Schema but still
XML-legal :) Plus, I have found (thanks to this list) the feature "
http://apache.org/xml/features/generate-synthetic-annotations" that puts
all my custom stuff into the annotations that I can access via
XSElementDeclaration.getAnnotation().
Initially I have found a bit hacky way to load my schemas by following this
path:
XSImplementation impl = new XSImplementationImpl();
XSLoader loader = impl.createXSLoader(impl.createStringList(new
String[]{"1.0"}));
...
loader.getConfig().setParameter("
http://apache.org/xml/properties/internal/entity-resolver", resolver);
loader.getConfig().setParameter("
http://apache.org/xml/properties/internal/error-handler", errorHandler);
loader.getConfig().setParameter("
http://apache.org/xml/features/generate-synthetic-annotations", true);
XSModel model = loader.loadURI("file://" + schemaFileName);
This works, I can use the API to find my element and its annotation. Then I
have realized that there are two ways to handle the annotation data - write
it to DocumentFragment or to SAX handler. There is more value for me in DOM
since I need to deal with some additional document elements anyway, so,
again following the suggestions from this list I have tried another
approach:
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
dbf.setValidating(false);
DocumentBuilder db = dbf.newDocumentBuilder();
...setting entity resolver and error handler...
Document doc = null;
try ( BufferedReader br = new BufferedReader(new FileReader(modelFile))
) {
doc = db.parse(new InputSource(br));
} catch (IOException e) {
...
}
XMLGrammarPreparser preParser = new XMLGrammarPreparser();
preParser.setEntityResolver(resolver);
preParser.setErrorHandler(errorHandler);
preParser.setFeature("
http://apache.org/xml/features/generate-synthetic-annotations", true);
preParser.setFeature("
http://apache.org/xml/features/validation/schema-full-checking", true);
preParser.setFeature("http://apache.org/xml/features/validation/schema",
true);
preParser.setFeature("
http://apache.org/xml/features/honour-all-schemaLocations", true);
preParser.setFeature("
http://apache.org/xml/features/validate-annotations", false);
preParser.setFeature("
http://apache.org/xml/features/generate-synthetic-annotations", true);
preParser.registerPreparser(XMLGrammarDescription.XML_SCHEMA, null);
DOMInputSource modelSource = new DOMInputSource(doc);
XSGrammar grammar =
(XSGrammar)preParser.preparseGrammar(XMLGrammarDescription.XML_SCHEMA,
modelSource);
XSModel model = grammar.toXSModel();
I do get the model here and I can use the same methods to find my element.
However, with this approach I get no annotations! Element's getAnnotation()
returns null. By the way, I do have also a real annotation for this element
(documentation). Using Xerces 2.11.0.SP1.
If needed, I can reduce my test schema and code to minimum and post here -
but maybe someone already knows the answer?
Thanks!
--
Nikolai Grigoriev