use of org.eclipse.xsd.util.XSDSchemaLocator in project tmdm-studio-se by Talend.
the class Util method getXSDSchema.
private static XSDSchema getXSDSchema(String namespaceURI, String rawData, final List<XSDImport> imports, final TreeObject treeObj, boolean uri, final List<Exception> exceptions, final Map<String, Integer> schemaMonitor) throws Exception {
FileInputStream fin = null;
try {
// $NON-NLS-1$//$NON-NLS-2$
final String xsdFileName = System.getProperty("user.dir") + "/.xsdModel.xml";
URI fileURI = URI.createFileURI(xsdFileName);
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(true);
documentBuilderFactory.setValidating(false);
DocumentBuilder documentBuilder;
XSDSchema schema = null;
InputSource source = null;
Document document = null;
String schemaLocation = rawData;
documentBuilder = documentBuilderFactory.newDocumentBuilder();
if (rawData == null) {
// $NON-NLS-1$
return XSDSchemaImpl.getSchemaForSchema("http://www.w3.org/2001/XMLSchema");
}
if (namespaceURI == null && rawData.endsWith(".xsd") && rawData.indexOf(File.separator) > 0) {
// $NON-NLS-1$
File rawFile = new File(rawData);
if (!rawFile.exists()) {
throw new IllegalArgumentException(rawData);
}
}
// import namespace="http://xxx" schemaLocation="xxxx"
if (namespaceURI != null && schemaLocation.endsWith(".xsd")) {
// $NON-NLS-1$
// $NON-NLS-1$
URL url = new java.net.URI(namespaceURI + "/" + rawData).toURL();
uri = false;
rawData = IOUtils.toString(url.openConnection().getInputStream());
// $NON-NLS-1$//$NON-NLS-2$
rawData = rawData.replaceAll("<!DOCTYPE(.*?)>", "");
}
if (rawData.equals("http://www.w3.org/2001/03/xml.xsd")) {
// $NON-NLS-1$
// $NON-NLS-1$
URL url = new java.net.URI("http://www.w3.org/2001/03/xml.xsd").toURL();
uri = false;
rawData = IOUtils.toString(url.openConnection().getInputStream());
// $NON-NLS-1$//$NON-NLS-2$
rawData = rawData.replaceAll("<!DOCTYPE(.*?)>", "");
}
if (uri) {
File file = new File(rawData);
if (file.exists()) {
fin = new FileInputStream(file);
source = new InputSource(fin);
} else {
source = new InputSource(new StringReader(Util.getResponseFromURL(rawData, treeObj)));
}
} else {
source = new InputSource(new StringReader(rawData));
}
try {
document = documentBuilder.parse(source);
} catch (SAXParseException ex) {
exceptions.add(ex);
return null;
}
schema = XSDSchemaImpl.createSchema(document.getDocumentElement());
ResourceSet resourceSet = new ResourceSetImpl();
Resource resource = resourceSet.createResource(fileURI);
resourceSet.getAdapterFactories().add(new AdapterFactoryImpl() {
class SchemaLocator extends AdapterImpl implements XSDSchemaLocator {
public XSDSchema locateSchema(XSDSchema xsdSchema, String namespaceURI, String rawSchemaLocationURI, String resolvedSchemaLocation) {
XSDSchema schema;
Integer rawCnt = schemaMonitor.get(rawSchemaLocationURI);
if (rawCnt == null) {
rawCnt = 0;
} else {
rawCnt++;
}
schemaMonitor.put(rawSchemaLocationURI, rawCnt);
if (rawCnt >= 10) {
schemaMonitor.put(rawSchemaLocationURI, -1);
return null;
}
try {
schema = Util.getXSDSchema(namespaceURI, rawSchemaLocationURI, imports, treeObj, true, exceptions, schemaMonitor);
} catch (Exception e) {
return XSDSchemaImpl.getSchemaForSchema(namespaceURI);
}
schema.setTargetNamespace(namespaceURI);
schema.setElement(schema.getDocument().getDocumentElement());
return schema;
}
public boolean isAdatperForType(Object type) {
return type == XSDSchemaLocator.class;
}
}
protected SchemaLocator schemaLocator = new SchemaLocator();
@Override
public boolean isFactoryForType(Object type) {
return type == XSDSchemaLocator.class;
}
@Override
public Adapter adaptNew(Notifier target, Object type) {
return schemaLocator;
}
});
// import namespace="http://xxx" schemaLocation="xxxx"
if (namespaceURI != null && schemaLocation.endsWith(".xsd")) {
// $NON-NLS-1$
schema.setSchemaLocation(schemaLocation);
} else {
schema.setSchemaLocation(fileURI.toString());
// set the schema for schema QName prefix to "xsd"
// $NON-NLS-1$
schema.setSchemaForSchemaQNamePrefix("xsd");
}
// catch up the NPE to make sure data model can still run in case of unknown conflict
try {
resource.getContents().add(schema);
} catch (Exception ex) {
log.error(ex.getMessage(), ex);
}
// Add the root schema to the resource that was created above
Iterator<Integer> iter = schemaMonitor.values().iterator();
while (iter.hasNext()) {
Integer it = iter.next();
if (it.intValue() == -1) {
return schema;
}
}
importSchema(schema, imports, schemaMonitor);
schema.setElement(document.getDocumentElement());
return schema;
} finally {
if (fin != null) {
fin.close();
}
}
}
Aggregations