List of usage examples for javax.xml.transform.stream StreamSource getInputStream
public InputStream getInputStream()
From source file:org.apache.ode.utils.DOMUtils.java
public static Document toDocumentFromStream(StreamSource source) throws IOException, SAXException { DocumentBuilder builder = getBuilder(); Document document = null;/*from ww w . j a v a 2 s . c om*/ Reader reader = source.getReader(); if (reader != null) { document = builder.parse(new InputSource(reader)); } else { InputStream inputStream = source.getInputStream(); if (inputStream != null) { InputSource inputsource = new InputSource(inputStream); inputsource.setSystemId(source.getSystemId()); document = builder.parse(inputsource); } else { throw new IOException("No input stream or reader available"); } } return document; }
From source file:org.apache.solr.handler.dataimport.processor.XPathEntityProcessor.java
private void initXpathReader() { useSolrAddXml = Boolean.parseBoolean(context.getEntityAttribute(USE_SOLR_ADD_SCHEMA)); streamRows = Boolean.parseBoolean(context.getEntityAttribute(STREAM)); if (context.getResolvedEntityAttribute("batchSize") != null) { blockingQueueSize = Integer.parseInt(context.getEntityAttribute("batchSize")); }//from w ww .j a va2 s . c om if (context.getResolvedEntityAttribute("readTimeOut") != null) { blockingQueueTimeOut = Integer.parseInt(context.getEntityAttribute("readTimeOut")); } String xslt = context.getEntityAttribute(XSL); if (xslt != null) { xslt = context.replaceTokens(xslt); try { // create an instance of TransformerFactory final TransformerFactory transFact = TransformerFactory.newInstance(); final SolrCore core = context.getSolrCore(); final StreamSource xsltSource; if (core != null) { // Solr cut off //final ResourceLoader loader = core.getResourceLoader(); //transFact.setURIResolver(new SystemIdResolver(loader).asURIResolver()); //xsltSource = new StreamSource(loader.openResource(xslt), // SystemIdResolver.createSystemIdFromResourceName(xslt)); xsltSource = new StreamSource(xslt); } else { // fallback for tests xsltSource = new StreamSource(xslt); } transFact.setErrorListener(xmllog); try { xslTransformer = transFact.newTransformer(xsltSource); } finally { // some XML parsers are broken and don't close the byte stream (but they should according to spec) IOUtils.closeQuietly(xsltSource.getInputStream()); } LOG.info("Using xslTransformer: " + xslTransformer.getClass().getName()); } catch (final Exception e) { throw new DataImportHandlerException(SEVERE, "Error initializing XSL ", e); } } if (useSolrAddXml) { // Support solr add documents xpathReader = new XPathRecordReader("/add/doc"); xpathReader.addField("name", "/add/doc/field/@name", true); xpathReader.addField("value", "/add/doc/field", true); } else { final String forEachXpath = context.getEntityAttribute(FOR_EACH); if (forEachXpath == null) throw new DataImportHandlerException(SEVERE, "Entity : " + context.getEntityAttribute("name") + " must have a 'forEach' attribute"); try { xpathReader = new XPathRecordReader(forEachXpath); for (final Map<String, String> field : context.getAllEntityFields()) { if (field.get(XPATH) == null) continue; int flags = 0; if ("true".equals(field.get("flatten"))) { flags = XPathRecordReader.FLATTEN; } String xpath = field.get(XPATH); xpath = context.replaceTokens(xpath); xpathReader.addField(field.get(DataImporter.COLUMN), xpath, Boolean.parseBoolean(field.get(DataImporter.MULTI_VALUED)), flags); } } catch (final RuntimeException e) { throw new DataImportHandlerException(SEVERE, "Exception while reading xpaths for fields", e); } } final String url = context.getEntityAttribute(URL); final List<String> l = url == null ? Collections.EMPTY_LIST : TemplateString.getVariables(url); for (final String s : l) { if (s.startsWith(entityName + ".")) { if (placeHolderVariables == null) placeHolderVariables = new ArrayList<String>(); placeHolderVariables.add(s.substring(entityName.length() + 1)); } } for (final Map<String, String> fld : context.getAllEntityFields()) { if (fld.get(COMMON_FIELD) != null && "true".equals(fld.get(COMMON_FIELD))) { if (commonFields == null) commonFields = new ArrayList<String>(); commonFields.add(fld.get(DataImporter.COLUMN)); } } }
From source file:org.apache.solr.handler.dataimport.XPathEntityProcessor.java
private void initXpathReader(VariableResolver resolver) { reinitXPathReader = false;/* w w w. ja va 2 s .c o m*/ useSolrAddXml = Boolean.parseBoolean(context.getEntityAttribute(USE_SOLR_ADD_SCHEMA)); streamRows = Boolean.parseBoolean(context.getEntityAttribute(STREAM)); if (context.getResolvedEntityAttribute("batchSize") != null) { blockingQueueSize = Integer.parseInt(context.getEntityAttribute("batchSize")); } if (context.getResolvedEntityAttribute("readTimeOut") != null) { blockingQueueTimeOut = Integer.parseInt(context.getEntityAttribute("readTimeOut")); } String xslt = context.getEntityAttribute(XSL); if (xslt != null) { xslt = context.replaceTokens(xslt); try { // create an instance of TransformerFactory TransformerFactory transFact = TransformerFactory.newInstance(); final SolrCore core = context.getSolrCore(); final StreamSource xsltSource; if (core != null) { final ResourceLoader loader = core.getResourceLoader(); transFact.setURIResolver(new SystemIdResolver(loader).asURIResolver()); xsltSource = new StreamSource(loader.openResource(xslt), SystemIdResolver.createSystemIdFromResourceName(xslt)); } else { // fallback for tests xsltSource = new StreamSource(xslt); } transFact.setErrorListener(xmllog); try { xslTransformer = transFact.newTransformer(xsltSource); } finally { // some XML parsers are broken and don't close the byte stream (but they should according to spec) IOUtils.closeQuietly(xsltSource.getInputStream()); } LOG.info("Using xslTransformer: " + xslTransformer.getClass().getName()); } catch (Exception e) { throw new DataImportHandlerException(SEVERE, "Error initializing XSL ", e); } } if (useSolrAddXml) { // Support solr add documents xpathReader = new XPathRecordReader("/add/doc"); xpathReader.addField("name", "/add/doc/field/@name", true); xpathReader.addField("value", "/add/doc/field", true); } else { String forEachXpath = context.getResolvedEntityAttribute(FOR_EACH); if (forEachXpath == null) throw new DataImportHandlerException(SEVERE, "Entity : " + context.getEntityAttribute("name") + " must have a 'forEach' attribute"); if (forEachXpath.equals(context.getEntityAttribute(FOR_EACH))) reinitXPathReader = true; try { xpathReader = new XPathRecordReader(forEachXpath); for (Map<String, String> field : context.getAllEntityFields()) { if (field.get(XPATH) == null) continue; int flags = 0; if ("true".equals(field.get("flatten"))) { flags = XPathRecordReader.FLATTEN; } String xpath = field.get(XPATH); xpath = context.replaceTokens(xpath); //!xpath.equals(field.get(XPATH) means the field xpath has a template //in that case ensure that the XPathRecordReader is reinitialized //for each xml if (!xpath.equals(field.get(XPATH)) && !context.isRootEntity()) reinitXPathReader = true; xpathReader.addField(field.get(DataImporter.COLUMN), xpath, Boolean.parseBoolean(field.get(DataImporter.MULTI_VALUED)), flags); } } catch (RuntimeException e) { throw new DataImportHandlerException(SEVERE, "Exception while reading xpaths for fields", e); } } String url = context.getEntityAttribute(URL); List<String> l = url == null ? Collections.EMPTY_LIST : resolver.getVariables(url); for (String s : l) { if (s.startsWith(entityName + ".")) { if (placeHolderVariables == null) placeHolderVariables = new ArrayList<>(); placeHolderVariables.add(s.substring(entityName.length() + 1)); } } for (Map<String, String> fld : context.getAllEntityFields()) { if (fld.get(COMMON_FIELD) != null && "true".equals(fld.get(COMMON_FIELD))) { if (commonFields == null) commonFields = new ArrayList<>(); commonFields.add(fld.get(DataImporter.COLUMN)); } } }
From source file:org.apache.solr.util.xslt.TransformerProvider.java
/** Return a Templates object for the given filename */ private Templates getTemplates(ResourceLoader loader, String filename, int cacheLifetimeSeconds) throws IOException { Templates result = null;// ww w.j a v a 2 s . co m lastFilename = null; try { if (log.isDebugEnabled()) { log.debug("compiling XSLT templates:" + filename); } final String fn = "xslt/" + filename; final TransformerFactory tFactory = TransformerFactory.newInstance(); tFactory.setURIResolver(new SystemIdResolver(loader).asURIResolver()); tFactory.setErrorListener(xmllog); final StreamSource src = new StreamSource(loader.openResource(fn), SystemIdResolver.createSystemIdFromResourceName(fn)); try { result = tFactory.newTemplates(src); } finally { // some XML parsers are broken and don't close the byte stream (but they should according to spec) IOUtils.closeQuietly(src.getInputStream()); } } catch (Exception e) { log.error(getClass().getName(), "newTemplates", e); final IOException ioe = new IOException("Unable to initialize Templates '" + filename + "'"); ioe.initCause(e); throw ioe; } lastFilename = filename; lastTemplates = result; cacheExpires = System.currentTimeMillis() + (cacheLifetimeSeconds * 1000); return result; }
From source file:org.apache.xmlgraphics.image.loader.util.ImageUtil.java
/** * Closes the InputStreams or ImageInputStreams of Source objects. Any exception occurring * while closing the stream is ignored./*from w w w. ja v a 2 s .com*/ * @param src the Source object */ public static void closeQuietly(Source src) { if (src == null) { return; } else if (src instanceof StreamSource) { StreamSource streamSource = (StreamSource) src; IOUtils.closeQuietly(streamSource.getInputStream()); streamSource.setInputStream(null); IOUtils.closeQuietly(streamSource.getReader()); streamSource.setReader(null); } else if (src instanceof ImageSource) { ImageSource imageSource = (ImageSource) src; if (imageSource.getImageInputStream() != null) { try { imageSource.getImageInputStream().close(); } catch (IOException ioe) { //ignore } imageSource.setImageInputStream(null); } } else if (src instanceof SAXSource) { InputSource is = ((SAXSource) src).getInputSource(); if (is != null) { IOUtils.closeQuietly(is.getByteStream()); is.setByteStream(null); IOUtils.closeQuietly(is.getCharacterStream()); is.setCharacterStream(null); } } }
From source file:org.apache.xmlgraphics.util.uri.DataURIResolverTestCase.java
static final void actualURLHAndlingTest(URIResolver resolver) throws Exception { Source src;//from w ww.j a va2 s. co m src = resolver.resolve("data:;base64,AAECAwQF", null); assertNotNull(src); StreamSource streamSource = (StreamSource) src; byte[] data = IOUtils.toByteArray(streamSource.getInputStream()); assertTrue("Decoded data doesn't match the test data", byteCmp(TESTDATA, 0, data)); src = resolver.resolve("data:application/octet-stream;interpreter=fop;base64,AAECAwQF", null); assertNotNull(src); streamSource = (StreamSource) src; assertNotNull(streamSource.getInputStream()); assertNull(streamSource.getReader()); data = IOUtils.toByteArray(streamSource.getInputStream()); assertTrue("Decoded data doesn't match the test data", byteCmp(TESTDATA, 0, data)); src = resolver.resolve("data:,FOP", null); assertNotNull(src); streamSource = (StreamSource) src; assertNull(streamSource.getInputStream()); assertNotNull(streamSource.getReader()); String text = IOUtils.toString(streamSource.getReader()); assertEquals("FOP", text); src = resolver.resolve("data:,A%20brief%20note", null); assertNotNull(src); streamSource = (StreamSource) src; text = IOUtils.toString(streamSource.getReader()); assertEquals("A brief note", text); src = resolver.resolve("data:text/plain;charset=iso-8859-7,%be%f9%be", null); assertNotNull(src); streamSource = (StreamSource) src; text = IOUtils.toString(streamSource.getReader()); assertEquals("\u038e\u03c9\u038e", text); }
From source file:org.dhatim.delivery.AbstractParser.java
protected static Reader getReader(Source source, String contentEncoding) { if (source != null) { if (source instanceof StreamSource) { StreamSource streamSource = (StreamSource) source; if (streamSource.getReader() != null) { return streamSource.getReader(); } else if (streamSource.getInputStream() != null) { return streamToReader(streamSource.getInputStream(), contentEncoding); } else if (streamSource.getSystemId() != null) { return systemIdToReader(streamSource.getSystemId(), contentEncoding); }//from w w w. j ava 2 s . c om throw new SmooksException("Invalid " + StreamSource.class.getName() + ". No InputStream, Reader or SystemId instance."); } else if (source.getSystemId() != null) { return systemIdToReader(source.getSystemId(), contentEncoding); } } return new NullReader(); }
From source file:org.dhatim.delivery.AbstractParser.java
protected InputStream getInputStream(StreamSource streamSource) { InputStream inputStream = streamSource.getInputStream(); String systemId = streamSource.getSystemId(); if (inputStream != null) { return inputStream; } else if (systemId != null) { return systemIdToStream(systemId); }/* www . ja v a 2 s .co m*/ return null; }
From source file:org.dhatim.delivery.Filter.java
protected Reader getReader(Source source, ExecutionContext executionContext) { if (source instanceof StreamSource) { StreamSource streamSource = (StreamSource) source; if (streamSource.getReader() != null) { return streamSource.getReader(); } else if (streamSource.getInputStream() != null) { try { if (executionContext instanceof ExecutionContext) { return new InputStreamReader(streamSource.getInputStream(), executionContext.getContentEncoding()); } else { return new InputStreamReader(streamSource.getInputStream(), "UTF-8"); }/*from w w w . j av a 2 s .co m*/ } catch (UnsupportedEncodingException e) { throw new SmooksException("Unable to decode input stream.", e); } } else { throw new SmooksException( "Invalid " + StreamSource.class.getName() + ". No InputStream or Reader instance."); } } return new NullReader(); }
From source file:org.dhatim.delivery.Filter.java
protected void close(Source source) { if (source instanceof StreamSource) { StreamSource streamSource = (StreamSource) source; try {/*from www. j a va2 s . c o m*/ if (streamSource.getReader() != null) { streamSource.getReader().close(); } else if (streamSource.getInputStream() != null) { InputStream inputStream = streamSource.getInputStream(); if (inputStream != System.in) { inputStream.close(); } } } catch (Throwable throwable) { logger.debug("Failed to close input stream/reader.", throwable); } } }