List of usage examples for javax.xml.transform.stream StreamSource StreamSource
public StreamSource(File f)
From source file:Compile.java
/** * Compiles an XSL stylesheet into a translet, wraps the translet * inside a Templates object and dumps it to a file. *//* w w w .j a v a2 s . c o m*/ public void run(String xsl) { try { // Set XSLTC's TransformerFactory implementation as the default System.setProperty("javax.xml.transform.TransformerFactory", "org.apache.xalan.xsltc.trax.TransformerFactoryImpl"); // Get an input stream for the XSL stylesheet StreamSource stylesheet = new StreamSource(xsl); // The TransformerFactory will compile the stylesheet and // put the translet classes inside the Templates object TransformerFactory factory = TransformerFactory.newInstance(); factory.setAttribute("generate-translet", Boolean.TRUE); Templates templates = factory.newTemplates(stylesheet); } catch (Exception e) { System.err.println("Exception: " + e); e.printStackTrace(); } System.exit(0); }
From source file:gov.nih.nci.cabig.caaers2adeers.xslt.ClasspathURIResolver.java
public Source resolve(String href, String base) throws TransformerException { if (log.isDebugEnabled()) log.debug(/* w w w. ja va2 s . c om*/ "resolving (" + baseFolder + href + ") the resource [base : " + base + ", href :" + href + "]"); try { Resource resource = resourceLoader.getResource(baseFolder + href); if (resource != null && resource.exists()) return new StreamSource(resource.getInputStream()); } catch (Exception e) { log.error("Unable to load resource [base : " + base + ", href: " + href + "]", e); } if (log.isDebugEnabled()) log.debug("unable to find resource [base : " + base + ", href :" + href + "], " + "so instructing to use default lookup"); return null; //use default lookup }
From source file:Main.java
/** * Transforms a memory document with XML format into another with HTML * format according an XSL file, and stores it in a file * /*from w w w. ja v a 2 s . com*/ * @param doc * The document to read * @param htmlFile * The name of the output (HTML) file * @param xslFile * The name of the XSL file which allows transform XML into HTML * according its style * * @throws FileNotFoundException * java.io.FileNotFoundException * @throws TransformerException * javax.xml.transform.TransformerException */ public static void write_DOM_into_an_HTML_file(Document doc, String htmlFile, String xslFile) throws FileNotFoundException, TransformerException { // An instance of a object transformer factory TransformerFactory tFactory = TransformerFactory.newInstance(); // Creates the output file File SalidaHTML = new File(htmlFile); // Associates the file to a file output stream FileOutputStream os = new FileOutputStream(SalidaHTML); // Creates a transformer object associated to the XSL file Transformer transformer = tFactory.newTransformer(new StreamSource(xslFile)); // Holds a tree with the information of a document in the form of a DOM // tree DOMSource sourceId = new DOMSource(doc); // Makes the transformation from the source in XML to the output in HML // according the transformer in XSL transformer.transform(sourceId, new StreamResult(os)); }
From source file:gov.nih.nci.cabig.caaers.utils.XmlValidator.java
public static boolean validateAgainstSchema(String xmlContent, String xsdUrl, StringBuffer validationResult) { boolean validXml = false; try {/* www .j av a 2 s . c om*/ // parse an XML document into a DOM tree DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); documentBuilderFactory.setValidating(false); documentBuilderFactory.setNamespaceAware(true); DocumentBuilder parser = documentBuilderFactory.newDocumentBuilder(); Document document = parser.parse(new InputSource(new StringReader(xmlContent))); // create a SchemaFactory capable of understanding WXS schemas SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); // load a WXS schema, represented by a Schema instance Source schemaFile = new StreamSource(getResources(xsdUrl)[0].getFile()); Schema schema = schemaFactory.newSchema(schemaFile); // create a Validator instance, which can be used to validate an instance document Validator validator = schema.newValidator(); // validate the DOM tree validator.validate(new DOMSource(document)); validXml = true; } catch (FileNotFoundException ex) { throw new CaaersSystemException("File Not found Exception", ex); } catch (IOException ioe) { validationResult.append(ioe.getMessage()); logger.error(ioe.getMessage()); } catch (SAXParseException spe) { validationResult.append("Line : " + spe.getLineNumber() + " - " + spe.getMessage()); logger.error("Line : " + spe.getLineNumber() + " - " + spe.getMessage()); } catch (SAXException e) { validationResult.append(e.toString()); logger.error(e.toString()); } catch (ParserConfigurationException pce) { validationResult.append(pce.getMessage()); } return validXml; }
From source file:Main.java
/** * Transform./*from www . jav a 2s .c o m*/ * * @param xmlFile * the xml file * @param xsltFile * the xslt file * @param outputStream * the output stream * @throws TransformerFactoryConfigurationError * the transformer factory configuration error * @throws TransformerConfigurationException * the transformer configuration exception * @throws TransformerException * the transformer exception */ public static void transform(final File xmlFile, final File xsltFile, final OutputStream outputStream) throws TransformerFactoryConfigurationError, TransformerConfigurationException, TransformerException { final Source xmlSource = new StreamSource(xmlFile); final Source xsltSource = new StreamSource(xsltFile); transform(xmlSource, xsltSource, outputStream); }
From source file:ee.ria.xroad.common.conf.globalconfextension.OcspFetchIntervalSchemaValidator.java
/** * Validates the input XML as string against the schema. * @param xml the input XML as string//from w ww . j a v a 2s .com * @throws Exception if validation fails */ public static void validate(String xml) throws Exception { validate(new StreamSource(new StringReader(xml))); }
From source file:com.aol.advertising.qiao.util.XmlConfigUtil.java
/** * Validate an XML document against the given schema. * * @param xmlFileLocationUri/* w ww. j a v a 2 s. c om*/ * Location URI of the document to be validated. * @param schemaLocationUri * Location URI of the XML schema in W3C XML Schema Language. * @return true if valid, false otherwise. */ public static boolean validateXml(String xmlFileLocationUri, String schemaLocationUri) { SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); InputStream is = null; try { Schema schema = factory.newSchema(CommonUtils.uriToURL(schemaLocationUri)); Validator validator = schema.newValidator(); URL url = CommonUtils.uriToURL(xmlFileLocationUri); is = url.openStream(); Source source = new StreamSource(is); validator.validate(source); logger.info(">> successfully validated configuration file: " + xmlFileLocationUri); return true; } catch (SAXParseException e) { logger.error(e.getMessage() + " (line:" + e.getLineNumber() + ", col:" + e.getColumnNumber() + ")"); } catch (Exception e) { if (e instanceof SAXParseException) { SAXParseException e2 = (SAXParseException) e; logger.error(e2.getMessage() + "at line:" + e2.getLineNumber() + ", col:" + e2.getColumnNumber()); } else { logger.error(e.getMessage()); } } finally { IOUtils.closeQuietly(is); } return false; }
From source file:hadoopInstaller.io.XMLDocumentReader.java
public static Document parse(FileObject xmlDocument, FileObject xsdDocument) throws InstallerConfigurationParseError { try {/*from ww w .ja v a2s. c om*/ // Validate against XML Schema DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); Schema schema = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI) .newSchema(new StreamSource(xsdDocument.getContent().getInputStream())); dbf.setValidating(false); dbf.setSchema(schema); DocumentBuilder db = dbf.newDocumentBuilder(); db.setErrorHandler(new ParseErrorHandler()); return db.parse(xmlDocument.getContent().getInputStream()); } catch (ParserConfigurationException | SAXException | IOException e) { throw new InstallerConfigurationParseError(e, "XMLDocumentReader.ParseError", xmlDocument.getName()); //$NON-NLS-1$ } }
From source file:Main.java
private static OutputStream applyTransformation(Source xsltSource, Map<String, Object> xsltParameters, InputStream inputXmlStream, OutputStream outputStream) throws TransformerException { // Create a transform factory instance. TransformerFactory tfactory = TransformerFactory.newInstance(); // Create a transformer for the stylesheet. Transformer transformer = tfactory.newTransformer(xsltSource); if (xsltParameters != null) { for (String paramName : xsltParameters.keySet()) { transformer.setParameter(paramName, xsltParameters.get(paramName)); }// ww w . jav a2s. com } // Transform the source XML to outputStream. transformer.transform(new StreamSource(inputXmlStream), new StreamResult(outputStream)); return outputStream; }
From source file:com.wantez.eregparser.Parser.java
public Parser(final String xsl) throws TransformerConfigurationException { final TransformerFactory tFactory = TransformerFactory.newInstance(); final ClassLoader classLoader = this.getClass().getClassLoader(); final InputStream stream = classLoader.getResourceAsStream(xsl); final StreamSource streamSource = new StreamSource(stream); streamSource.setSystemId(classLoader.getResource(xsl).getPath()); this.transformer = tFactory.newTransformer(streamSource); }