Example usage for org.xml.sax InputSource setSystemId

List of usage examples for org.xml.sax InputSource setSystemId

Introduction

In this page you can find the example usage for org.xml.sax InputSource setSystemId.

Prototype

public void setSystemId(String systemId) 

Source Link

Document

Set the system identifier for this input source.

Usage

From source file:org.mule.config.MuleDtdResolver.java

public InputSource resolveEntity(String publicId, String systemId) throws IOException, SAXException {
    logger.debug("Trying to resolve XML entity with public ID: " + publicId + " and system ID: " + systemId);

    InputSource source = null;
    currentXsl = null;//from w  w  w .  j  a  v  a2  s . co m
    if (delegate != null) {
        source = delegate.resolveEntity(publicId, systemId);
    }
    if ((source == null) && StringUtils.isNotBlank(systemId) && systemId.endsWith(".dtd")) {
        String[] tokens = systemId.split("/");
        String dtdFile = tokens[tokens.length - 1];
        logger.debug("Looking on classpath for " + SEARCH_PATH + dtdFile);

        InputStream is = IOUtils.getResourceAsStream(SEARCH_PATH + dtdFile, getClass(), /* tryAsFile */
                true, /* tryAsUrl */false);
        if (is != null) {
            source = new InputSource(is);
            source.setPublicId(publicId);
            source.setSystemId(systemId);
            logger.debug("Found on classpath mule DTD: " + systemId);
            currentXsl = xsl;
            return source;
        }
        logger.debug("Could not find dtd resource on classpath: " + SEARCH_PATH + dtdFile);
    }
    return source;
}

From source file:org.mycore.common.content.MCRContent.java

/**
 * Returns content as SAX input source.//from   w ww .  j  av  a  2s . c  o m
 * 
 * @return input source to read content from
 */
public InputSource getInputSource() throws IOException {
    InputSource source = new InputSource(getInputStream());
    source.setSystemId(getSystemId());
    return source;
}

From source file:org.ojbc.web.portal.services.XslFormattersTest.java

private SAXSource createSourceAndSetSystemId(Resource inputStream) {
    try {/*  w  w  w  .  j a va2  s  .c o m*/
        InputSource inputSource = new InputSource(inputStream.getInputStream());
        inputSource.setEncoding(CharEncoding.UTF_8);
        inputSource.setSystemId(inputStream.getURL().toExternalForm());
        return new SAXSource(inputSource);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.openhealthtools.openatna.anom.codes.CodeParser.java

public static void parse(Collection<String> codes) {
    try {/* www  .j a v a2  s . c  o m*/
        SAXParserFactory spf = SAXParserFactory.newInstance();
        spf.setValidating(false);
        javax.xml.parsers.SAXParser sp = spf.newSAXParser();
        Handler handler = new Handler();
        for (String code : codes) {
            try {
                URL url = new URL(code);
                InputSource input = new InputSource(url.openStream());
                input.setSystemId(code.toString());
                sp.parse(input, handler);
            } catch (Exception e) {
                log.warn("Error parsing codes at:" + code);
            }
        }
    } catch (Exception e) {
        throw new RuntimeException("Error loading system codes", e);
    }
}

From source file:org.openmrs.module.UpdateFileParser.java

/**
 * Parse the contents of the update.rdf file.
 *
 * @throws ModuleException/*from   w  w w .j  av  a2s  .  c om*/
 */
public void parse() throws ModuleException {
    StringReader stringReader = null;
    try {
        Document updateDoc = null;
        try {
            stringReader = new StringReader(content);
            InputSource inputSource = new InputSource(stringReader);
            inputSource.setSystemId("./");

            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();

            // Disable resolution of external entities. See TRUNK-3942 
            db.setEntityResolver(new EntityResolver() {

                public InputSource resolveEntity(String publicId, String systemId) {
                    return new InputSource(new StringReader(""));
                }
            });

            updateDoc = db.parse(inputSource);
        } catch (Exception e) {
            log.warn("Unable to parse content");
            throw new ModuleException("Error parsing update.rdf file: " + content, e);
        }

        Element rootNode = updateDoc.getDocumentElement();

        String configVersion = rootNode.getAttribute("configVersion");

        if (!validConfigVersions().contains(configVersion)) {
            throw new ModuleException(
                    "Invalid configVersion: '" + configVersion + "' found In content: " + content);
        }

        if ("1.0".equals(configVersion)) {
            // the only update in the xml file is the 'best fit'
            this.moduleId = getElement(rootNode, configVersion, "moduleId");
            this.currentVersion = getElement(rootNode, configVersion, "currentVersion");
            this.downloadURL = getElement(rootNode, configVersion, "downloadURL");
        } else if ("1.1".equals(configVersion)) {

            this.moduleId = rootNode.getAttribute("moduleId");

            NodeList nodes = rootNode.getElementsByTagName("update");
            this.currentVersion = ""; // default to the lowest version possible

            // loop over all 'update' tags
            for (Integer i = 0; i < nodes.getLength(); i++) {
                Element currentNode = (Element) nodes.item(i);
                String currentVersion = getElement(currentNode, configVersion, "currentVersion");
                // if the currently saved version is less than the current tag
                if (ModuleUtil.compareVersion(this.currentVersion, currentVersion) < 0) {
                    String requireOpenMRSVersion = getElement(currentNode, configVersion,
                            "requireOpenMRSVersion");
                    // if the openmrs code version is compatible, this node is a winner
                    if (requireOpenMRSVersion == null || ModuleUtil.matchRequiredVersions(
                            OpenmrsConstants.OPENMRS_VERSION_SHORT, requireOpenMRSVersion)) {
                        this.currentVersion = currentVersion;
                        this.downloadURL = getElement(currentNode, configVersion, "downloadURL");
                    }
                }
            }
        }
    } catch (ModuleException e) {
        // rethrow the moduleException
        throw e;
    } finally {
        if (stringReader != null) {
            stringReader.close();
        }
    }

}

From source file:org.pentaho.reporting.libraries.xmlns.parser.ParserEntityResolver.java

/**
 * Allow the application to resolve external entities.
 * <p/>/*from   w  w w. j  a  v  a 2 s  .c om*/
 * Resolves the DTD definition to point to a local copy, if the specified public ID is known to this resolver.
 *
 * @param publicId the public ID.
 * @param systemId the system ID.
 * @return The input source.
 */
public InputSource resolveEntity(final String publicId, final String systemId) {
    try {
        // cannot validate without public id ...
        if (publicId == null) {
            //Log.debug ("No PUBLIC ID, cannot continue");
            if (systemId != null) {
                final URL location = getDTDLocation(systemId);
                if (location != null) {
                    final InputSource inputSource = new InputSource(location.openStream());
                    inputSource.setSystemId(systemId);
                    return inputSource;
                }
            }
            return null;
        }

        final URL location = getDTDLocation(publicId);
        if (location != null) {
            final InputSource inputSource = new InputSource(location.openStream());
            inputSource.setSystemId(systemId);
            inputSource.setPublicId(publicId);
            return inputSource;
        }
        final String message = getDeprecatedDTDMessage(publicId);
        if (message != null) {
            logger.info(message);
        } else {
            logger.info("A public ID was given for the document, but it was unknown or invalid.");
        }
        return null;
    } catch (IOException ioe) {
        logger.warn("Unable to open specified DTD", ioe);
    }
    return null;
}

From source file:org.quartz.xml.JobSchedulingDataProcessor.java

/**
 * Process the xmlfile named <code>fileName</code> with the given system
 * ID./*w w w.j av a2  s. com*/
 * 
 * @param fileName
 *          meta data file name.
 * @param systemId
 *          system ID.
 */
public void processFile(String fileName, String systemId)
        throws ValidationException, ParserConfigurationException, SAXException, IOException, SchedulerException,
        ClassNotFoundException, ParseException {
    clearValidationExceptions();

    scheduledJobs.clear();
    jobsToSchedule.clear();
    calsToSchedule.clear();

    getLog().info("Parsing XML file: " + fileName + " with systemId: " + systemId + " validating: "
            + digester.getValidating() + " validating schema: " + digester.getSchema());
    InputSource is = new InputSource(getInputStream(fileName));
    is.setSystemId(systemId);
    digester.push(this);
    digester.parse(is);

    maybeThrowValidationException();
}

From source file:org.quartz.xml.JobSchedulingDataProcessor.java

/**
 * Process the xmlfile named <code>fileName</code> with the given system
 * ID.//from   w  w  w  .  ja  v  a  2  s. com
 * 
 * @param stream
 *          an input stream containing the xml content.
 * @param systemId
 *          system ID.
 */
public void processStream(InputStream stream, String systemId)
        throws ValidationException, ParserConfigurationException, SAXException, IOException, SchedulerException,
        ClassNotFoundException, ParseException {
    clearValidationExceptions();

    scheduledJobs.clear();
    jobsToSchedule.clear();
    calsToSchedule.clear();

    getLog().info("Parsing XML from stream with systemId: " + systemId + " validating: "
            + digester.getValidating() + " validating schema: " + digester.getSchema());
    InputSource is = new InputSource(stream);
    is.setSystemId(systemId);
    digester.push(this);
    digester.parse(is);

    maybeThrowValidationException();
}

From source file:org.quartz.xml.JobSchedulingDataProcessor.java

/**
 * EntityResolver interface.//from ww w .j av  a  2s.  c  o m
 * <p/>
 * Allow the application to resolve external entities.
 * <p/>
 * Until <code>quartz.dtd</code> has a public ID, it must resolved as a
 * system ID. Here's the order of resolution (if one fails, continue to the
 * next).
 * <ol>
 * <li>Tries to resolve the <code>systemId</code> with <code>ClassLoader.getResourceAsStream(String)</code>.
 * </li>
 * <li>If the <code>systemId</code> starts with <code>QUARTZ_SYSTEM_ID_PREFIX</code>,
 * then resolve the part after <code>QUARTZ_SYSTEM_ID_PREFIX</code> with
 * <code>ClassLoader.getResourceAsStream(String)</code>.</li>
 * <li>Else try to resolve <code>systemId</code> as a URL.
 * <li>If <code>systemId</code> has a colon in it, create a new <code>URL</code>
 * </li>
 * <li>Else resolve <code>systemId</code> as a <code>File</code> and
 * then call <code>File.toURL()</code>.</li>
 * </li>
 * </ol>
 * <p/>
 * If the <code>publicId</code> does exist, resolve it as a URL.  If the
 * <code>publicId</code> is the Quartz public ID, then resolve it locally.
 * 
 * @param publicId
 *          The public identifier of the external entity being referenced,
 *          or null if none was supplied.
 * @param systemId
 *          The system identifier of the external entity being referenced.
 * @return An InputSource object describing the new input source, or null
 *         to request that the parser open a regular URI connection to the
 *         system identifier.
 * @exception SAXException
 *              Any SAX exception, possibly wrapping another exception.
 * @exception IOException
 *              A Java-specific IO exception, possibly the result of
 *              creating a new InputStream or Reader for the InputSource.
 */
public InputSource resolveEntity(String publicId, String systemId) {
    InputSource inputSource = null;

    InputStream is = null;

    URL url = null;

    try {
        if (publicId == null) {
            if (systemId != null) {
                // resolve Quartz Schema locally
                if (QUARTZ_SCHEMA.equals(systemId)) {
                    is = getClass().getResourceAsStream(QUARTZ_XSD);
                } else {
                    is = getInputStream(systemId);

                    if (is == null) {
                        int start = systemId.indexOf(QUARTZ_SYSTEM_ID_PREFIX);

                        if (start > -1) {
                            String fileName = systemId.substring(QUARTZ_SYSTEM_ID_PREFIX.length());
                            is = getInputStream(fileName);
                        } else {
                            if (systemId.indexOf(':') == -1) {
                                File file = new java.io.File(systemId);
                                url = file.toURL();
                            } else {
                                url = new URL(systemId);
                            }

                            is = url.openStream();
                        }
                    }
                }
            }
        } else {
            // resolve Quartz DTD locally
            if (QUARTZ_PUBLIC_ID.equals(publicId)) {
                is = getClass().getResourceAsStream(QUARTZ_DTD);
            } else {
                url = new URL(systemId);
                is = url.openStream();
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (is != null) {
            inputSource = new InputSource(is);
            inputSource.setPublicId(publicId);
            inputSource.setSystemId(systemId);
        }

    }

    return inputSource;
}

From source file:org.rimudb.configuration.AbstractXmlLoader.java

private Document loadXML(InputStream is) throws Exception {
    // Load document
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);//  w  w w. j a v a2s. c om

    factory.setIgnoringElementContentWhitespace(true);
    factory.setIgnoringComments(true);
    factory.setValidating(false); // Don't use DTD validation

    DocumentBuilder docBuilder = factory.newDocumentBuilder();

    ErrorHandler eh = new StrictErrorHandler();
    docBuilder.setErrorHandler(eh);

    InputSource inputSource = new InputSource(is);
    inputSource.setPublicId(RimuDBNamespace.URI);
    inputSource.setSystemId(RimuDBNamespace.URI);

    Document document = docBuilder.parse(is);
    is.close();

    // Determine the XML schema version from the XML document without validating
    Element root = document.getDocumentElement();
    setDocumentSchema(lookupSchemaVersion(root));

    // Validate the XML document and determine the XML Schema version
    if (isValidateXML()) {
        if (getDocumentSchema() != null) {
            // Validate the document against the schema found in the document 
            SAXParseException saxParseException = validate(document, getDocumentSchema());
            if (saxParseException != null) {
                throw saxParseException;
            }
        } else {
            setDocumentSchema(lookupSchemaByValidation(document));
        }
    }

    return document;
}