use of org.apache.xerces.xni.XNIException in project iaf by ibissource.
the class XMLSchemaFactory method newSchema.
public Schema newSchema(Source[] schemas) throws SAXException {
// this will let the loader store parsed Grammars into the pool.
XMLGrammarPoolImplExtension pool = new XMLGrammarPoolImplExtension();
fXMLGrammarPoolWrapper.setGrammarPool(pool);
XMLInputSource[] xmlInputSources = new XMLInputSource[schemas.length];
InputStream inputStream;
Reader reader;
for (int i = 0; i < schemas.length; ++i) {
Source source = schemas[i];
if (source instanceof StreamSource) {
StreamSource streamSource = (StreamSource) source;
String publicId = streamSource.getPublicId();
String systemId = streamSource.getSystemId();
inputStream = streamSource.getInputStream();
reader = streamSource.getReader();
XMLInputSource xmlInputSource = new XMLInputSource(publicId, systemId, null);
xmlInputSource.setByteStream(inputStream);
xmlInputSource.setCharacterStream(reader);
xmlInputSources[i] = xmlInputSource;
} else if (source instanceof SAXSource) {
SAXSource saxSource = (SAXSource) source;
InputSource inputSource = saxSource.getInputSource();
if (inputSource == null) {
throw new SAXException(JAXPValidationMessageFormatter.formatMessage(fXMLSchemaLoader.getLocale(), "SAXSourceNullInputSource", null));
}
xmlInputSources[i] = new SAXInputSource(saxSource.getXMLReader(), inputSource);
} else if (source instanceof DOMSource) {
DOMSource domSource = (DOMSource) source;
Node node = domSource.getNode();
String systemID = domSource.getSystemId();
xmlInputSources[i] = new DOMInputSource(node, systemID);
} else // }
if (source == null) {
throw new NullPointerException(JAXPValidationMessageFormatter.formatMessage(fXMLSchemaLoader.getLocale(), "SchemaSourceArrayMemberNull", null));
} else {
throw new IllegalArgumentException(JAXPValidationMessageFormatter.formatMessage(fXMLSchemaLoader.getLocale(), "SchemaFactorySourceUnrecognized", new Object[] { source.getClass().getName() }));
}
}
try {
fXMLSchemaLoader.loadGrammar(xmlInputSources);
} catch (XNIException e) {
// this should have been reported to users already.
throw Util.toSAXException(e);
} catch (IOException e) {
// this hasn't been reported, so do so now.
SAXParseException se = new SAXParseException(e.getMessage(), null, e);
if (fErrorHandler != null) {
fErrorHandler.error(se);
}
// and we must throw it.
throw se;
}
// Clear reference to grammar pool.
fXMLGrammarPoolWrapper.setGrammarPool(null);
// Select Schema implementation based on grammar count.
final int grammarCount = pool.getGrammarCount();
AbstractXMLSchema schema = null;
if (fUseGrammarPoolOnly) {
throw new NotImplementedException("fUseGrammarPoolOnly");
// if (grammarCount > 1) {
// schema = new XMLSchemaHack(new ReadOnlyGrammarPool(pool));
// }
// else if (grammarCount == 1) {
// Grammar[] grammars = pool.retrieveInitialGrammarSet(XMLGrammarDescription.XML_SCHEMA);
// schema = new XMLSchemaHack(grammars[0]);
// }
// else {
// schema = new XMLSchemaHack();
// }
} else {
schema = new XMLSchema(new ReadOnlyGrammarPool(pool), false);
}
propagateFeatures(schema);
return schema;
}
use of org.apache.xerces.xni.XNIException in project iaf by ibissource.
the class ClassLoaderXmlEntityResolverTest method classLoaderXmlEntityResolverCannotLoadExternalEntities.
@Test
public void classLoaderXmlEntityResolverCannotLoadExternalEntities() throws Exception {
ClassLoaderXmlEntityResolver resolver = new ClassLoaderXmlEntityResolver(scopeProvider);
XMLResourceIdentifier resourceIdentifier = getXMLResourceIdentifier("ftp://share.host.org/UDTSchema.xsd");
URL url = this.getClass().getResource("/ClassLoader/request-ftp.xsd");
assertNotNull(url);
resourceIdentifier.setBaseSystemId(url.toExternalForm());
XNIException thrown = assertThrows(XNIException.class, () -> {
resolver.resolveEntity(resourceIdentifier);
});
assertThat(thrown.getMessage(), startsWith("Cannot lookup resource [ftp://share.host.org/UDTSchema.xsd] not allowed with protocol [ftp]"));
}
use of org.apache.xerces.xni.XNIException in project webtools.sourceediting by eclipse.
the class XMLValidator method createXMLReader.
/**
* Create an XML Reader.
*
* @return The newly created XML reader or null if unsuccessful.
* @throws Exception
*/
protected XMLReader createXMLReader(final XMLValidationInfo valinfo, XMLEntityResolver entityResolver) throws Exception {
XMLReader reader = null;
// move to Xerces-2... add the contextClassLoader stuff
ClassLoader prevClassLoader = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
MyStandardParserConfiguration configuration = new MyStandardParserConfiguration(valinfo);
reader = new org.apache.xerces.parsers.SAXParser(configuration) {
private XMLLocator locator = null;
/* (non-Javadoc)
* @see org.apache.xerces.parsers.AbstractSAXParser#startDocument(org.apache.xerces.xni.XMLLocator, java.lang.String, org.apache.xerces.xni.NamespaceContext, org.apache.xerces.xni.Augmentations)
*/
public void startDocument(org.apache.xerces.xni.XMLLocator theLocator, java.lang.String encoding, NamespaceContext nscontext, org.apache.xerces.xni.Augmentations augs) {
locator = theLocator;
valinfo.setXMLLocator(theLocator);
super.startDocument(theLocator, encoding, nscontext, augs);
}
/* (non-Javadoc)
* @see org.apache.xerces.parsers.AbstractSAXParser#startElement(org.apache.xerces.xni.QName, org.apache.xerces.xni.XMLAttributes, org.apache.xerces.xni.Augmentations)
*/
public void startElement(QName element, XMLAttributes attributes, Augmentations augs) throws XNIException {
valinfo.getStartElementLocations().push(new LocationCoordinate(locator.getLineNumber(), locator.getColumnNumber()));
super.startElement(element, attributes, augs);
}
/* (non-Javadoc)
* @see org.apache.xerces.parsers.AbstractSAXParser#endElement(org.apache.xerces.xni.QName, org.apache.xerces.xni.Augmentations)
*/
public void endElement(QName element, Augmentations augs) throws XNIException {
super.endElement(element, augs);
valinfo.getStartElementLocations().pop();
}
};
// $NON-NLS-1$
reader.setFeature("http://apache.org/xml/features/continue-after-fatal-error", false);
// $NON-NLS-1$
reader.setFeature("http://xml.org/sax/features/namespace-prefixes", valinfo.isNamespaceEncountered());
// $NON-NLS-1$
reader.setFeature("http://xml.org/sax/features/namespaces", valinfo.isNamespaceEncountered());
reader.setContentHandler(new DefaultHandler() {
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
valinfo.getErrorCustomizationManager().startElement(uri, localName);
}
public void endElement(String uri, String localName, String qName) throws SAXException {
valinfo.getErrorCustomizationManager().endElement(uri, localName);
}
});
// MH make sure validation works even when a customer entityResolver is note set (i.e. via setURIResolver())
if (entityResolver != null) {
// $NON-NLS-1$
reader.setProperty("http://apache.org/xml/properties/internal/entity-resolver", entityResolver);
}
// $NON-NLS-1$
reader.setProperty("http://xml.org/sax/properties/declaration-handler", new MyDeclHandler());
} catch (Exception e) {
Logger.logException(e);
// e.printStackTrace();
} finally {
Thread.currentThread().setContextClassLoader(prevClassLoader);
}
return reader;
}
use of org.apache.xerces.xni.XNIException in project intellij-community by JetBrains.
the class XmlResourceResolver method resolveEntity.
@Override
@Nullable
public XMLInputSource resolveEntity(XMLResourceIdentifier xmlResourceIdentifier) throws XNIException, IOException {
String publicId = xmlResourceIdentifier.getLiteralSystemId() != null ? xmlResourceIdentifier.getLiteralSystemId() : xmlResourceIdentifier.getNamespace();
if (publicId != null) {
try {
String userDir = new File(System.getProperty("user.dir")).toURI().getPath();
String publicIdPath = new URI(publicId).getPath();
if (publicIdPath.startsWith(userDir)) {
publicId = publicIdPath.substring(publicIdPath.indexOf(userDir) + userDir.length());
}
} catch (Exception e) {
}
}
PsiFile psiFile = resolve(xmlResourceIdentifier.getBaseSystemId(), publicId);
if (psiFile == null && xmlResourceIdentifier.getBaseSystemId() != null) {
psiFile = ExternalResourceManager.getInstance().getResourceLocation(xmlResourceIdentifier.getBaseSystemId(), myFile, null);
}
if (psiFile == null && xmlResourceIdentifier.getLiteralSystemId() != null && xmlResourceIdentifier.getNamespace() != null) {
psiFile = resolve(xmlResourceIdentifier.getBaseSystemId(), publicId = xmlResourceIdentifier.getNamespace());
}
if (psiFile == null) {
if (publicId != null && publicId.contains(":/")) {
try {
myErrorReporter.processError(new SAXParseException(XmlErrorMessages.message("xml.validate.external.resource.is.not.registered", publicId), publicId, null, 0, 0), ValidateXmlActionHandler.ProblemType.ERROR);
} catch (SAXException ignore) {
}
final XMLInputSource source = new XMLInputSource(xmlResourceIdentifier);
source.setPublicId(publicId);
source.setCharacterStream(new StringReader(""));
return source;
}
return null;
}
XMLInputSource source = new XMLInputSource(xmlResourceIdentifier);
if (xmlResourceIdentifier.getLiteralSystemId() == null) {
VirtualFile virtualFile = psiFile.getVirtualFile();
if (virtualFile != null) {
final String url = VfsUtilCore.fixIDEAUrl(virtualFile.getUrl());
source.setBaseSystemId(url);
source.setSystemId(url);
}
}
source.setPublicId(publicId);
source.setCharacterStream(new StringReader(psiFile.getText()));
return source;
}
use of org.apache.xerces.xni.XNIException in project iaf by ibissource.
the class ValidatorHandlerImpl method startElement.
public void startElement(QName element, XMLAttributes attributes, Augmentations augs) throws XNIException {
if (fContentHandler != null) {
try {
fTypeInfoProvider.beginStartElement(augs, attributes);
fContentHandler.startElement((element.uri != null) ? element.uri : XMLSymbols.EMPTY_STRING, element.localpart, element.rawname, fAttrAdapter);
} catch (SAXException e) {
throw new XNIException(e);
} finally {
fTypeInfoProvider.finishStartElement();
}
}
}
Aggregations