Example usage for javax.xml.transform Source getSystemId

List of usage examples for javax.xml.transform Source getSystemId

Introduction

In this page you can find the example usage for javax.xml.transform Source getSystemId.

Prototype

public String getSystemId();

Source Link

Document

Get the system identifier that was set with setSystemId.

Usage

From source file:Main.java

/**
 * Validates XML on a XMLschema//from ww  w .  ja  va2  s.  c  om
 * 
 * @param xmlSchema
 * @param sourceXml
 */
public static void validateXmlOnSchema(File xmlSchema, Source sourceXml) {
    Schema schema = newSchema(xmlSchema);
    try {
        schema.newValidator().validate(sourceXml);
    } catch (Exception e) {
        throw new RuntimeException(
                "Could not validate '" + sourceXml.getSystemId() + "' with '" + xmlSchema.getName() + "'!", e);
    }
}

From source file:Main.java

/**
 * Validates XML on a XMLschema//from  ww w  .j  av  a 2s.  c o  m
 * 
 * @param xmlSchema
 * @param sourceXml
 */
public static void validateXmlOnSchema(Source xmlSchema, Source sourceXml) {
    Schema schema = newSchema(xmlSchema);
    try {
        schema.newValidator().validate(sourceXml);
    } catch (Exception e) {
        throw new RuntimeException(
                "Could not validate '" + sourceXml.getSystemId() + "' with '" + xmlSchema.getSystemId() + "'!",
                e);
    }
}

From source file:Main.java

public static boolean validate(URL schemaFile, String xmlString) {
    boolean success = false;
    try {//from  w  ww .j  a  v a2  s . c o  m
        SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema schema = schemaFactory.newSchema(schemaFile);
        Validator validator = schema.newValidator();
        Source xmlSource = null;
        try {
            xmlSource = new StreamSource(new java.io.StringReader(xmlString));
            validator.validate(xmlSource);
            System.out.println("Congratulations, the document is valid");
            success = true;
        } catch (SAXException e) {
            e.printStackTrace();
            System.out.println(xmlSource.getSystemId() + " is NOT valid");
            System.out.println("Reason: " + e.getLocalizedMessage());
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return success;
}

From source file:com.meltmedia.rodimus.RodimusCli.java

public static TransformerHandler getContentHandler(Source source) throws Exception {
    try {//from w w  w. j a v a2s .  co m
        SAXTransformerFactory factory = (SAXTransformerFactory) new TransformerFactoryImpl();
        return factory.newTransformerHandler(source);
    } catch (Exception e) {
        throw new Exception("Could not load transform " + source.getSystemId(), e);
    }
}

From source file:net.sf.joost.trax.TrAXHelper.java

/**
 * Helpermethod for getting an InputSource from a StreamSource.
 * @param source <code>Source</code>
 * @return An <code>InputSource</code> object or null
 * @throws TransformerConfigurationException
 *///from   w  w w .j a  va 2 s  .c o m
protected static InputSource getInputSourceForStreamSources(Source source, ErrorListener errorListener)
        throws TransformerConfigurationException {

    if (DEBUG)
        log.debug("getting an InputSource from a StreamSource");
    InputSource input = null;
    String systemId = source.getSystemId();

    if (systemId == null) {
        systemId = "";
    }
    try {
        if (source instanceof StreamSource) {
            if (DEBUG)
                log.debug("Source is a StreamSource");
            StreamSource stream = (StreamSource) source;
            InputStream istream = stream.getInputStream();
            Reader reader = stream.getReader();
            // Create InputSource from Reader or InputStream in Source
            if (istream != null) {
                input = new InputSource(istream);
            } else {
                if (reader != null) {
                    input = new InputSource(reader);
                } else {
                    input = new InputSource(systemId);
                }
            }
        } else {
            //Source type is not supported
            if (errorListener != null) {
                try {
                    errorListener
                            .fatalError(new TransformerConfigurationException("Source is not a StreamSource"));
                    return null;
                } catch (TransformerException e2) {
                    if (DEBUG)
                        log.debug("Source is not a StreamSource");
                    throw new TransformerConfigurationException("Source is not a StreamSource");
                }
            }
            if (DEBUG)
                log.debug("Source is not a StreamSource");
            throw new TransformerConfigurationException("Source is not a StreamSource");
        }
        //setting systemId
        input.setSystemId(systemId);
        //          } catch (NullPointerException nE) {
        //              //catching NullPointerException
        //              if(errorListener != null) {
        //                  try {
        //                      errorListener.fatalError(
        //                              new TransformerConfigurationException(nE));
        //                      return null;
        //                  } catch( TransformerException e2) {
        //                      log.debug(nE);
        //                      throw new TransformerConfigurationException(nE.getMessage());
        //                  }
        //              }
        //              log.debug(nE);
        //              throw new TransformerConfigurationException(nE.getMessage());
    } catch (SecurityException sE) {
        //catching SecurityException
        if (errorListener != null) {
            try {
                errorListener.fatalError(new TransformerConfigurationException(sE));
                return null;
            } catch (TransformerException e2) {
                if (DEBUG)
                    log.debug(sE);
                throw new TransformerConfigurationException(sE.getMessage());
            }
        }
        if (DEBUG)
            log.debug(sE);
        throw new TransformerConfigurationException(sE.getMessage());
    }
    return (input);
}

From source file:Main.java

/**
 * Utility to get the bytes uri//from w  w  w.  ja v a2s .c o  m
 *
 * @param source the resource to get
 */
public static InputSource sourceToInputSource(Source source) {
    if (source instanceof SAXSource) {
        return ((SAXSource) source).getInputSource();
    } else if (source instanceof DOMSource) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        Node node = ((DOMSource) source).getNode();
        if (node instanceof Document) {
            node = ((Document) node).getDocumentElement();
        }
        Element domElement = (Element) node;
        ElementToStream(domElement, baos);
        InputSource isource = new InputSource(source.getSystemId());
        isource.setByteStream(new ByteArrayInputStream(baos.toByteArray()));
        return isource;
    } else if (source instanceof StreamSource) {
        StreamSource ss = (StreamSource) source;
        InputSource isource = new InputSource(ss.getSystemId());
        isource.setByteStream(ss.getInputStream());
        isource.setCharacterStream(ss.getReader());
        isource.setPublicId(ss.getPublicId());
        return isource;
    } else {
        return getInputSourceFromURI(source.getSystemId());
    }
}

From source file:com.spoledge.util.xslt.TemplatesCache.java

/**
 * Returns a templates cache entry for the specified system ID.
 * @return the templates found in the cache or a new one
 *//*from  www .j  a v  a2 s .  co m*/
public Templates getTemplates(Source source) throws TransformerConfigurationException {
    return getTemplates(source.getSystemId(), source);
}

From source file:org.callimachusproject.xml.XdmNodeFactory.java

private XdmNode parse(Source source) throws SAXException {
    try {//w w  w .  j ava 2s.  c o m
        DocumentBuilder builder = processor.newDocumentBuilder();
        builder.setLineNumbering(true);
        builder.setDTDValidation(false);
        if (source.getSystemId() != null) {
            builder.setBaseURI(URI.create(source.getSystemId()));
        }
        return builder.build(source);
    } catch (SaxonApiException e) {
        throw new SAXException(e);
    }
}

From source file:net.sf.joost.trax.TrAXHelper.java

/**
* Converts a supplied <code>Source</code> to a <code>SAXSource</code>.
* @param source The supplied input source
* @param errorListener an ErrorListener object
* @return a <code>SAXSource</code>
*///from w w w. java 2s  . c  o m
public static SAXSource getSAXSource(Source source, ErrorListener errorListener) throws TransformerException {

    if (DEBUG)
        log.debug("getting a SAXSource from a Source");
    //SAXSource
    if (source instanceof SAXSource) {
        if (DEBUG)
            log.debug("source is an instance of SAXSource, so simple return");
        return (SAXSource) source;
    }
    //DOMSource
    if (source instanceof DOMSource) {
        if (DEBUG)
            log.debug("source is an instance of DOMSource");
        InputSource is = new InputSource();
        Node startNode = ((DOMSource) source).getNode();
        Document doc;
        if (startNode instanceof Document) {
            doc = (Document) startNode;
        } else {
            doc = startNode.getOwnerDocument();
        }
        if (DEBUG)
            log.debug("using DOMDriver");
        DOMDriver driver = new DOMDriver();
        driver.setDocument(doc);
        is.setSystemId(source.getSystemId());
        driver.setSystemId(source.getSystemId());
        return new SAXSource(driver, is);
    }
    //StreamSource
    if (source instanceof StreamSource) {
        if (DEBUG)
            log.debug("source is an instance of StreamSource");
        InputSource isource = getInputSourceForStreamSources(source, errorListener);
        return new SAXSource(isource);
    } else {
        String errMsg = "Unknown type of source";
        if (log != null)
            log.error(errMsg);
        IllegalArgumentException iE = new IllegalArgumentException(errMsg);
        TransformerConfigurationException tE = new TransformerConfigurationException(iE.getMessage(), iE);
        if (errorListener != null)
            errorListener.error(tE);
        else
            throw tE;
        return null;
    }
}

From source file:com.enonic.vertical.adminweb.handlers.SimpleContentHandlerServlet.java

protected DOMSource buildXSL(HttpSession session, AdminService admin, int contentTypeKey)
        throws VerticalAdminException {

    DOMSource result = null;/* ww  w .ja  v a2  s  .  co  m*/
    try {
        Document sourceDoc = XMLTool.domparse(admin.getContentTypeModuleData(contentTypeKey));

        // Set whether fields are indexed or not
        Document indexDoc = XMLTool.domparse(admin.getIndexingParametersXML(contentTypeKey));
        Element[] indexingParams = XMLTool.getElements(indexDoc.getDocumentElement(), "index");

        Element browseElem = XMLTool.getElement(sourceDoc.getDocumentElement(), "browse");
        Element[] fields = XMLTool.getElements(browseElem, "field");
        for (Element field : fields) {
            String xpath = XMLTool.getElementText(XMLTool.getElement(field, "xpath"));
            boolean indexed = false;

            // Check whether this xpath is in the index doc
            if (xpath != null) {
                for (Element indexingParam : indexingParams) {
                    if ((xpath).equals(indexingParam.getAttribute("xpath"))) {
                        indexed = true;
                    }
                }
            }

            field.setAttribute("indexed", String.valueOf(indexed));
        }

        Element rootElement = sourceDoc.getDocumentElement();

        // check for xsl:
        boolean enablePreview = false;
        Element previewXSLElement = XMLTool.getElement(rootElement, "previewxsl");
        if (previewXSLElement != null) {
            String cdata = XMLTool.getElementText(previewXSLElement);
            if (cdata.length() > 0) {
                enablePreview = true;
            }
        }

        // extract module xml:
        Element moduleElement = XMLTool.getElement(rootElement, "config");
        sourceDoc = XMLTool.createDocument();
        sourceDoc.appendChild(sourceDoc.importNode(moduleElement, true));

        StringWriter swriter = new StringWriter();
        Map<String, Object> xslParams = new HashMap<String, Object>();

        xslParams.put("xsl_prefix", "");

        xslParams.put("enablepreview", String.valueOf(enablePreview));
        Source xslFile = AdminStore.getStylesheet(session, FORM_TEMPLATE);
        transformXML(session, swriter, new DOMSource(sourceDoc), xslFile, xslParams);

        result = new DOMSource(XMLTool.domparse(swriter.toString()));
        result.setSystemId(xslFile.getSystemId());
    } catch (TransformerConfigurationException e) {
        VerticalAdminLogger.errorAdmin(this.getClass(), 50, "XSLT error: %t", e);
    } catch (TransformerException e) {
        VerticalAdminLogger.errorAdmin(this.getClass(), 50, "XSLT error: %t", e);
    }

    return result;
}