List of usage examples for javax.xml.stream XMLEventReader nextEvent
public XMLEvent nextEvent() throws XMLStreamException;
From source file:org.apache.olingo.fit.utils.AbstractXMLUtilities.java
public InputStream addAtomInlinecount(final InputStream feed, final int count, final Accept accept) throws Exception { final XMLEventReader reader = getEventReader(feed); final ByteArrayOutputStream bos = new ByteArrayOutputStream(); final XMLEventWriter writer = getEventWriter(bos); try {// w w w . ja v a2 s.c o m final XmlElement feedElement = extractElement(reader, writer, Collections.<String>singletonList("feed"), 0, 1, 1).getValue(); writer.add(feedElement.getStart()); addAtomElement(IOUtils.toInputStream(String.format("<m:count>%d</m:count>", count)), writer); writer.add(feedElement.getContentReader()); writer.add(feedElement.getEnd()); while (reader.hasNext()) { writer.add(reader.nextEvent()); } } finally { writer.flush(); writer.close(); reader.close(); IOUtils.closeQuietly(feed); } return new ByteArrayInputStream(bos.toByteArray()); }
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;/* w w w .ja v a 2 s. c om*/ 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.XMLUtilities.java
public XMLElement getXmlElement(final StartElement start, final XMLEventReader reader) throws Exception { final XMLElement res = new XMLElement(); res.setStart(start);// w ww. ja v a 2 s . c o m final Charset encoding = Charset.forName(org.apache.olingo.commons.api.Constants.UTF8); final ByteArrayOutputStream content = new ByteArrayOutputStream(); final OutputStreamWriter writer = new OutputStreamWriter(content, encoding); int depth = 1; while (reader.hasNext() && depth > 0) { final XMLEvent event = reader.nextEvent(); if (event.getEventType() == XMLStreamConstants.START_ELEMENT) { depth++; } else if (event.getEventType() == XMLStreamConstants.END_ELEMENT) { depth--; } if (depth == 0) { res.setEnd(event.asEndElement()); } else { event.writeAsEncodedUnicode(writer); } } writer.flush(); writer.close(); res.setContent(new ByteArrayInputStream(content.toByteArray())); return res; }
From source file:org.apache.olingo.fit.utils.XMLUtilities.java
public Map.Entry<Integer, XMLElement> extractElement(final XMLEventReader reader, final XMLEventWriter writer, final List<String> path, final Collection<Map.Entry<String, String>> filter, final boolean filterInOr, final int startPathPos, final int minPathPos, final int maxPathPos) throws Exception { StartElement start = null;/* ww w . j ava 2 s . c o m*/ int searchFor = 0; int depth = startPathPos; // Current inspected element String current = null; // set defaults final List<String> pathElementNames = path == null ? Collections.<String>emptyList() : path; final Collection<Map.Entry<String, String>> filterAttrs = filter == null ? Collections.<Map.Entry<String, String>>emptySet() : filter; while (reader.hasNext() && start == null) { final XMLEvent event = reader.nextEvent(); if (event.getEventType() == XMLStreamConstants.START_ELEMENT) { depth++; if (current != null || ((minPathPos < 0 || minPathPos <= depth) && (maxPathPos < 0 || depth <= maxPathPos))) { if (pathElementNames.isEmpty() || pathElementNames.get(searchFor).trim() .equals(event.asStartElement().getName().getLocalPart())) { if (searchFor < pathElementNames.size() - 1) { // path exploring not completed writeEvent(event, writer); current = pathElementNames.get(searchFor).trim(); searchFor++; } else { // path exploring completed ... evaluate filter about path element name attribute boolean match = filterAttrs.isEmpty() || !filterInOr; for (Map.Entry<String, String> filterAttr : filterAttrs) { final Attribute attr = event.asStartElement() .getAttributeByName(new QName(filterAttr.getKey().trim())); if (attr == null || !filterAttr.getValue().trim().equals(attr.getValue())) { match = filterInOr ? match : false; } else { match = filterInOr ? true : match; } } if (match) { // found searched element start = event.asStartElement(); } else { skipElement(event.asStartElement(), reader, writer, false); depth--; } } } else if (current == null) { writeEvent(event, writer); } else { // skip element skipElement(event.asStartElement(), reader, writer, false); depth--; } } else { writeEvent(event, writer); } } else if (event.getEventType() == XMLStreamConstants.END_ELEMENT) { depth--; writeEvent(event, writer); if (event.asEndElement().getName().getLocalPart().equals(current)) { // back step .... searchFor--; current = searchFor > 0 ? pathElementNames.get(searchFor - 1).trim() : null; } } else { writeEvent(event, writer); } } if (start == null) { throw new NotFoundException(); } return new SimpleEntry<Integer, XMLElement>(Integer.valueOf(depth - 1), getXmlElement(start, reader)); }
From source file:org.apache.olingo.fit.utils.XMLUtilities.java
public InputStream addAtomInlinecount(final InputStream feed, final int count) throws Exception { final XMLEventReader reader = getEventReader(feed); final ByteArrayOutputStream bos = new ByteArrayOutputStream(); final XMLEventWriter writer = getEventWriter(bos); try {/*www . ja v a 2 s .c om*/ final XMLElement feedElement = extractElement(reader, writer, Collections.<String>singletonList("feed"), 0, 1, 1).getValue(); writer.add(feedElement.getStart()); addAtomElement(IOUtils.toInputStream(String.format("<m:count>%d</m:count>", count), Constants.ENCODING), writer); writer.add(feedElement.getContentReader()); writer.add(feedElement.getEnd()); while (reader.hasNext()) { writer.add(reader.nextEvent()); } } finally { writer.flush(); writer.close(); reader.close(); IOUtils.closeQuietly(feed); } return new ByteArrayInputStream(bos.toByteArray()); }
From source file:org.apache.olingo.fit.utils.XMLUtilities.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;// w ww.ja v a 2 s. 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 && Constants.get(ConstantKey.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 && Constants.get(ConstantKey.LINK).equals(event.asEndElement().getName().getLocalPart())) { writeNext = true; } else if (event.getEventType() == XMLStreamConstants.START_ELEMENT && (Constants.get(ConstantKey.PROPERTIES)) .equals(event.asStartElement().getName().getLocalPart())) { writeCurrent = true; writeNext = false; inProperties = true; } else if (event.getEventType() == XMLStreamConstants.END_ELEMENT && (Constants.get(ConstantKey.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 ((Constants.get(ConstantKey.ATOM_PROPERTY_PREFIX) + propertyName.trim()) .equals(elementName)) { writeCurrent = true; found.remove(propertyName); currentName = propertyName; } } } else if (event.getEventType() == XMLStreamConstants.END_ELEMENT && StringUtils.isNotBlank(currentName) && (Constants.get(ConstantKey.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.XMLUtilities.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(Constants.get(ConstantKey.ATOM_PROPERTY_PREFIX) + element); }//from w w w . j a va 2 s . com 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); while (changesReader.hasNext()) { final XMLEvent event = changesReader.nextEvent(); if (event.isStartElement() && event.asStartElement().getName().equals(element.getValue().getStart().getName())) { writer.add(element.getValue().getStart()); writer.add(changesReader); } else { writer.add(event); } } 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.server.core.deserializer.xml.ODataXmlDeserializer.java
private Object primitive(final XMLEventReader reader, final StartElement start, final EdmType type, final boolean isNullable, final Integer maxLength, final Integer precision, final Integer scale, final boolean isUnicode) throws XMLStreamException, EdmPrimitiveTypeException, DeserializerException { Object value = null;/*from ww w .j ava 2 s . c om*/ boolean foundEndProperty = false; while (reader.hasNext() && !foundEndProperty) { final XMLEvent event = reader.nextEvent(); if (event.isCharacters() && !event.asCharacters().isWhiteSpace()) { if (type instanceof AbstractGeospatialType<?>) { throw new DeserializerException("geo types support not implemented", DeserializerException.MessageKeys.NOT_IMPLEMENTED); } final EdmPrimitiveType primitiveType = (EdmPrimitiveType) type; final String stringValue = event.asCharacters().getData(); value = primitiveType.valueOfString(stringValue, isNullable, maxLength, precision, scale, isUnicode, primitiveType.getDefaultType()); } if (event.isEndElement() && start.getName().equals(event.asEndElement().getName())) { foundEndProperty = true; } } return value; }
From source file:org.apache.olingo.server.core.deserializer.xml.ODataXmlDeserializer.java
private Object complex(final XMLEventReader reader, final StartElement start, final EdmComplexType edmComplex) throws XMLStreamException, EdmPrimitiveTypeException, DeserializerException { ComplexValue value = new ComplexValue(); boolean foundEndProperty = false; while (reader.hasNext() && !foundEndProperty) { final XMLEvent event = reader.nextEvent(); if (event.isStartElement()) { StartElement se = event.asStartElement(); EdmProperty p = (EdmProperty) edmComplex.getProperty(se.getName().getLocalPart()); value.getValue().add(property(reader, se, p.getType(), p.isNullable(), p.getMaxLength(), p.getPrecision(), p.getScale(), p.isUnicode(), p.isCollection())); }/*from ww w.j a va2 s . c o m*/ if (event.isEndElement() && start.getName().equals(event.asEndElement().getName())) { foundEndProperty = true; } } return value; }
From source file:org.apache.olingo.server.core.deserializer.xml.ODataXmlDeserializer.java
private void collection(final Valuable valuable, final XMLEventReader reader, final StartElement start, final EdmType edmType, final boolean isNullable, final Integer maxLength, final Integer precision, final Integer scale, final boolean isUnicode) throws XMLStreamException, EdmPrimitiveTypeException, DeserializerException { List<Object> values = new ArrayList<Object>(); boolean foundEndProperty = false; while (reader.hasNext() && !foundEndProperty) { final XMLEvent event = reader.nextEvent(); if (event.isStartElement()) { if (edmType instanceof EdmPrimitiveType) { values.add(primitive(reader, event.asStartElement(), edmType, isNullable, maxLength, precision, scale, isUnicode)); } else if (edmType instanceof EdmComplexType) { values.add(complex(reader, event.asStartElement(), (EdmComplexType) edmType)); }// w w w.j a va2 s . com // do not add null or empty values } if (event.isEndElement() && start.getName().equals(event.asEndElement().getName())) { foundEndProperty = true; } } valuable.setValue(getValueType(edmType, true), values); }