List of usage examples for javax.xml.stream XMLEventReader close
public void close() throws XMLStreamException;
From source file:com.msopentech.odatajclient.testservice.utils.XMLUtilities.java
public static Map.Entry<String, List<String>> extractLinkURIs(final InputStream is) throws Exception { final ByteArrayOutputStream bos = new ByteArrayOutputStream(); IOUtils.copy(is, bos);/* w ww .j a va 2 s . c om*/ IOUtils.closeQuietly(is); XMLEventReader reader = getEventReader(new ByteArrayInputStream(bos.toByteArray())); final List<String> links = new ArrayList<String>(); try { while (true) { links.add(IOUtils.toString(getAtomElement(reader, null, "uri").getContent())); } } catch (Exception ignore) { // End document reached ... } reader.close(); String next; reader = getEventReader(new ByteArrayInputStream(bos.toByteArray())); try { next = IOUtils.toString(getAtomElement(reader, null, "next").getContent()); } catch (Exception ignore) { // next link is not mandatory next = null; } reader.close(); return new AbstractMap.SimpleEntry<String, List<String>>(next, links); }
From source file:WpRDFFunctionLibrary.java
public static void mergeGpmltoSingleFile(String gpmlLocation) throws IOException, XMLStreamException, ParserConfigurationException, SAXException, TransformerException { // Based on: http://stackoverflow.com/questions/10759775/how-to-merge-1000-xml-files-into-one-in-java //for (int i = 1; i < 8 ; i++) { Writer outputWriter = new FileWriter("/tmp/WpGPML.xml"); XMLOutputFactory xmlOutFactory = XMLOutputFactory.newFactory(); XMLEventWriter xmlEventWriter = xmlOutFactory.createXMLEventWriter(outputWriter); XMLEventFactory xmlEventFactory = XMLEventFactory.newFactory(); xmlEventWriter.add(xmlEventFactory.createStartDocument("ISO-8859-1", "1.0")); xmlEventWriter.add(xmlEventFactory.createStartElement("", null, "PathwaySet")); xmlEventWriter.add(xmlEventFactory.createAttribute("creationData", basicCalls.now())); XMLInputFactory xmlInFactory = XMLInputFactory.newFactory(); File dir = new File(gpmlLocation); File[] rootFiles = dir.listFiles(); //the section below is only in case of analysis sets for (File rootFile : rootFiles) { String fileName = FilenameUtils.removeExtension(rootFile.getName()); System.out.println(fileName); String[] identifiers = fileName.split("_"); System.out.println(fileName); String wpIdentifier = identifiers[identifiers.length - 2]; String wpRevision = identifiers[identifiers.length - 1]; //Pattern pattern = Pattern.compile("_(WP[0-9]+)_([0-9]+).gpml"); //Matcher matcher = pattern.matcher(fileName); //System.out.println(matcher.find()); //String wpIdentifier = matcher.group(1); File tempFile = new File(constants.localAllGPMLCacheDir() + wpIdentifier + "_" + wpRevision + ".gpml"); //System.out.println(matcher.group(1)); //String wpRevision = matcher.group(2); //System.out.println(matcher.group(2)); if (!(tempFile.exists())) { System.out.println(tempFile.getName()); Document currentGPML = basicCalls.openXmlFile(rootFile.getPath()); basicCalls.saveDOMasXML(WpRDFFunctionLibrary.addWpProvenance(currentGPML, wpIdentifier, wpRevision), constants.localCurrentGPMLCache() + tempFile.getName()); }//from ww w . j a v a2 s. co m } dir = new File("/tmp/GPML"); rootFiles = dir.listFiles(); for (File rootFile : rootFiles) { System.out.println(rootFile); XMLEventReader xmlEventReader = xmlInFactory.createXMLEventReader(new StreamSource(rootFile)); XMLEvent event = xmlEventReader.nextEvent(); // Skip ahead in the input to the opening document element try { while (event.getEventType() != XMLEvent.START_ELEMENT) { event = xmlEventReader.nextEvent(); } do { xmlEventWriter.add(event); event = xmlEventReader.nextEvent(); } while (event.getEventType() != XMLEvent.END_DOCUMENT); xmlEventReader.close(); } catch (Exception e) { System.out.println("Malformed gpml file"); } } xmlEventWriter.add(xmlEventFactory.createEndElement("", null, "PathwaySet")); xmlEventWriter.add(xmlEventFactory.createEndDocument()); xmlEventWriter.close(); outputWriter.close(); }
From source file:HTTPChunkLocator.java
@Override public ChunkLocation[] getChunkLocations(final File file, final String virtualFsName) throws ChunkStorageException { final long inode = getInode(file); final long fileSizeInBytes = getFileSizeInBytes(file); final XMLInputFactory factory = XMLInputFactory.newInstance(); XMLEventReader reader = null; try {/* w ww. ja va 2 s.c o m*/ final URI uri = new URI(endpoint.getScheme(), endpoint.getUserInfo(), endpoint.getHost(), endpoint.getPort(), endpoint.getPath(), "inum=" + inode, null); reader = factory.createXMLEventReader(openStream(uri)); final List<ChunkLocation> parseChunks = parseChunks(reader, fileSizeInBytes); return parseChunks.toArray(new ChunkLocation[0]); } catch (final Exception e) { throw new ChunkStorageException(e); } finally { if (reader != null) { try { reader.close(); } catch (final XMLStreamException e) { // ignore } } } }
From source file:com.vistatec.ocelot.xliff.okapi.OkapiXLIFFFactory.java
@Override public XLIFFVersion detectXLIFFVersion(File detectVersion) throws IOException, XMLStreamException { try (BOMInputStream bomInputStream = new BOMInputStream(new FileInputStream(detectVersion), ByteOrderMark.UTF_8, ByteOrderMark.UTF_16BE, ByteOrderMark.UTF_16LE, ByteOrderMark.UTF_32BE, ByteOrderMark.UTF_32LE)) {// w w w .java2 s . c o m String bom = "UTF-8"; if (bomInputStream.hasBOM()) { bom = bomInputStream.getBOMCharsetName(); } XMLInputFactory xml = XMLInputFactory.newInstance(); XMLEventReader reader = xml.createXMLEventReader(bomInputStream, bom); while (reader.hasNext()) { XMLEvent event = reader.nextEvent(); switch (event.getEventType()) { case XMLEvent.START_ELEMENT: StartElement startElement = (StartElement) event; String localPart = startElement.getName().getLocalPart(); if (localPart.equals("xliff")) { @SuppressWarnings("unchecked") Iterator<Attribute> attrs = startElement.getAttributes(); while (attrs.hasNext()) { Attribute attr = attrs.next(); if (isXliffVersionAttributeName(attr.getName())) { String value = attr.getValue(); reader.close(); if ("2.0".equals(value)) { return XLIFFVersion.XLIFF20; } else { return XLIFFVersion.XLIFF12; } } } } break; default: break; } } throw new IllegalStateException("Could not detect XLIFF version"); } }
From source file:cn.ctyun.amazonaws.http.StaxResponseHandler.java
/** * @see cn.ctyun.amazonaws.http.HttpResponseHandler#handle(cn.ctyun.amazonaws.http.HttpResponse) *//*from w w w. ja va 2 s.co m*/ public AmazonWebServiceResponse<T> handle(HttpResponse response) throws Exception { log.trace("Parsing service response XML"); InputStream content = response.getContent(); if (content == null) content = new ByteArrayInputStream("<eof/>".getBytes()); XMLEventReader eventReader = xmlInputFactory.createXMLEventReader(content); try { AmazonWebServiceResponse<T> awsResponse = new AmazonWebServiceResponse<T>(); StaxUnmarshallerContext unmarshallerContext = new StaxUnmarshallerContext(eventReader, response.getHeaders()); unmarshallerContext.registerMetadataExpression("ResponseMetadata/RequestId", 2, ResponseMetadata.AWS_REQUEST_ID); unmarshallerContext.registerMetadataExpression("requestId", 2, ResponseMetadata.AWS_REQUEST_ID); registerAdditionalMetadataExpressions(unmarshallerContext); T result = responseUnmarshaller.unmarshall(unmarshallerContext); awsResponse.setResult(result); Map<String, String> metadata = unmarshallerContext.getMetadata(); awsResponse.setResponseMetadata(new ResponseMetadata(metadata)); log.trace("Done parsing service response"); return awsResponse; } finally { try { eventReader.close(); } catch (Exception e) { } } }
From source file:com.msopentech.odatajclient.testservice.utils.XMLUtilities.java
public String getEdmTypeFromXML(final InputStream is, final String[] path) throws Exception { final XMLEventReader reader = getEventReader(is); final Attribute type = getPropertyStartTag(reader, path).getAttributeByName(new QName(TYPE)); reader.close(); if (type == null) { throw new NotFoundException(); }// w ww .j a va 2 s . c om return type.getValue(); }
From source file:sapience.injectors.stax.inject.ModelBasedStaxStreamInjector.java
/** * If the reference is more then a simple attribute, we have to add new XML (subtree) to the stream. We transform * the reference into an InputStream and invoke another SAX parsing process for it. But the parsed events are added * to the main XMLEventWriter. // w ww . j a v a2s .co m * * @param w * @param string * @throws XMLStreamException * @throws XMLStreamException */ private void createEventsForElement(XMLEventWriter w, Reference ref) throws XMLStreamException { XMLEventReader r = null; try { StringBuilder target = new StringBuilder(ref.getTarget().toString()); NamespaceContext c = w.getNamespaceContext(); ByteArrayInputStream bais = new ByteArrayInputStream(target.toString().getBytes()); getXMLInputFactory().setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, false); r = getXMLInputFactory().createXMLEventReader(bais); while (r.hasNext()) { XMLEvent e = r.nextEvent(); switch (e.getEventType()) { case XMLEvent.START_DOCUMENT: break; case XMLEvent.END_DOCUMENT: break; default: w.add(e); break; } } } finally { ; if (r != null) r.close(); } }
From source file:com.msopentech.odatajclient.testservice.utils.XMLUtilities.java
public InputStream getAtomProperty(final InputStream is, final String[] path) throws Exception { final XMLEventReader reader = getEventReader(is); final XmlElement props = getAtomElement(reader, null, PROPERTIES); final XMLEventReader propsReader = props.getContentReader(); reader.close(); final InputStream propertyStream = writeFromStartToEndElement(getPropertyStartTag(propsReader, path), propsReader, true);//from www .j a v a2s. co m if (propertyStream == null) { throw new NotFoundException(); } return propertyStream; }
From source file:com.google.code.activetemplates.impl.TemplateCompilerImpl.java
@Override public void compile(final Template t, final TemplateModel model, XmlResult out) throws TemplateCompileException { final XmlSource s = t.createSource(); XMLEventReader r = null; XMLEventWriter w = null;/*w w w . j a v a 2s .c o m*/ try { r = inFactory.createXMLEventReader(s.getSource()); w = outFactory.createXMLEventWriter(out.getResult()); final XMLStreamException[] xe = new XMLStreamException[1]; final XMLEventReader fr = r; final XMLEventWriter fw = w; StandardEvaluationContext eContext = new StandardEvaluationContext(model); eContext.addPropertyAccessor(new TemplateModelPropertyAccessor()); CompileContext ctx = new CompileContext(fr, fw, eFactory, eComponentFactory, expressionParser, eContext); doCompile(t.getName(), ctx); if (xe[0] != null) { throw xe[0]; } } catch (XMLStreamException e) { throw new TemplateCompileException(e); } finally { s.close(); if (r != null) try { r.close(); } catch (XMLStreamException e) { } if (w != null) try { w.close(); } catch (XMLStreamException e) { } } }
From source file:com.msopentech.odatajclient.testservice.utils.XMLUtilities.java
@Override public Map<String, InputStream> getChanges(final InputStream src) throws Exception { final Map<String, InputStream> res = new HashMap<String, InputStream>(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); IOUtils.copy(src, bos);/*from ww w . j a va 2 s .c om*/ IOUtils.closeQuietly(src); // retrieve properties ... XMLEventReader reader = getEventReader(new ByteArrayInputStream(bos.toByteArray())); final Map.Entry<Integer, XmlElement> propertyElement = getAtomElement(reader, null, PROPERTIES, null, 0, 2, 3, false); reader.close(); reader = propertyElement.getValue().getContentReader(); try { while (true) { final XmlElement property = getAtomElement(reader, null, null); res.put(property.getStart().getName().getLocalPart(), property.toStream()); } } catch (Exception ignore) { // end } reader.close(); // retrieve links ... reader = getEventReader(new ByteArrayInputStream(bos.toByteArray())); try { int pos = 0; while (true) { final Map.Entry<Integer, XmlElement> linkElement = getAtomElement(reader, null, LINK, null, pos, 2, 2, false); res.put("[LINK]" + linkElement.getValue().getStart().getAttributeByName(new QName("title")).getValue(), linkElement.getValue().toStream()); pos = linkElement.getKey(); } } catch (Exception ignore) { // end } return res; }