use of com.sun.xml.xsom.XSSchemaSet in project midpoint by Evolveum.
the class SchemaProcessor method getComplexTypeToElementName.
private Map<QName, List<QName>> getComplexTypeToElementName(ClassOutline classOutline) {
Map<QName, List<QName>> complexTypeToElementName = new HashMap<QName, List<QName>>();
XSSchemaSet schemaSet = classOutline.target.getSchemaComponent().getRoot();
for (XSSchema schema : schemaSet.getSchemas()) {
Map<String, XSElementDecl> elemDecls = schema.getElementDecls();
for (Entry<String, XSElementDecl> entry : elemDecls.entrySet()) {
XSElementDecl decl = entry.getValue();
XSType xsType = decl.getType();
if (xsType.getName() == null) {
continue;
}
QName type = new QName(xsType.getTargetNamespace(), xsType.getName());
List<QName> qnames = complexTypeToElementName.get(type);
if (qnames == null) {
qnames = new ArrayList<QName>();
complexTypeToElementName.put(type, qnames);
}
qnames.add(new QName(decl.getTargetNamespace(), decl.getName()));
}
}
return complexTypeToElementName;
}
use of com.sun.xml.xsom.XSSchemaSet in project midpoint by Evolveum.
the class SchemaProcessor method updateObjectFactoryElements.
/**
* Marks ObjectFactory.createXYZ methods for elements with a:rawType annotation as @Raw.
*/
private void updateObjectFactoryElements(Outline outline) {
XSSchemaSet schemaSet = outline.getModel().schemaComponent;
for (CElementInfo elementInfo : outline.getModel().getAllElements()) {
QName name = elementInfo.getElementName();
XSComponent elementDecl;
if (elementInfo.getSchemaComponent() != null) {
// it's strange but elements seem not to have this filled-in...
elementDecl = elementInfo.getSchemaComponent();
} else {
elementDecl = schemaSet.getElementDecl(name.getNamespaceURI(), name.getLocalPart());
}
boolean isRaw = hasAnnotation(elementDecl, A_RAW_TYPE);
if (isRaw) {
print("*** Raw element found: " + elementInfo.getElementName());
JDefinedClass objectFactory = outline.getPackageContext(elementInfo._package()).objectFactory();
// finding method corresponding to the given element
boolean methodFound = false;
for (JMethod method : objectFactory.methods()) {
for (JAnnotationUse annotationUse : method.annotations()) {
if (XmlElementDecl.class.getName().equals(annotationUse.getAnnotationClass().fullName())) {
// ugly method of finding the string value of the annotation members (couldn't find any better)
JAnnotationValue namespaceValue = annotationUse.getAnnotationMembers().get("namespace");
StringWriter namespaceWriter = new StringWriter();
JFormatter namespaceFormatter = new JFormatter(namespaceWriter);
namespaceValue.generate(namespaceFormatter);
JAnnotationValue nameValue = annotationUse.getAnnotationMembers().get("name");
StringWriter nameWriter = new StringWriter();
JFormatter nameFormatter = new JFormatter(nameWriter);
nameValue.generate(nameFormatter);
if (("\"" + name.getNamespaceURI() + "\"").equals(namespaceWriter.toString()) && ("\"" + name.getLocalPart() + "\"").equals(nameWriter.toString())) {
print("*** Annotating method as @Raw: " + method.name());
method.annotate(Raw.class);
methodFound = true;
break;
}
}
}
}
if (!methodFound) {
throw new IllegalStateException("No factory method found for element " + name);
}
}
}
}
use of com.sun.xml.xsom.XSSchemaSet in project atlasmap by atlasmap.
the class SchemaInspector method doInspect.
private void doInspect(InputStream is) throws Exception {
xmlDocument = AtlasXmlModelFactory.createXmlDocument();
Fields fields = new Fields();
xmlDocument.setFields(fields);
namespaceContext = new AtlasXmlNamespaceContext();
rootNamespace = null;
XSOMParser parser = new XSOMParser(SAXParserFactory.newInstance());
parser.setAnnotationParser(new DomAnnotationParserFactory());
parser.setErrorHandler(new XSOMErrorHandler());
Transformer transformer = TransformerFactory.newInstance().newTransformer();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
Document doc = dbf.newDocumentBuilder().parse(is);
Element root = doc.getDocumentElement();
if (root == null) {
throw new XmlInspectionException("XML schema document is empty");
} else if ("SchemaSet".equals(root.getLocalName())) {
XPath xpath = XPathFactory.newInstance().newXPath();
xpath.setNamespaceContext(namespaceContext);
NodeList subSchemas = (NodeList) xpath.evaluate(String.format("/%s:SchemaSet/%s:AdditionalSchemas/%s:schema", NS_PREFIX_SCHEMASET, NS_PREFIX_SCHEMASET, NS_PREFIX_XMLSCHEMA), doc, XPathConstants.NODESET);
for (int i = 0; i < subSchemas.getLength(); i++) {
Element e = (Element) subSchemas.item(i);
inheritNamespaces(e, false);
parser.parse(toInputStream(transformer, e));
}
Element rootSchema = (Element) xpath.evaluate(String.format("/%s:SchemaSet/%s:schema", NS_PREFIX_SCHEMASET, NS_PREFIX_XMLSCHEMA), doc, XPathConstants.NODE);
if (rootSchema == null) {
throw new XmlInspectionException("The root schema '/SchemaSet/schema' must be specified once and only once");
}
rootNamespace = getTargetNamespace(rootSchema);
if (rootNamespace != null && !rootNamespace.isEmpty()) {
namespaceContext.add("tns", rootNamespace);
}
inheritNamespaces(rootSchema, true);
parser.parse(toInputStream(transformer, rootSchema));
} else if ("schema".equals(root.getLocalName())) {
parser.parse(toInputStream(transformer, root));
rootNamespace = getTargetNamespace(root);
if (rootNamespace != null && !rootNamespace.isEmpty()) {
namespaceContext.add("tns", rootNamespace);
}
} else {
throw new XmlInspectionException(String.format("Unsupported document element '%s': root element must be 'schema' or 'SchemaSet'", root.getLocalName()));
}
XSSchemaSet schemaSet = parser.getResult();
printSchemaSet(schemaSet);
populateNamespaces();
}
Aggregations