Java Code Examples for org.xml.sax.InputSource#setSystemId()

The following examples show how to use org.xml.sax.InputSource#setSystemId() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DTDParser.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Parse a DTD.
 */
public void parse(String uri)
        throws IOException, SAXException {
    InputSource in;

    init();
    // System.out.println ("parse (\"" + uri + "\")");
    in = resolver.resolveEntity(null, uri);

    // If custom resolver punts resolution to parser, handle it ...
    if (in == null) {
        in = Resolver.createInputSource(new java.net.URL(uri), false);

        // ... or if custom resolver doesn't correctly construct the
        // input entity, patch it up enough so relative URIs work, and
        // issue a warning to minimize later confusion.
    } else if (in.getSystemId() == null) {
        warning("P-065", null);
        in.setSystemId(uri);
    }

    parseInternal(in);
}
 
Example 2
Source File: XMLConfiguration.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Loads a configuration file from the specified input source.
 *
 * @param source the input source
 * @throws ConfigurationException if an error occurs
 */
private void load(final InputSource source) throws ConfigurationException
{
    if (locator == null)
    {
        throw new ConfigurationException("Load operation not properly "
                + "initialized! Do not call read(InputStream) directly,"
                + " but use a FileHandler to load a configuration.");
    }

    try
    {
        final URL sourceURL = locator.getSourceURL();
        if (sourceURL != null)
        {
            source.setSystemId(sourceURL.toString());
        }

        final DocumentBuilder builder = createDocumentBuilder();
        final Document newDocument = builder.parse(source);
        final Document oldDocument = getDocument();
        initProperties(XMLDocumentHelper.forSourceDocument(newDocument),
                oldDocument == null);
    }
    catch (final SAXParseException spe)
    {
        throw new ConfigurationException("Error parsing " + source.getSystemId(), spe);
    }
    catch (final Exception e)
    {
        this.getLogger().debug("Unable to load the configuration: " + e);
        throw new ConfigurationException("Unable to load the configuration", e);
    }
}
 
Example 3
Source File: UnmarshallerImpl.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
private static InputSource streamSourceToInputSource( StreamSource ss ) {
    InputSource is = new InputSource();
    is.setSystemId( ss.getSystemId() );
    is.setByteStream( ss.getInputStream() );
    is.setCharacterStream( ss.getReader() );

    return is;
}
 
Example 4
Source File: ValidatorHandlerImpl.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Resolves the given resource and adapts the <code>LSInput</code>
 * returned into an <code>InputSource</code>.
 */
public InputSource resolveEntity(String name, String publicId,
        String baseURI, String systemId) throws SAXException, IOException {
    if (fEntityResolver != null) {
        LSInput lsInput = fEntityResolver.resolveResource(XML_TYPE, null, publicId, systemId, baseURI);
        if (lsInput != null) {
            final String pubId = lsInput.getPublicId();
            final String sysId = lsInput.getSystemId();
            final String baseSystemId = lsInput.getBaseURI();
            final Reader charStream = lsInput.getCharacterStream();
            final InputStream byteStream = lsInput.getByteStream();
            final String data = lsInput.getStringData();
            final String encoding = lsInput.getEncoding();

            /**
             * An LSParser looks at inputs specified in LSInput in
             * the following order: characterStream, byteStream,
             * stringData, systemId, publicId. For consistency
             * with the DOM Level 3 Load and Save Recommendation
             * use the same lookup order here.
             */
            InputSource inputSource = new InputSource();
            inputSource.setPublicId(pubId);
            inputSource.setSystemId((baseSystemId != null) ? resolveSystemId(systemId, baseSystemId) : systemId);

            if (charStream != null) {
                inputSource.setCharacterStream(charStream);
            }
            else if (byteStream != null) {
                inputSource.setByteStream(byteStream);
            }
            else if (data != null && data.length() != 0) {
                inputSource.setCharacterStream(new StringReader(data));
            }
            inputSource.setEncoding(encoding);
            return inputSource;
        }
    }
    return null;
}
 
Example 5
Source File: UnmarshallerImpl.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private static InputSource streamSourceToInputSource( StreamSource ss ) {
    InputSource is = new InputSource();
    is.setSystemId( ss.getSystemId() );
    is.setByteStream( ss.getInputStream() );
    is.setCharacterStream( ss.getReader() );

    return is;
}
 
Example 6
Source File: ModelLoader.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Parses a set of schemas inside a WSDL file.
 *
 * A WSDL file may contain multiple &lt;xsd:schema> elements.
 */
private XSSchemaSet loadWSDL()
    throws SAXException {


    // build DOMForest just like we handle XML Schema
    DOMForest forest = buildDOMForest( new XMLSchemaInternalizationLogic() );

    DOMForestScanner scanner = new DOMForestScanner(forest);

    XSOMParser xsomParser = createXSOMParser( forest );

    // find <xsd:schema>s and parse them individually
    for( InputSource grammar : opt.getGrammars() ) {
        Document wsdlDom = forest.get( grammar.getSystemId() );
        if (wsdlDom == null) {
            String systemId = Options.normalizeSystemId(grammar.getSystemId());
            if (forest.get(systemId) != null) {
                grammar.setSystemId(systemId);
                wsdlDom = forest.get( grammar.getSystemId() );
            }
        }

        NodeList schemas = wsdlDom.getElementsByTagNameNS(XMLConstants.W3C_XML_SCHEMA_NS_URI,"schema");
        for( int i=0; i<schemas.getLength(); i++ )
            scanner.scan( (Element)schemas.item(i), xsomParser.getParserHandler() );
    }
    return xsomParser.getResult();
}
 
Example 7
Source File: ModelLoader.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Parses a set of schemas inside a WSDL file.
 *
 * A WSDL file may contain multiple &lt;xsd:schema> elements.
 */
private XSSchemaSet loadWSDL()
    throws SAXException {


    // build DOMForest just like we handle XML Schema
    DOMForest forest = buildDOMForest( new XMLSchemaInternalizationLogic() );

    DOMForestScanner scanner = new DOMForestScanner(forest);

    XSOMParser xsomParser = createXSOMParser( forest );

    // find <xsd:schema>s and parse them individually
    for( InputSource grammar : opt.getGrammars() ) {
        Document wsdlDom = forest.get( grammar.getSystemId() );
        if (wsdlDom == null) {
            String systemId = Options.normalizeSystemId(grammar.getSystemId());
            if (forest.get(systemId) != null) {
                grammar.setSystemId(systemId);
                wsdlDom = forest.get( grammar.getSystemId() );
            }
        }

        NodeList schemas = wsdlDom.getElementsByTagNameNS(XMLConstants.W3C_XML_SCHEMA_NS_URI,"schema");
        for( int i=0; i<schemas.getLength(); i++ )
            scanner.scan( (Element)schemas.item(i), xsomParser.getParserHandler() );
    }
    return xsomParser.getResult();
}
 
Example 8
Source File: TriXParser.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Parses the data from the supplied InputStream, using the supplied baseURI to resolve any relative URI references.
 *
 * @param in      The InputStream from which to read the data, must not be <tt>null</tt>.
 * @param baseURI The URI associated with the data in the InputStream, must not be <tt>null</tt>.
 * @throws IOException              If an I/O error occurred while data was read from the InputStream.
 * @throws RDFParseException        If the parser has found an unrecoverable parse error.
 * @throws RDFHandlerException      If the configured statement handler encountered an unrecoverable error.
 * @throws IllegalArgumentException If the supplied input stream or base URI is <tt>null</tt>.
 */
@Override
public void parse(InputStream in, String baseURI) throws IOException, RDFParseException, RDFHandlerException {
	if (in == null) {
		throw new IllegalArgumentException("Input stream cannot be 'null'");
	}
	if (baseURI == null) {
		throw new IllegalArgumentException("Base URI cannot be 'null'");
	}

	InputSource inputSource = new InputSource(new BOMInputStream(in, false));
	inputSource.setSystemId(baseURI);

	parse(inputSource);
}
 
Example 9
Source File: Options.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
private InputSource absolutize(InputSource is) {
    // absolutize all the system IDs in the input, so that we can map system IDs to DOM trees.
    try {
        URL baseURL = new File(".").getCanonicalFile().toURL();
        is.setSystemId( new URL(baseURL,is.getSystemId()).toExternalForm() );
    } catch( IOException e ) {
        logger.log(Level.FINE, "{0}, {1}", new Object[]{is.getSystemId(), e.getLocalizedMessage()});
    }
    return is;
}
 
Example 10
Source File: ValidatorHandlerImpl.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
/**
 * Resolves the given resource and adapts the <code>LSInput</code>
 * returned into an <code>InputSource</code>.
 */
public InputSource resolveEntity(String name, String publicId,
        String baseURI, String systemId) throws SAXException, IOException {
    if (fEntityResolver != null) {
        LSInput lsInput = fEntityResolver.resolveResource(XML_TYPE, null, publicId, systemId, baseURI);
        if (lsInput != null) {
            final String pubId = lsInput.getPublicId();
            final String sysId = lsInput.getSystemId();
            final String baseSystemId = lsInput.getBaseURI();
            final Reader charStream = lsInput.getCharacterStream();
            final InputStream byteStream = lsInput.getByteStream();
            final String data = lsInput.getStringData();
            final String encoding = lsInput.getEncoding();

            /**
             * An LSParser looks at inputs specified in LSInput in
             * the following order: characterStream, byteStream,
             * stringData, systemId, publicId. For consistency
             * with the DOM Level 3 Load and Save Recommendation
             * use the same lookup order here.
             */
            InputSource inputSource = new InputSource();
            inputSource.setPublicId(pubId);
            inputSource.setSystemId((baseSystemId != null) ? resolveSystemId(systemId, baseSystemId) : systemId);

            if (charStream != null) {
                inputSource.setCharacterStream(charStream);
            }
            else if (byteStream != null) {
                inputSource.setByteStream(byteStream);
            }
            else if (data != null && data.length() != 0) {
                inputSource.setCharacterStream(new StringReader(data));
            }
            inputSource.setEncoding(encoding);
            return inputSource;
        }
    }
    return null;
}
 
Example 11
Source File: XSLTC.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Compiles an XSL stylesheet pointed to by a URL
 * @param url An URL containing the input XSL stylesheet
 */
public boolean compile(URL url) {
    try {
        // Open input stream from URL and wrap inside InputSource
        final InputStream stream = url.openStream();
        final InputSource input = new InputSource(stream);
        input.setSystemId(url.toString());
        return compile(input, _className);
    }
    catch (IOException e) {
        _parser.reportError(Constants.FATAL, new ErrorMsg(ErrorMsg.JAXP_COMPILE_ERR, e));
        return false;
    }
}
 
Example 12
Source File: RelativeXIncludeTest.java    From simple-binary-encoding with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldParseFileInSubDir() throws Exception
{
    final URL testResource = getClass().getClassLoader().getResource("sub/basic-schema.xml");
    assertNotNull(testResource);

    final InputSource is = new InputSource(testResource.openStream());
    final File file = new File(testResource.getFile());
    is.setSystemId(file.toPath().toAbsolutePath().getParent().toUri().toString());
    final MessageSchema messageSchema = parse(is, ParserOptions.DEFAULT);

    assertNotNull(messageSchema.getType("Symbol"));
}
 
Example 13
Source File: XMLHelper.java    From ant-ivy with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
public static void parse(URL xmlURL, URL schema, DefaultHandler handler, LexicalHandler lHandler)
        throws SAXException, IOException, ParserConfigurationException {
    try (InputStream xmlStream = URLHandlerRegistry.getDefault().openStream(xmlURL)) {
        InputSource inSrc = new InputSource(xmlStream);
        inSrc.setSystemId(toSystemId(xmlURL));
        parse(inSrc, schema, handler, lHandler);
    }
}
 
Example 14
Source File: DocumentBuilder.java    From jdk8u60 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Parse the content of the given <code>InputStream</code> as an
 * XML document and return a new DOM {@link Document} object.
 * An <code>IllegalArgumentException</code> is thrown if the
 * <code>InputStream</code> is null.
 *
 * @param is InputStream containing the content to be parsed.
 * @param systemId Provide a base for resolving relative URIs.
 *
 * @return A new DOM Document object.
 *
 * @throws IOException If any IO errors occur.
 * @throws SAXException If any parse errors occur.
 * @throws IllegalArgumentException When <code>is</code> is <code>null</code>
 *
 * @see org.xml.sax.DocumentHandler
 */

public Document parse(InputStream is, String systemId)
    throws SAXException, IOException {
    if (is == null) {
        throw new IllegalArgumentException("InputStream cannot be null");
    }

    InputSource in = new InputSource(is);
    in.setSystemId(systemId);
    return parse(in);
}
 
Example 15
Source File: DocumentBuilder.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Parse the content of the given <code>InputStream</code> as an
 * XML document and return a new DOM {@link Document} object.
 * An <code>IllegalArgumentException</code> is thrown if the
 * <code>InputStream</code> is null.
 *
 * @param is InputStream containing the content to be parsed.
 * @param systemId Provide a base for resolving relative URIs.
 *
 * @return A new DOM Document object.
 *
 * @throws IOException If any IO errors occur.
 * @throws SAXException If any parse errors occur.
 * @throws IllegalArgumentException When <code>is</code> is <code>null</code>
 *
 * @see org.xml.sax.DocumentHandler
 */

public Document parse(InputStream is, String systemId)
    throws SAXException, IOException {
    if (is == null) {
        throw new IllegalArgumentException("InputStream cannot be null");
    }

    InputSource in = new InputSource(is);
    in.setSystemId(systemId);
    return parse(in);
}
 
Example 16
Source File: SAXParser.java    From jdk8u60 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * <p>Parse the content of the given {@link java.io.InputStream}
 * instance as XML using the specified {@link org.xml.sax.HandlerBase}.
 * <i> Use of the DefaultHandler version of this method is recommended as
 * the HandlerBase class has been deprecated in SAX 2.0</i>.</p>
 *
 * @param is InputStream containing the content to be parsed.
 * @param hb The SAX HandlerBase to use.
 * @param systemId The systemId which is needed for resolving relative URIs.
 *
 * @throws IllegalArgumentException If the given <code>InputStream</code> is
 *   <code>null</code>.
 * @throws IOException If any IO error occurs interacting with the
 *   <code>InputStream</code>.
 * @throws SAXException If any SAX errors occur during processing.
 *
 * @see org.xml.sax.DocumentHandler version of this method instead.
 */
public void parse(
    InputStream is,
    HandlerBase hb,
    String systemId)
    throws SAXException, IOException {
    if (is == null) {
        throw new IllegalArgumentException("InputStream cannot be null");
    }

    InputSource input = new InputSource(is);
    input.setSystemId(systemId);
    this.parse(input, hb);
}
 
Example 17
Source File: SAXParser.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Parse the content of the given {@link java.io.InputStream}
 * instance as XML using the specified
 * {@link org.xml.sax.helpers.DefaultHandler}.
 *
 * @param is InputStream containing the content to be parsed.
 * @param dh The SAX DefaultHandler to use.
 * @param systemId The systemId which is needed for resolving relative URIs.
 *
 * @throws IllegalArgumentException If the given InputStream is null.
 * @throws IOException If any IO errors occur.
 * @throws SAXException If any SAX errors occur during processing.
 *
 * @see org.xml.sax.DocumentHandler version of this method instead.
 */
public void parse(
    InputStream is,
    DefaultHandler dh,
    String systemId)
    throws SAXException, IOException {
    if (is == null) {
        throw new IllegalArgumentException("InputStream cannot be null");
    }

    InputSource input = new InputSource(is);
    input.setSystemId(systemId);
    this.parse(input, dh);
}
 
Example 18
Source File: XSLTC.java    From Bytecoder with Apache License 2.0 2 votes vote down vote up
/**
 * Compiles an XSL stylesheet passed in through an InputStream
 * @param stream An InputStream that will pass in the stylesheet contents
 * @param name The name of the translet class to generate
 * @return 'true' if the compilation was successful
 */
public boolean compile(InputStream stream, String name) {
    final InputSource input = new InputSource(stream);
    input.setSystemId(name); // We have nothing else!!!
    return compile(input, name);
}
 
Example 19
Source File: XSLTC.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Compiles an XSL stylesheet passed in through an InputStream
 * @param stream An InputStream that will pass in the stylesheet contents
 * @param name The name of the translet class to generate
 * @return 'true' if the compilation was successful
 */
public boolean compile(InputStream stream, String name) {
    final InputSource input = new InputSource(stream);
    input.setSystemId(name); // We have nothing else!!!
    return compile(input, name);
}
 
Example 20
Source File: SaxResourceUtils.java    From spring-analysis-note with MIT License 2 votes vote down vote up
/**
 * Create a SAX {@code InputSource} from the given resource.
 * <p>Sets the system identifier to the resource's {@code URL}, if available.
 * @param resource the resource
 * @return the input source created from the resource
 * @throws IOException if an I/O exception occurs
 * @see InputSource#setSystemId(String)
 * @see #getSystemId(org.springframework.core.io.Resource)
 */
public static InputSource createInputSource(Resource resource) throws IOException {
	InputSource inputSource = new InputSource(resource.getInputStream());
	inputSource.setSystemId(getSystemId(resource));
	return inputSource;
}