use of org.apache.xerces.xni.XMLResourceIdentifier in project intellij-community by JetBrains.
the class ModelGen method loadModel.
public void loadModel(final File... modelRoots) throws Exception {
XMLEntityResolver resolver = new XMLEntityResolver() {
public XMLInputSource resolveEntity(XMLResourceIdentifier xmlResourceIdentifier) throws XNIException, IOException {
String esid = xmlResourceIdentifier.getExpandedSystemId();
if (esid == null) {
final String location = schemaLocationMap.get(xmlResourceIdentifier.getNamespace());
if (location != null) {
esid = location;
} else {
return null;
}
}
// Util.log("resolving "+esid);
File f = null;
for (File root : modelRoots) {
if (root == null)
continue;
if (root.isDirectory()) {
final String fileName = esid.substring(esid.lastIndexOf('/') + 1);
f = new File(root, fileName);
} else {
f = root;
}
}
if (f == null || !f.exists()) {
Util.logerr("unable to resolve: " + esid);
return null;
}
esid = f.getPath();
return new XMLInputSource(null, esid, null);
}
};
ArrayList<File> files = new ArrayList<>();
for (File root : modelRoots) {
ContainerUtil.addAll(files, root.listFiles());
}
loader.loadModel(model, files, resolver);
Util.log(model.jtMap.size() + " java types loaded");
}
use of org.apache.xerces.xni.XMLResourceIdentifier in project ofbiz-framework by apache.
the class UtilXml method readXmlDocument.
public static Document readXmlDocument(InputStream is, boolean validate, String docDescription, boolean withPosition) throws SAXException, ParserConfigurationException, java.io.IOException {
if (!withPosition) {
return readXmlDocument(is, validate, docDescription);
}
if (is == null) {
Debug.logWarning("[UtilXml.readXmlDocument] InputStream was null, doing nothing", module);
return null;
}
long startTime = System.currentTimeMillis();
Document document = null;
DOMParser parser = new DOMParser() {
private XMLLocator locator = null;
private void setLineColumn(Node node) {
if (locator == null) {
throw new java.lang.IllegalStateException("XMLLocator is null");
}
if (node.getUserData("startLine") != null) {
return;
}
node.setUserData("systemId", locator.getLiteralSystemId(), null);
node.setUserData("startLine", locator.getLineNumber(), null);
node.setUserData("startColumn", locator.getColumnNumber(), null);
}
private void setLineColumn() {
try {
Node node = (Node) getProperty("http://apache.org/xml/properties/dom/current-element-node");
if (node != null) {
setLineColumn(node);
}
} catch (SAXException ex) {
Debug.logWarning(ex, module);
}
}
private void setLastChildLineColumn() {
try {
Node node = (Node) getProperty("http://apache.org/xml/properties/dom/current-element-node");
if (node != null) {
setLineColumn(node.getLastChild());
}
} catch (SAXException ex) {
Debug.logWarning(ex, module);
}
}
@Override
public void startGeneralEntity(String name, XMLResourceIdentifier identifier, String encoding, Augmentations augs) throws XNIException {
super.startGeneralEntity(name, identifier, encoding, augs);
setLineColumn();
}
@Override
public void comment(XMLString text, Augmentations augs) throws XNIException {
super.comment(text, augs);
setLastChildLineColumn();
}
@Override
public void processingInstruction(String target, XMLString data, Augmentations augs) throws XNIException {
super.processingInstruction(target, data, augs);
setLastChildLineColumn();
}
@Override
public void startDocument(XMLLocator locator, String encoding, NamespaceContext namespaceContext, Augmentations augs) throws XNIException {
super.startDocument(locator, encoding, namespaceContext, augs);
this.locator = locator;
setLineColumn();
}
@Override
public void doctypeDecl(String rootElement, String publicId, String systemId, Augmentations augs) throws XNIException {
super.doctypeDecl(rootElement, publicId, systemId, augs);
}
@Override
public void startElement(QName elementQName, XMLAttributes attrList, Augmentations augs) throws XNIException {
super.startElement(elementQName, attrList, augs);
setLineColumn();
}
@Override
public void characters(XMLString text, Augmentations augs) throws XNIException {
super.characters(text, augs);
setLastChildLineColumn();
}
@Override
public void ignorableWhitespace(XMLString text, Augmentations augs) throws XNIException {
super.ignorableWhitespace(text, augs);
setLastChildLineColumn();
}
};
parser.setFeature("http://xml.org/sax/features/namespaces", true);
parser.setFeature("http://xml.org/sax/features/validation", validate);
parser.setFeature("http://apache.org/xml/features/validation/schema", validate);
parser.setFeature("http://apache.org/xml/features/dom/defer-node-expansion", false);
// with a SchemaUrl, a URL object
if (validate) {
LocalResolver lr = new LocalResolver(new DefaultHandler());
ErrorHandler eh = new LocalErrorHandler(docDescription, lr);
parser.setEntityResolver(lr);
parser.setErrorHandler(eh);
}
InputSource inputSource = new InputSource(is);
inputSource.setSystemId(docDescription);
parser.parse(inputSource);
document = parser.getDocument();
double totalSeconds = (System.currentTimeMillis() - startTime) / 1000.0;
if (Debug.verboseOn()) {
Debug.logVerbose("XML Read " + totalSeconds + "s: " + docDescription, module);
}
return document;
}
Aggregations