List of usage examples for javax.xml.stream XMLEventReader close
public void close() throws XMLStreamException;
From source file:org.apache.olingo.fit.utils.AbstractXMLUtilities.java
@Override public InputStream selectEntity(final InputStream entity, final String[] propertyNames) throws Exception { final XMLEventReader reader = getEventReader(entity); final ByteArrayOutputStream bos = new ByteArrayOutputStream(); final XMLEventWriter writer = getEventWriter(bos); final List<String> found = new ArrayList<String>(Arrays.asList(propertyNames)); boolean inProperties = false; boolean writeCurrent = true; Boolean writeNext = null;//from www . j ava2s.c o m String currentName = null; final List<String> fieldToBeSaved = new ArrayList<String>(Arrays.asList(propertyNames)); while (reader.hasNext()) { final XMLEvent event = reader.nextEvent(); if (event.getEventType() == XMLStreamConstants.START_ELEMENT && LINK.equals(event.asStartElement().getName().getLocalPart()) && !fieldToBeSaved .contains(event.asStartElement().getAttributeByName(new QName("title")).getValue()) && !"edit".equals(event.asStartElement().getAttributeByName(new QName("rel")).getValue())) { writeCurrent = false; } else if (event.getEventType() == XMLStreamConstants.END_ELEMENT && LINK.equals(event.asEndElement().getName().getLocalPart())) { writeNext = true; } else if (event.getEventType() == XMLStreamConstants.START_ELEMENT && (PROPERTIES).equals(event.asStartElement().getName().getLocalPart())) { writeCurrent = true; writeNext = false; inProperties = true; } else if (event.getEventType() == XMLStreamConstants.END_ELEMENT && (PROPERTIES).equals(event.asEndElement().getName().getLocalPart())) { writeCurrent = true; } else if (inProperties) { if (event.getEventType() == XMLStreamConstants.START_ELEMENT) { final String elementName = event.asStartElement().getName().getLocalPart(); for (String propertyName : propertyNames) { if ((ATOM_PROPERTY_PREFIX + propertyName.trim()).equals(elementName)) { writeCurrent = true; found.remove(propertyName); currentName = propertyName; } } } else if (event.getEventType() == XMLStreamConstants.END_ELEMENT && StringUtils.isNotBlank(currentName) && (ATOM_PROPERTY_PREFIX + currentName.trim()) .equals(event.asEndElement().getName().getLocalPart())) { writeNext = false; currentName = null; } } if (writeCurrent) { writer.add(event); } if (writeNext != null) { writeCurrent = writeNext; writeNext = null; } } writer.flush(); writer.close(); reader.close(); IOUtils.closeQuietly(entity); // Do not raise any exception in order to support FC properties as well // if (!found.isEmpty()) { // throw new Exception(String.format("Could not find a properties '%s'", found)); // } return new ByteArrayInputStream(bos.toByteArray()); }
From source file:org.apache.olingo.fit.utils.AbstractXMLUtilities.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);/* ww w . j av a 2s .c om*/ IOUtils.closeQuietly(src); // retrieve properties ... XMLEventReader reader = getEventReader(new ByteArrayInputStream(bos.toByteArray())); final Map.Entry<Integer, XmlElement> propertyElement = extractElement(reader, null, Collections.<String>singletonList(PROPERTIES), 0, 2, 3); reader.close(); reader = propertyElement.getValue().getContentReader(); try { while (true) { final XmlElement property = extractElement(reader, null, null, 0, -1, -1).getValue(); 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 = extractElement(reader, null, Collections.<String>singletonList(LINK), pos, 2, 2); res.put("[LINK]" + linkElement.getValue().getStart().getAttributeByName(new QName("title")).getValue(), linkElement.getValue().toStream()); pos = linkElement.getKey(); } } catch (Exception ignore) { // end } return res; }
From source file:org.apache.olingo.fit.utils.AbstractXMLUtilities.java
@Override public InputStream setChanges(final InputStream toBeChanged, final Map<String, InputStream> properties) throws Exception { XMLEventReader reader = getEventReader(toBeChanged); final ByteArrayOutputStream bos = new ByteArrayOutputStream(); XMLEventWriter writer = getEventWriter(bos); // --------------------------------- // add property changes // --------------------------------- Map.Entry<Integer, XmlElement> propertyElement = extractElement(reader, writer, Collections.<String>singletonList(PROPERTIES), 0, 2, 3); writer.flush();/* w w w . j ava2 s .com*/ ByteArrayOutputStream pbos = new ByteArrayOutputStream(); OutputStreamWriter pwriter = new OutputStreamWriter(pbos); final XMLEventReader propertyReader = propertyElement.getValue().getContentReader(); try { while (true) { final XmlElement property = extractElement(propertyReader, null, null, 0, -1, -1).getValue(); final String name = property.getStart().getName().getLocalPart(); if (properties.containsKey(name)) { // replace final InputStream replacement = properties.get(name); properties.remove(property.getStart().getName().getLocalPart()); pwriter.append(IOUtils.toString(replacement)); IOUtils.closeQuietly(replacement); } else { pwriter.append(IOUtils.toString(property.toStream())); } } } catch (Exception ignore) { // end } for (Map.Entry<String, InputStream> remains : properties.entrySet()) { if (!remains.getKey().startsWith("[LINK]")) { pwriter.append(IOUtils.toString(remains.getValue())); IOUtils.closeQuietly(remains.getValue()); } } pwriter.flush(); pwriter.close(); writer.add(propertyElement.getValue().getStart()); writer.add(new XMLEventReaderWrapper(new ByteArrayInputStream(pbos.toByteArray()))); writer.add(propertyElement.getValue().getEnd()); IOUtils.closeQuietly(pbos); writer.add(reader); reader.close(); writer.flush(); writer.close(); // --------------------------------- // --------------------------------- // add navigationm changes // --------------------------------- // remove existent links for (Map.Entry<String, InputStream> remains : properties.entrySet()) { if (remains.getKey().startsWith("[LINK]")) { reader = getEventReader(new ByteArrayInputStream(bos.toByteArray())); bos.reset(); writer = getEventWriter(bos); try { final String linkName = remains.getKey().substring(remains.getKey().indexOf("]") + 1); extractElement(reader, writer, Collections.<String>singletonList(LINK), Collections.<Map.Entry<String, String>>singleton( new SimpleEntry<String, String>("title", linkName)), false, 0, 2, 2); writer.add(reader); } catch (Exception ignore) { // ignore } writer.flush(); writer.close(); } } reader = getEventReader(new ByteArrayInputStream(bos.toByteArray())); bos.reset(); writer = getEventWriter(bos); propertyElement = extractElement(reader, writer, Collections.<String>singletonList(CONTENT), 0, 2, 2); writer.flush(); pbos.reset(); pwriter = new OutputStreamWriter(pbos); for (Map.Entry<String, InputStream> remains : properties.entrySet()) { if (remains.getKey().startsWith("[LINK]")) { pwriter.append(IOUtils.toString(remains.getValue())); IOUtils.closeQuietly(remains.getValue()); } } pwriter.flush(); pwriter.close(); writer.add(new XMLEventReaderWrapper(new ByteArrayInputStream(pbos.toByteArray()))); IOUtils.closeQuietly(pbos); writer.add(propertyElement.getValue().getStart()); writer.add(propertyElement.getValue().getContentReader()); writer.add(propertyElement.getValue().getEnd()); writer.add(reader); reader.close(); writer.flush(); writer.close(); // --------------------------------- return new ByteArrayInputStream(bos.toByteArray()); }
From source file:org.apache.olingo.fit.utils.AbstractXMLUtilities.java
@Override protected InputStream replaceLink(final InputStream toBeChanged, final String linkName, final InputStream replacement) throws Exception { final XMLEventReader reader = getEventReader(toBeChanged); final ByteArrayOutputStream bos = new ByteArrayOutputStream(); final XMLEventWriter writer = getEventWriter(bos); final XMLEventFactory eventFactory = XMLEventFactory.newInstance(); XMLEvent newLine = eventFactory.createSpace("\n"); try {//ww w .j av a 2s . c o m final XmlElement linkElement = extractElement(reader, writer, Collections.<String>singletonList(LINK), Collections.<Map.Entry<String, String>>singletonList( new SimpleEntry<String, String>("title", linkName)), false, 0, -1, -1).getValue(); writer.add(linkElement.getStart()); // ------------------------------------------ // write inline ... // ------------------------------------------ writer.add(newLine); writer.add(eventFactory.createStartElement("m", null, "inline")); addAtomElement(replacement, writer); writer.add(eventFactory.createEndElement("m", null, "inline")); writer.add(newLine); // ------------------------------------------ writer.add(linkElement.getEnd()); writer.add(reader); writer.flush(); writer.close(); } finally { reader.close(); IOUtils.closeQuietly(toBeChanged); } return new ByteArrayInputStream(bos.toByteArray()); }
From source file:org.apache.olingo.fit.utils.AbstractXMLUtilities.java
@Override public Map.Entry<String, List<String>> extractLinkURIs(final InputStream is) throws Exception { final ByteArrayOutputStream bos = new ByteArrayOutputStream(); IOUtils.copy(is, bos);//from ww w.j a v a2s.co m IOUtils.closeQuietly(is); XMLEventReader reader = getEventReader(new ByteArrayInputStream(bos.toByteArray())); final List<String> links = new ArrayList<String>(); try { while (true) { links.add(IOUtils .toString(extractElement(reader, null, Collections.<String>singletonList("uri"), 0, -1, -1) .getValue().getContent())); } } catch (Exception ignore) { // End document reached ... } reader.close(); String next; reader = getEventReader(new ByteArrayInputStream(bos.toByteArray())); try { next = IOUtils .toString(extractElement(reader, null, Collections.<String>singletonList("next"), 0, -1, -1) .getValue().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:org.apache.olingo.fit.utils.AbstractXMLUtilities.java
@Override public InputStream getProperty(final String entitySetName, final String entityId, final List<String> path, final String edmType) throws Exception { final List<String> pathElements = new ArrayList<String>(); for (String element : path) { pathElements.add(ATOM_PROPERTY_PREFIX + element); }// w w w. j ava2 s . com final InputStream src = fsManager.readFile(Commons.getEntityBasePath(entitySetName, entityId) + ENTITY, Accept.XML); final XMLEventReader reader = getEventReader(src); final XmlElement property = extractElement(reader, null, pathElements, 0, 3, 4).getValue(); reader.close(); IOUtils.closeQuietly(src); final ByteArrayOutputStream bos = new ByteArrayOutputStream(); final XMLEventWriter writer = getEventWriter(bos); final XMLEventFactory eventFactory = XMLEventFactory.newInstance(); writer.add(eventFactory.createStartDocument("UTF-8", "1.0")); writer.add(property.getStart()); if (property.getStart().getAttributeByName(new QName(ATOM_DATASERVICE_NS)) == null) { writer.add(eventFactory.createNamespace(ATOM_PROPERTY_PREFIX.substring(0, 1), DATASERVICES_NS)); } if (property.getStart().getAttributeByName(new QName(ATOM_METADATA_NS)) == null) { writer.add(eventFactory.createNamespace(ATOM_METADATA_PREFIX.substring(0, 1), METADATA_NS)); } writer.add(property.getContentReader()); writer.add(property.getEnd()); writer.flush(); writer.close(); return new ByteArrayInputStream(bos.toByteArray()); }
From source file:org.apache.olingo.fit.utils.AbstractXMLUtilities.java
@Override public InputStream replaceProperty(final InputStream src, final InputStream replacement, final List<String> path, final boolean justValue) throws Exception { final List<String> pathElements = new ArrayList<String>(); for (String element : path) { pathElements.add(ATOM_PROPERTY_PREFIX + element); }//from w w w. j av a2 s. c o m final XMLEventReader reader = getEventReader(src); final ByteArrayOutputStream bos = new ByteArrayOutputStream(); final XMLEventWriter writer = getEventWriter(bos); final Map.Entry<Integer, XmlElement> element = extractElement(reader, writer, pathElements, 0, 3, 4); if (justValue) { writer.add(element.getValue().getStart()); } final XMLEventReader changesReader = new XMLEventReaderWrapper(replacement); writer.add(changesReader); changesReader.close(); IOUtils.closeQuietly(replacement); if (justValue) { writer.add(element.getValue().getEnd()); } writer.add(reader); reader.close(); IOUtils.closeQuietly(src); writer.flush(); writer.close(); return new ByteArrayInputStream(bos.toByteArray()); }
From source file:org.apache.olingo.fit.utils.AbstractXMLUtilities.java
@Override public InputStream deleteProperty(final InputStream src, final List<String> path) throws Exception { final List<String> pathElements = new ArrayList<String>(); for (String element : path) { pathElements.add(ATOM_PROPERTY_PREFIX + element); }/* w w w . ja v a2s . co m*/ final XMLEventReader reader = getEventReader(src); final ByteArrayOutputStream bos = new ByteArrayOutputStream(); final XMLEventWriter writer = getEventWriter(bos); final Map.Entry<Integer, XmlElement> element = extractElement(reader, writer, pathElements, 0, 3, 4); final XMLEventReader changesReader = new XMLEventReaderWrapper( IOUtils.toInputStream(String.format("<%s m:null=\"true\" />", path.get(path.size() - 1)))); writer.add(changesReader); changesReader.close(); writer.add(reader); reader.close(); IOUtils.closeQuietly(src); writer.flush(); writer.close(); return new ByteArrayInputStream(bos.toByteArray()); }
From source file:org.apache.olingo.fit.utils.v3.XMLUtilities.java
@Override public void retrieveLinkInfoFromMetadata() throws Exception { final MetadataLinkInfo metadataLinkInfo = new MetadataLinkInfo(); Commons.getLinkInfo().put(version, metadataLinkInfo); final InputStream metadata = fsManager.readFile(Constants.METADATA, Accept.XML); final XMLEventReader reader = getEventReader(metadata); try {//from w w w . j a v a 2s . co m while (true) { final Map.Entry<Integer, XmlElement> entitySetElement = extractElement(reader, null, Collections.<String>singletonList("EntitySet"), null, false, 0, -1, -1); retrieveLinks(entitySetElement.getValue(), metadataLinkInfo); } } catch (Exception e) { } finally { reader.close(); } }
From source file:org.apache.olingo.fit.utils.v3.XMLUtilities.java
private void retrieveLinks(final XmlElement entitySetElement, final MetadataLinkInfo metadataLinkInfo) throws Exception { final InputStream metadata = fsManager.readFile(Constants.METADATA, Accept.XML); final ByteArrayOutputStream bos = new ByteArrayOutputStream(); IOUtils.copy(metadata, bos);//from ww w .j av a 2s . com IOUtils.closeQuietly(metadata); final String entitySetName = entitySetElement.getStart().getAttributeByName(new QName("Name")).getValue() .trim(); final String entityType = entitySetElement.getStart().getAttributeByName(new QName("EntityType")).getValue() .trim(); final Collection<Map.Entry<String, String>> filter = new HashSet<Map.Entry<String, String>>(); filter.add(new SimpleEntry<String, String>("Name", entityType.substring(entityType.lastIndexOf(".") + 1, entityType.length()))); filter.add(new SimpleEntry<String, String>("BaseType", entityType)); final XMLEventReader reader = getEventReader(new ByteArrayInputStream(bos.toByteArray())); final Map.Entry<Integer, XmlElement> entityTypeElement = extractElement(reader, null, Collections.<String>singletonList("EntityType"), filter, true, 0, -1, -1); final XMLEventReader entityReader = entityTypeElement.getValue().getContentReader(); int size = 0; try { while (true) { final XmlElement navProperty = extractElement(entityReader, null, Collections.<String>singletonList("NavigationProperty"), null, false, 0, -1, -1).getValue(); final String linkName = navProperty.getStart().getAttributeByName(new QName("Name")).getValue(); final Map.Entry<String, Boolean> target = getTargetInfo(navProperty.getStart(), linkName); metadataLinkInfo.addLink(entitySetName, linkName, target.getKey(), target.getValue()); size++; } } catch (Exception e) { } finally { entityReader.close(); } if (size == 0) { metadataLinkInfo.addEntitySet(entitySetName); } }