List of usage examples for javax.xml.transform.stream StreamSource StreamSource
public StreamSource(File f)
From source file:org.energyos.espi.common.domain.ServiceDeliveryPointUnmarshallerTests.java
@Before public void setup() { serviceDeliveryPoint = (ServiceDeliveryPoint) domainMarshaller .unmarshal(new StreamSource(new StringReader(XML_INPUT))); }
From source file:Main.java
/** * Applies an XSLT./*from w ww.j a va 2 s .c o m*/ * * @param xml The soucrce to translate. * @param xsltFilename The filename of the xsl file to use. * @param params A map of parameters to pass to the transformation. * if null is passed none are used. * * @return The string representing of the result of the transformation. */ public static String applyXSLT(Source xml, String xsltFilename, Map params) { try { String fullPath = xsltFilename; File xslt = new File(fullPath); TransformerFactory f = TransformerFactory.newInstance(); StreamSource xsltSrc = new StreamSource(xslt); StringWriter out = new StringWriter(); StreamResult res = new StreamResult(out); Transformer t = f.newTransformer(xsltSrc); if (params != null) { Iterator i = params.keySet().iterator(); while (i.hasNext()) { Object obj = i.next(); t.setParameter(obj.toString(), params.get(obj)); } } t.transform(xml, res); return out.toString(); } catch (Exception e) { return null; } }
From source file:cl.vmetrix.operation.persistence.XmlValidator.java
/** * The method validatorXMLstg determinate if the String XML is correct. * @return a boolean (true or false) to indicate if the XML is correct. * @param xml is the String XML to validate against XSD Schema * @throws SAXException/*from w w w .j av a 2 s .co m*/ * @throws IOException */ public boolean validateXMLstg(String xml) throws SAXException, IOException { String rpta = ""; boolean validation = true; try { xml = new String(xml.getBytes("UTF-8")); // System.out.println("---> XML in UTF-8: "+ xml); // convert String into InputStream InputStream is = new ByteArrayInputStream(xml.getBytes()); Source xmlFile = new StreamSource(is);//new File("C:/XML/424437.xml")); // XSD schema String schemea = getFileSchema("operationSchema.xsd"); InputStream sch = new ByteArrayInputStream(schemea.getBytes()); Source schemaFile = new StreamSource(sch);//new File("main/resources/Operation.xsd")); // Preparing the schema SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = schemaFactory.newSchema(schemaFile); // Validator creation Validator validator = schema.newValidator(); // DException handle of validator final List<SAXParseException> exceptions = new LinkedList<SAXParseException>(); validator.setErrorHandler(new ErrorHandler() { @Override public void warning(SAXParseException exception) throws SAXException { exceptions.add(exception); } @Override public void fatalError(SAXParseException exception) throws SAXException { exceptions.add(exception); } @Override public void error(SAXParseException exception) throws SAXException { exceptions.add(exception); } }); // XML validation validator.validate(xmlFile); // Validation result. If there are errors, detailed the exact position in the XML and the error if (exceptions.size() == 0) { rpta = "XML IS VALID"; } else { validation = false; StringBuffer sb = new StringBuffer(); sb.append("XML IS INVALID"); sb.append("\n"); sb.append("NUMBER OF ERRORS: " + exceptions.size()); sb.append("\n"); for (int i = 0; i < exceptions.size(); i++) { i = i + 1; sb.append("Error # " + i + ":"); sb.append("\n"); i = i - 1; sb.append(" - Line: " + ((SAXParseException) exceptions.get(i)).getLineNumber()); sb.append("\n"); sb.append(" - Column: " + ((SAXParseException) exceptions.get(i)).getColumnNumber()); sb.append("\n"); sb.append(" - Error message: " + ((Throwable) exceptions.get(i)).getLocalizedMessage()); sb.append("\n"); sb.append("------------------------------"); } rpta = sb.toString(); logger.debug(rpta); } } catch (SAXException e) { logger.error("SAXException in XML validator: ", e); logger.debug(rpta); throw new SAXException(e); } catch (IOException e) { logger.error("IOException in XML validator: ", e); logger.debug(rpta); throw new IOException(e); } return validation; }
From source file:com.bluecloud.ioc.parse.KernelXMLParser.java
public KernelXMLParser() throws KernelXMLParserException { InputStream is = KernelXMLParser.class.getClassLoader().getResourceAsStream(SHCAME); if (null == is) { throw new KernelXMLParserException(SHCAME + "?"); }/*from www .j av a 2 s . c om*/ SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); try { Schema schema = factory.newSchema(new StreamSource(is)); validator = schema.newValidator(); is.close(); } catch (Exception e) { throw new KernelXMLParserException(e); } }
From source file:Main.java
public static synchronized Object deserialize(Source source, InputStream xsltSource, Class cls) throws TransformerConfigurationException, JAXBException, TransformerException { Object obj = null;/*from w ww.ja v a 2 s .c om*/ JAXBContext jc = JAXBContext.newInstance(cls); if (xsltSource != null) { TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer; transformer = factory.newTransformer(new StreamSource(xsltSource)); JAXBResult result = new JAXBResult(jc); transformer.transform(source, result); obj = result.getResult(); } else { obj = jc.createUnmarshaller().unmarshal(source); } return obj; }
From source file:uk.ac.bbsrc.tgac.miso.core.util.TaxonomyUtils.java
public static String checkScientificNameAtNCBI(String scientificName) { try {//from w ww. ja va2 s . c om String query = ncbiEntrezUtilsURL + "db=taxonomy&term=" + URLEncoder.encode(scientificName, "UTF-8"); final HttpClient httpclient = HttpClientBuilder.create().build(); HttpGet httpget = new HttpGet(query); try { HttpResponse response = httpclient.execute(httpget); String out = parseEntity(response.getEntity()); log.info(out); try { DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document d = docBuilder.newDocument(); TransformerFactory.newInstance().newTransformer() .transform(new StreamSource(new UnicodeReader(out)), new DOMResult(d)); NodeList nl = d.getElementsByTagName("Id"); for (int i = 0; i < nl.getLength(); i++) { Element e = (Element) nl.item(i); return e.getTextContent(); } } catch (ParserConfigurationException e) { log.error("check scientific name at NCBI", e); } catch (TransformerException e) { log.error("check scientific name at NCBI", e); } } catch (ClientProtocolException e) { log.error("check scientific name at NCBI", e); } catch (IOException e) { log.error("check scientific name at NCBI", e); } } catch (UnsupportedEncodingException e) { log.error("check scientific name at NCBI", e); } return null; }
From source file:org.energyos.espi.common.domain.BatchListUnmarshallerTest.java
@Before public void before() throws JAXBException, FeedException { batchList = (BatchList) marshaller/*from www.j a v a2 s .c om*/ .unmarshal(new StreamSource(new InputStreamReader(IOUtils.toInputStream(XML_INPUT)))); }
From source file:au.id.hazelwood.xmltvguidebuilder.config.ConfigFactory.java
public ConfigFactory() throws Exception { StopWatch stopWatch = new StopWatch(); stopWatch.start();//from w w w. ja va2 s . c om SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); schema = schemaFactory.newSchema(new StreamSource(toInputStream("/config.xsd"))); stopWatch.stop(); LOGGER.debug("ConfigFactory created in {}", formatDurationWords(stopWatch.getTime())); }
From source file:com.wantez.eregparser.Parser.java
public void parse(final String input, final String output) throws FileNotFoundException, TransformerException { this.log.info(input + " -> " + output); final StreamSource streamSource = new StreamSource(input); final StreamResult streamResult = new StreamResult(new FileOutputStream(output)); this.transformer.transform(streamSource, streamResult); }
From source file:org.energyos.espi.thirdparty.repository.impl.ResourceRESTRepositoryImpl.java
public IdentifiedObject get(Authorization authorization, String url) { HttpHeaders requestHeaders = new HttpHeaders(); requestHeaders.set("Authorization", "Bearer " + authorization.getAccessToken()); @SuppressWarnings({ "rawtypes", "unchecked" }) HttpEntity<?> requestEntity = new HttpEntity(requestHeaders); HttpEntity<String> response = template.exchange(url, HttpMethod.GET, requestEntity, String.class); return (IdentifiedObject) marshaller.unmarshal(new StreamSource(response.getBody())); }