Search in sources :

Example 1 with XNIException

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;
}
Also used : InputSource(org.xml.sax.InputSource) XMLInputSource(org.apache.xerces.xni.parser.XMLInputSource) DOMInputSource(org.apache.xerces.util.DOMInputSource) SAXInputSource(org.apache.xerces.util.SAXInputSource) DOMSource(javax.xml.transform.dom.DOMSource) XMLInputSource(org.apache.xerces.xni.parser.XMLInputSource) Node(org.w3c.dom.Node) NotImplementedException(org.apache.commons.lang.NotImplementedException) XMLEventReader(javax.xml.stream.XMLEventReader) Reader(java.io.Reader) DOMSource(javax.xml.transform.dom.DOMSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) InputSource(org.xml.sax.InputSource) XMLInputSource(org.apache.xerces.xni.parser.XMLInputSource) DOMInputSource(org.apache.xerces.util.DOMInputSource) SAXSource(javax.xml.transform.sax.SAXSource) StAXSource(javax.xml.transform.stax.StAXSource) SAXInputSource(org.apache.xerces.util.SAXInputSource) XNIException(org.apache.xerces.xni.XNIException) SAXException(org.xml.sax.SAXException) SAXParseException(org.xml.sax.SAXParseException) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) IOException(java.io.IOException) DOMInputSource(org.apache.xerces.util.DOMInputSource) SAXSource(javax.xml.transform.sax.SAXSource) SAXInputSource(org.apache.xerces.util.SAXInputSource)

Example 2 with XNIException

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]"));
}
Also used : XMLResourceIdentifier(org.apache.xerces.xni.XMLResourceIdentifier) URL(java.net.URL) XNIException(org.apache.xerces.xni.XNIException) Test(org.junit.Test)

Example 3 with XNIException

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;
}
Also used : Augmentations(org.apache.xerces.xni.Augmentations) QName(org.apache.xerces.xni.QName) XMLLocator(org.apache.xerces.xni.XMLLocator) XMLAttributes(org.apache.xerces.xni.XMLAttributes) Attributes(org.xml.sax.Attributes) XMLLocator(org.apache.xerces.xni.XMLLocator) XNIException(org.apache.xerces.xni.XNIException) URISyntaxException(java.net.URISyntaxException) XNIException(org.apache.xerces.xni.XNIException) FileNotFoundException(java.io.FileNotFoundException) SAXException(org.xml.sax.SAXException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) SAXParseException(org.xml.sax.SAXParseException) DefaultHandler(org.xml.sax.helpers.DefaultHandler) SAXException(org.xml.sax.SAXException) Augmentations(org.apache.xerces.xni.Augmentations) XMLAttributes(org.apache.xerces.xni.XMLAttributes) NamespaceContext(org.apache.xerces.xni.NamespaceContext) XMLReader(org.xml.sax.XMLReader)

Example 4 with XNIException

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;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) XMLInputSource(org.apache.xerces.xni.parser.XMLInputSource) SAXParseException(org.xml.sax.SAXParseException) StringReader(java.io.StringReader) PsiFile(com.intellij.psi.PsiFile) XmlFile(com.intellij.psi.xml.XmlFile) VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiFile(com.intellij.psi.PsiFile) File(java.io.File) URI(java.net.URI) XNIException(org.apache.xerces.xni.XNIException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException) SAXException(org.xml.sax.SAXException) SAXException(org.xml.sax.SAXException) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with XNIException

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();
        }
    }
}
Also used : XNIException(org.apache.xerces.xni.XNIException) SAXException(org.xml.sax.SAXException)

Aggregations

XNIException (org.apache.xerces.xni.XNIException)7 SAXException (org.xml.sax.SAXException)5 IOException (java.io.IOException)3 SAXParseException (org.xml.sax.SAXParseException)3 XMLInputSource (org.apache.xerces.xni.parser.XMLInputSource)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiFile (com.intellij.psi.PsiFile)1 XmlFile (com.intellij.psi.xml.XmlFile)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 InputStream (java.io.InputStream)1 Reader (java.io.Reader)1 StringReader (java.io.StringReader)1 ConnectException (java.net.ConnectException)1 MalformedURLException (java.net.MalformedURLException)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 URL (java.net.URL)1 UnknownHostException (java.net.UnknownHostException)1 XMLEventReader (javax.xml.stream.XMLEventReader)1