List of usage examples for javax.xml.transform.stream StreamSource getReader
public Reader getReader()
From source file:com.jaeksoft.searchlib.Client.java
public int updateTextDocuments(StreamSource streamSource, String charset, Integer bufferSize, String capturePattern, Integer langPosition, List<String> fieldList, InfoCallback infoCallBack) throws SearchLibException, IOException, NoSuchAlgorithmException, URISyntaxException, InstantiationException, IllegalAccessException, ClassNotFoundException { if (capturePattern == null) throw new SearchLibException("No capture pattern"); if (fieldList == null || fieldList.size() == 0) throw new SearchLibException("empty field list"); String[] fields = fieldList.toArray(new String[fieldList.size()]); Matcher matcher = Pattern.compile(capturePattern).matcher(""); BufferedReader br = null;/*from ww w .j a v a 2s .co m*/ Reader reader = null; SchemaField uniqueSchemaField = getSchema().getFieldList().getUniqueField(); String uniqueField = uniqueSchemaField != null ? uniqueSchemaField.getName() : null; if (charset == null) charset = "UTF-8"; if (bufferSize == null) bufferSize = 50; try { Collection<IndexDocument> docList = new ArrayList<IndexDocument>(bufferSize); reader = streamSource.getReader(); if (reader == null) reader = new InputStreamReader(streamSource.getInputStream(), charset); br = reader instanceof BufferedReader ? (BufferedReader) reader : new BufferedReader(reader); String line; int docCount = 0; IndexDocument lastDocument = null; String lastUniqueValue = null; while ((line = br.readLine()) != null) { matcher.reset(line); if (!matcher.matches()) continue; LanguageEnum lang = LanguageEnum.UNDEFINED; int matcherGroupCount = matcher.groupCount(); if (langPosition != null && matcherGroupCount >= langPosition) lang = LanguageEnum.findByNameOrCode(matcher.group(langPosition)); IndexDocument document = new IndexDocument(lang); int i = matcherGroupCount < fields.length ? matcherGroupCount : fields.length; String uniqueValue = null; while (i > 0) { String value = matcher.group(i--); String f = fields[i]; document.add(f, value, 1.0F); if (f.equals(uniqueField)) uniqueValue = value; } // Consecutive documents with same uniqueKey value are merged // (multivalued) if (uniqueField != null && lastDocument != null && uniqueValue != null && uniqueValue.equals(lastUniqueValue)) { lastDocument.addIfNotAlreadyHere(document); continue; } docList.add(document); if (docList.size() == bufferSize) docCount = updateDocList(0, docCount, docList, infoCallBack); lastUniqueValue = uniqueValue; lastDocument = document; } if (docList.size() > 0) docCount = updateDocList(0, docCount, docList, infoCallBack); return docCount; } finally { if (br != null) if (br != reader) IOUtils.close(br); } }
From source file:nl.nn.adapterframework.validation.xerces_2_11.XMLSchemaFactory.java
public Schema newSchema(Source[] schemas) throws SAXException { // this will let the loader store parsed Grammars into the pool. XMLGrammarPoolImplExtension pool = new XMLGrammarPoolImplExtension(); fXMLGrammarPoolWrapper.setGrammarPool(pool); XMLInputSource[] xmlInputSources = new XMLInputSource[schemas.length]; InputStream inputStream;// ww w. jav a 2 s .co m Reader reader; for (int i = 0; i < schemas.length; ++i) { Source source = schemas[i]; if (source instanceof StreamSource) { StreamSource streamSource = (StreamSource) source; String publicId = streamSource.getPublicId(); String systemId = streamSource.getSystemId(); inputStream = streamSource.getInputStream(); reader = streamSource.getReader(); XMLInputSource xmlInputSource = new XMLInputSource(publicId, systemId, null); xmlInputSource.setByteStream(inputStream); xmlInputSource.setCharacterStream(reader); xmlInputSources[i] = xmlInputSource; } else if (source instanceof SAXSource) { SAXSource saxSource = (SAXSource) source; InputSource inputSource = saxSource.getInputSource(); if (inputSource == null) { throw new SAXException(JAXPValidationMessageFormatter .formatMessage(fXMLSchemaLoader.getLocale(), "SAXSourceNullInputSource", null)); } xmlInputSources[i] = new SAXInputSource(saxSource.getXMLReader(), inputSource); } else if (source instanceof DOMSource) { DOMSource domSource = (DOMSource) source; Node node = domSource.getNode(); String systemID = domSource.getSystemId(); xmlInputSources[i] = new DOMInputSource(node, systemID); } // else if (source instanceof StAXSource) { // StAXSource staxSource = (StAXSource) source; // XMLEventReader eventReader = staxSource.getXMLEventReader(); // if (eventReader != null) { // xmlInputSources[i] = new StAXInputSource(eventReader); // } // else { // xmlInputSources[i] = new StAXInputSource(staxSource.getXMLStreamReader()); // } // } else if (source == null) { throw new NullPointerException(JAXPValidationMessageFormatter .formatMessage(fXMLSchemaLoader.getLocale(), "SchemaSourceArrayMemberNull", null)); } else { throw new IllegalArgumentException( JAXPValidationMessageFormatter.formatMessage(fXMLSchemaLoader.getLocale(), "SchemaFactorySourceUnrecognized", new Object[] { source.getClass().getName() })); } } try { fXMLSchemaLoader.loadGrammar(xmlInputSources); } catch (XNIException e) { // this should have been reported to users already. throw Util.toSAXException(e); } catch (IOException e) { // this hasn't been reported, so do so now. SAXParseException se = new SAXParseException(e.getMessage(), null, e); if (fErrorHandler != null) { fErrorHandler.error(se); } throw se; // and we must throw it. } // Clear reference to grammar pool. fXMLGrammarPoolWrapper.setGrammarPool(null); // Select Schema implementation based on grammar count. final int grammarCount = pool.getGrammarCount(); AbstractXMLSchema schema = null; if (fUseGrammarPoolOnly) { throw new NotImplementedException("fUseGrammarPoolOnly"); // if (grammarCount > 1) { // schema = new XMLSchemaHack(new ReadOnlyGrammarPool(pool)); // } // else if (grammarCount == 1) { // Grammar[] grammars = pool.retrieveInitialGrammarSet(XMLGrammarDescription.XML_SCHEMA); // schema = new XMLSchemaHack(grammars[0]); // } // else { // schema = new XMLSchemaHack(); // } } else { schema = new XMLSchema(new ReadOnlyGrammarPool(pool), false); } propagateFeatures(schema); return schema; }
From source file:org.apache.fop.afp.util.DefaultFOPResourceAccessor.java
/** {@inheritDoc} */ public InputStream createInputStream(URI uri) throws IOException { //Step 1: resolve against local base URI --> URI URI resolved = resolveAgainstBase(uri); //Step 2: resolve against the user agent --> stream String base = (this.categoryBaseURI != null ? this.categoryBaseURI : this.userAgent.getBaseURL()); Source src = userAgent.resolveURI(resolved.toASCIIString(), base); if (src == null) { throw new FileNotFoundException("Resource not found: " + uri.toASCIIString()); } else if (src instanceof StreamSource) { StreamSource ss = (StreamSource) src; InputStream in = ss.getInputStream(); if (in != null) { return in; }/* w w w . java 2 s . co m*/ if (ss.getReader() != null) { //Don't support reader, retry using system ID below IOUtils.closeQuietly(ss.getReader()); } } URL url = new URL(src.getSystemId()); return url.openStream(); }
From source file:org.apache.fop.fonts.FontInfoConfigurator.java
private static void closeSource(Source src) { if (src instanceof StreamSource) { StreamSource streamSource = (StreamSource) src; IOUtils.closeQuietly(streamSource.getInputStream()); IOUtils.closeQuietly(streamSource.getReader()); }/*www . ja va 2s.c om*/ }
From source file:org.apache.ode.utils.DOMUtils.java
/** * Parse an XML document located using an {@link InputSource} using the * pooled document builder./* w ww.java 2 s . com*/ */ public static Document sourceToDOM(Source inputSource) throws IOException { try { /* // Requires JDK 1.6+ if (inputSource instanceof StAXSource) { StAXSource stax = (StAXSource) inputSource; //if (stax.getXMLEventReader() != null || sax.getXMLStreamReader() != null) { if (sax.getXMLStreamReader() != null) { return parse(stax.getXMLStreamReader()); } } */ if (inputSource instanceof SAXSource) { InputSource sax = ((SAXSource) inputSource).getInputSource(); if (sax.getCharacterStream() != null || sax.getByteStream() != null) { return parse(((SAXSource) inputSource).getInputSource()); } } if (inputSource instanceof DOMSource) { Node node = ((DOMSource) inputSource).getNode(); if (node != null) { return toDOMDocument(node); } } if (inputSource instanceof StreamSource) { StreamSource stream = (StreamSource) inputSource; if (stream.getReader() != null || stream.getInputStream() != null) { return toDocumentFromStream((StreamSource) inputSource); } } DOMResult domresult = new DOMResult(newDocument()); Transformer txer = getTransformer(); txer.transform(inputSource, domresult); return (Document) domresult.getNode(); } catch (SAXException e) { throwIOException(e); } catch (TransformerException e) { throwIOException(e); } throw new IllegalArgumentException("Cannot parse XML source: " + inputSource.getClass()); }
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.ja 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.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 . j a va2s.co m*/ * @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.io.XmlSourceUtil.java
/** * Closes the InputStreams or ImageInputStreams of Source objects. Any exception occurring * while closing the stream is ignored./*from w w w . j a v a2 s .c o m*/ * @param src the Source object */ public static void closeQuietly(Source src) { if (src instanceof StreamSource) { StreamSource streamSource = (StreamSource) src; IOUtils.closeQuietly(streamSource.getReader()); } else if (src instanceof ImageSource) { if (ImageUtil.getImageInputStream(src) != null) { try { ImageUtil.getImageInputStream(src).close(); } catch (IOException ioe) { // ignore } } } else if (src instanceof SAXSource) { InputSource is = ((SAXSource) src).getInputSource(); if (is != null) { IOUtils.closeQuietly(is.getByteStream()); IOUtils.closeQuietly(is.getCharacterStream()); } } removeStreams(src); }
From source file:org.apache.xmlgraphics.util.uri.DataURIResolverTestCase.java
static final void actualURLHAndlingTest(URIResolver resolver) throws Exception { Source src;//from w w w.jav a 2 s. c om 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); }/* w w w .j a va 2 s . c o m*/ 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(); }