Example usage for org.xml.sax InputSource setPublicId

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

Introduction

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

Prototype

public void setPublicId(String publicId) 

Source Link

Document

Set the public identifier for this input source.

Usage

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

private Document loadXML(InputStream is) throws Exception {
    // Load document
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);//from   w w  w .  ja  va  2 s .  co m

    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;
}

From source file:org.seasar.mayaa.impl.util.xml.XMLHandler.java

public InputSource resolveEntity(String publicId, String systemId) {
    String path = systemId;/*from   w ww. j a v a  2  s  .co m*/
    Map entities = getEntityMap();
    if (entities != null && entities.containsKey(publicId)) {
        path = (String) entities.get(publicId);
    } else if (entities != null && entities.containsKey(systemId)) {
        path = (String) entities.get(systemId);
    } else {
        int pos = systemId.lastIndexOf('/');
        if (pos != -1) {
            path = systemId.substring(pos);
        }
    }
    Class neighborClass = getNeighborClass();
    if (neighborClass == null) {
        neighborClass = getClass();
    }
    ClassLoaderSourceDescriptor source = new ClassLoaderSourceDescriptor();
    source.setSystemID(path);
    source.setNeighborClass(neighborClass);
    if (source.exists()) {
        InputSource ret = new InputSource(source.getInputStream());
        ret.setPublicId(publicId);
        ret.setSystemId(path);
        return ret;
    }
    Log log = getLog();
    if (log != null && log.isWarnEnabled()) {
        String message = StringUtil.getMessage(XMLHandler.class, 0, publicId, systemId);
        log.warn(message);
    }
    return null;
}

From source file:org.springframework.beans.factory.xml.BeansDtdResolver.java

@Override
@Nullable//from  ww  w .  j av  a2s  .c o m
public InputSource resolveEntity(String publicId, @Nullable String systemId) throws IOException {
    if (logger.isTraceEnabled()) {
        logger.trace("Trying to resolve XML entity with public ID [" + publicId + "] and system ID [" + systemId
                + "]");
    }
    if (systemId != null && systemId.endsWith(DTD_EXTENSION)) {
        int lastPathSeparator = systemId.lastIndexOf("/");
        int dtdNameStart = systemId.indexOf(DTD_NAME, lastPathSeparator);
        if (dtdNameStart != -1) {
            String dtdFile = DTD_NAME + DTD_EXTENSION;
            if (logger.isTraceEnabled()) {
                logger.trace("Trying to locate [" + dtdFile + "] in Spring jar on classpath");
            }
            try {
                Resource resource = new ClassPathResource(dtdFile, getClass());
                InputSource source = new InputSource(resource.getInputStream());
                source.setPublicId(publicId);
                source.setSystemId(systemId);
                if (logger.isDebugEnabled()) {
                    logger.debug("Found beans DTD [" + systemId + "] in classpath: " + dtdFile);
                }
                return source;
            } catch (IOException ex) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Could not resolve beans DTD [" + systemId + "]: not found in classpath", ex);
                }
            }

        }
    }

    // Use the default behavior -> download from website or wherever.
    return null;
}

From source file:org.springframework.beans.factory.xml.PluggableSchemaResolver.java

@Override
@Nullable//from  w ww .  ja  v  a  2  s.c o m
public InputSource resolveEntity(String publicId, @Nullable String systemId) throws IOException {
    if (logger.isTraceEnabled()) {
        logger.trace("Trying to resolve XML entity with public id [" + publicId + "] and system id [" + systemId
                + "]");
    }

    if (systemId != null) {
        String resourceLocation = getSchemaMappings().get(systemId);
        if (resourceLocation != null) {
            Resource resource = new ClassPathResource(resourceLocation, this.classLoader);
            try {
                InputSource source = new InputSource(resource.getInputStream());
                source.setPublicId(publicId);
                source.setSystemId(systemId);
                if (logger.isDebugEnabled()) {
                    logger.debug("Found XML schema [" + systemId + "] in classpath: " + resourceLocation);
                }
                return source;
            } catch (FileNotFoundException ex) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Couldn't find XML schema [" + systemId + "]: " + resource, ex);
                }
            }
        }
    }
    return null;
}

From source file:org.springframework.beans.factory.xml.ResourceEntityResolver.java

@Override
@Nullable//from   www.  ja v a2 s . c om
public InputSource resolveEntity(String publicId, @Nullable String systemId) throws SAXException, IOException {
    InputSource source = super.resolveEntity(publicId, systemId);
    if (source == null && systemId != null) {
        String resourcePath = null;
        try {
            String decodedSystemId = URLDecoder.decode(systemId, "UTF-8");
            String givenUrl = new URL(decodedSystemId).toString();
            String systemRootUrl = new File("").toURI().toURL().toString();
            // Try relative to resource base if currently in system root.
            if (givenUrl.startsWith(systemRootUrl)) {
                resourcePath = givenUrl.substring(systemRootUrl.length());
            }
        } catch (Exception ex) {
            // Typically a MalformedURLException or AccessControlException.
            if (logger.isDebugEnabled()) {
                logger.debug("Could not resolve XML entity [" + systemId + "] against system root URL", ex);
            }
            // No URL (or no resolvable URL) -> try relative to resource base.
            resourcePath = systemId;
        }
        if (resourcePath != null) {
            if (logger.isTraceEnabled()) {
                logger.trace(
                        "Trying to locate XML entity [" + systemId + "] as resource [" + resourcePath + "]");
            }
            Resource resource = this.resourceLoader.getResource(resourcePath);
            source = new InputSource(resource.getInputStream());
            source.setPublicId(publicId);
            source.setSystemId(systemId);
            if (logger.isDebugEnabled()) {
                logger.debug("Found XML entity [" + systemId + "]: " + resource);
            }
        }
    }
    return source;
}

From source file:org.springmodules.remoting.xmlrpc.dom.XmlRpcDtdResolver.java

/**
 * @see EntityResolver#resolveEntity(String, String)
 */// w  ww . ja va2  s  .com
public InputSource resolveEntity(String publicId, String systemId) {
    if (logger.isDebugEnabled()) {
        logger.debug("Trying to resolve XML entity with public ID [" + publicId + "] and system ID [" + systemId
                + "]");
    }
    if (systemId != null && systemId.indexOf(DTD_NAME) > systemId.lastIndexOf("/")) {
        String dtdFile = systemId.substring(systemId.indexOf(DTD_NAME));

        if (logger.isDebugEnabled()) {
            logger.debug("Trying to locate [" + dtdFile + "] under [" + SEARCH_PACKAGE + "]");
        }

        try {
            Resource resource = new ClassPathResource(SEARCH_PACKAGE + dtdFile, getClass());
            InputSource source = new InputSource(resource.getInputStream());
            source.setPublicId(publicId);
            source.setSystemId(systemId);

            if (logger.isDebugEnabled()) {
                logger.debug("Found beans DTD [" + systemId + "] in classpath");
            }
            return source;

        } catch (IOException ex) {
            if (logger.isDebugEnabled()) {
                logger.debug("Could not resolve beans DTD [" + systemId + "]: not found in class path", ex);
            }
        }
    }
    return null;
}

From source file:org.topazproject.xml.transform.CustomEntityResolver.java

/**
 * Resolve the specified entity using the configured <code>URLRetriever</code> (and any
 * of its delegates)./* w  w w.  j  av a 2s  .c  om*/
 *
 * @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.
 * @throws IOException Indicates a problem retrieving the entity.
 */
public InputSource resolveEntity(String publicId, String systemId) throws IOException {
    if (log.isDebugEnabled())
        log.debug("Resolving entity '" + systemId + "'");

    byte[] res = retriever.retrieve(systemId, publicId);
    if (log.isDebugEnabled())
        log.debug("Entity '" + systemId + "' " + (res != null ? "found" : "not found"));

    if (res == null)
        return null;

    InputSource is = new InputSource(new ByteArrayInputStream(res));
    is.setPublicId(publicId);
    is.setSystemId(systemId);

    return is;
}

From source file:org.wso2.carbon.bpel.core.ode.integration.axis2.Axis2UriResolver.java

public InputSource resolveEntity(String targetNamespace, String schemaLocation, String baseUri) {
    if (log.isDebugEnabled()) {
        log.debug("resolveEntity: targetNamespace=" + targetNamespace + " schemaLocation=" + schemaLocation
                + " baseUri=" + baseUri);
    }/*from  w w w.j av a2s.co  m*/
    InputStream is;
    try {
        URI base = new URI(baseUri);
        URI uri = base.resolve(schemaLocation);
        is = uri.toURL().openStream();
        if (is == null) {
            log.error("Exception resolving entity: schemaLocation=" + schemaLocation + " baseUri=" + baseUri);
            return null;
        }
        InputSource source = new InputSource(is);
        source.setSystemId(uri.toString());
        source.setPublicId(schemaLocation);
        return new InputSource(is);
    } catch (Exception e) {
        log.error("Exception resolving entity: schemaLocation=" + schemaLocation + " baseUri=" + baseUri, e);
        return null;
    }
}

From source file:org.wso2.carbon.humantask.core.integration.HumanTaskSchemaURIResolver.java

public InputSource resolveEntity(String targetNamespace, String schemaLocation, String baseUri) {
    if (log.isDebugEnabled()) {
        log.debug("resolveEntity: targetNamespace=" + targetNamespace + " schemaLocation=" + schemaLocation
                + " baseUri=" + baseUri);
    }/*from  w  ww  . j  a  va2 s  . c  om*/
    InputStream is;
    try {
        URI base = new URI("file:" + baseUri);
        URI uri = base.resolve(schemaLocation);
        is = uri.toURL().openStream();
        if (is == null) {
            log.error("Exception resolving entity: schemaLocation=" + schemaLocation + " baseUri=" + baseUri);
            return null;
        }
        InputSource source = new InputSource(is);
        source.setSystemId(uri.toString());
        source.setPublicId(schemaLocation);
        return new InputSource(is);
    } catch (Exception e) {
        log.error("Exception resolving entity: schemaLocation=" + schemaLocation + " baseUri=" + baseUri, e);
        return null;
    }
}

From source file:org.wyona.yanel.impl.resources.usecase.UsecaseResource.java

/**
 * Generate jelly view.//w  w w  . j a v  a 2  s  .c  o m
 */
private InputStream getJellyInputStream(String viewID, String viewTemplate, boolean XMLoutput)
        throws UsecaseException {
    try {

        if (log.isDebugEnabled())
            log.debug("viewTemplate: " + viewTemplate);
        Repository repo = this.getRealm().getRepository();

        JellyContext jellyContext = new JellyContext();
        jellyContext.setVariable("resource", this);
        jellyContext.setVariable("yanel.back2context", PathUtil.backToContext(realm, getPath()));
        jellyContext.setVariable("yanel.back2realm", PathUtil.backToRealm(getPath()));
        jellyContext.setVariable("yanel.globalHtdocsPath", PathUtil.getGlobalHtdocsPath(this));
        jellyContext.setVariable("yanel.resourcesHtdocsPath", PathUtil.getResourcesHtdocsPathURLencoded(this));
        jellyContext.setVariable("yanel.reservedPrefix", this.getYanel().getReservedPrefix());
        //jellyContext.setVariable("request", request);

        ByteArrayOutputStream jellyResultStream = new ByteArrayOutputStream();
        XMLOutput jellyOutput = XMLOutput.createXMLOutput(jellyResultStream, XMLoutput);
        InputStream templateInputStream;
        String templatePublicId;
        String templateSystemId;
        if (viewTemplate.startsWith("/")) {
            if (log.isDebugEnabled())
                log.debug(
                        "Accessing view template directly from the repo (no protocol specified). View Template: "
                                + viewTemplate);
            // for backwards compatibility. when not using a protocol
            templateInputStream = repo.getNode(viewTemplate).getInputStream();
            templatePublicId = "yanelrepo:" + viewTemplate;
            /*XXX HACK the following does not work: Jelly wants URLs => protocol must be registered by the JVM!
            templateSystemId = "yanelrepo:"+viewTemplate;
            */
            templateSystemId = "file:///yanelrepo" + viewTemplate;

        } else {
            if (log.isDebugEnabled())
                log.debug(
                        "Accessing view template through the source-resolver (protocol specified). View Template: "
                                + viewTemplate);
            SourceResolver resolver = new SourceResolver(this);
            Source templateSource = resolver.resolve(viewTemplate, null);
            templateInputStream = ((StreamSource) templateSource).getInputStream();
            templatePublicId = templateSource.getSystemId();
            /*XXX HACK the following does not work: Jelly wants URLs => protocol must be registered by the JVM!
            templateSystemId = templateSource.getSystemId();
            */
            templateSystemId = "file:///" + viewTemplate.replaceFirst(":", "/");
        }
        InputSource inputSource = new InputSource(templateInputStream);
        inputSource.setPublicId(templatePublicId);
        inputSource.setSystemId(templateSystemId);
        jellyContext.runScript(inputSource, jellyOutput);
        //XXX should we close templateInputStream here?!?

        jellyOutput.flush();
        byte[] result = jellyResultStream.toByteArray();
        //System.out.println(new String(result, "utf-8"));
        return new ByteArrayInputStream(result);
    } catch (Exception e) {
        String errorMsg = "Error creating 'jelly' view '" + viewID + "' of usecase resource: " + getName()
                + ": " + e;
        log.error(errorMsg, e);
        throw new UsecaseException(errorMsg, e);
    }
}