List of usage examples for javax.xml.stream XMLEventReader hasNext
@Override public boolean hasNext();
From source file:com.msopentech.odatajclient.testservice.utils.XMLUtilities.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 XMLOutputFactory xof = XMLOutputFactory.newInstance(); final XMLEventWriter writer = xof.createXMLEventWriter(bos); try {/*from ww w .ja va 2s . c om*/ final XmlElement feedElement = getAtomElement(reader, writer, "feed"); 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:com.msopentech.odatajclient.testservice.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 XMLOutputFactory xof = XMLOutputFactory.newInstance(); final XMLEventWriter writer = xof.createXMLEventWriter(bos); final List<String> found = new ArrayList<String>(Arrays.asList(propertyNames)); boolean inProperties = false; boolean writeCurrent = true; Boolean writeNext = null;/*from w w w.j a va 2s .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:com.marklogic.client.functionaltest.BasicJavaClientREST.java
/** * Convert XMLEventReader To String/* ww w . jav a 2 s . c o m*/ * @param XMLEventReader * @return String * @throws XMLStreamException, TransformerException, IOException, ParserConfigurationException, SAXException */ public String convertXMLEventReaderToString(XMLEventReader fileRead) throws IOException, TransformerException, XMLStreamException { //BufferedReader br = (BufferedReader) fileRead; String readContent = ""; String line = null; while (fileRead.hasNext()) readContent = readContent + fileRead.next(); return readContent; }
From source file:com.msopentech.odatajclient.testservice.utils.XMLUtilities.java
private InputStream writeFromStartToEndElement(final StartElement element, final XMLEventReader reader, final boolean document) throws XMLStreamException { final ByteArrayOutputStream bos = new ByteArrayOutputStream(); final XMLOutputFactory xof = XMLOutputFactory.newInstance(); final XMLEventWriter writer = xof.createXMLEventWriter(bos); final QName name = element.getName(); if (document) { final XMLEventFactory eventFactory = XMLEventFactory.newInstance(); writer.add(eventFactory.createStartDocument("UTF-8", "1.0")); writer.add(element);/* w ww. j a v a 2 s . c o m*/ if (element.getAttributeByName(new QName(ATOM_DATASERVICE_NS)) == null) { writer.add(eventFactory.createNamespace(ATOM_PROPERTY_PREFIX.substring(0, 1), DATASERVICES_NS)); } if (element.getAttributeByName(new QName(ATOM_METADATA_NS)) == null) { writer.add(eventFactory.createNamespace(ATOM_METADATA_PREFIX.substring(0, 1), METADATA_NS)); } } else { writer.add(element); } XMLEvent event = element; while (reader.hasNext() && !(event.isEndElement() && name.equals(event.asEndElement().getName()))) { event = reader.nextEvent(); writer.add(event); } writer.flush(); writer.close(); return new ByteArrayInputStream(bos.toByteArray()); }
From source file:nl.nn.adapterframework.validation.XSD.java
public void init() throws ConfigurationException { if (noNamespaceSchemaLocation != null) { url = ClassUtils.getResourceURL(classLoader, noNamespaceSchemaLocation); if (url == null) { throw new ConfigurationException("Cannot find [" + noNamespaceSchemaLocation + "]"); }/* www.jav a2s. c om*/ resourceTarget = noNamespaceSchemaLocation; toString = noNamespaceSchemaLocation; } else { if (resource != null) { url = ClassUtils.getResourceURL(classLoader, resource); if (url == null) { throw new ConfigurationException("Cannot find [" + resource + "]"); } resourceTarget = resource; toString = resource; if (resourceInternalReference != null) { resourceTarget = resourceTarget + "-" + resourceInternalReference + ".xsd"; toString = toString + "!" + resourceInternalReference; } } else if (sourceXsds == null) { throw new ConfigurationException( "None of noNamespaceSchemaLocation, resource or mergedResources is specified"); } else { resourceTarget = "["; toString = "["; boolean first = true; for (XSD xsd : sourceXsds) { if (first) { first = false; } else { resourceTarget = resourceTarget + ", "; toString = toString + ", "; } resourceTarget = resourceTarget + xsd.getResourceTarget().replaceAll("/", "_"); toString = toString + xsd.toString(); } resourceTarget = resourceTarget + "].xsd"; toString = toString + "]"; } if (parentLocation == null) { this.parentLocation = ""; } } try { InputStream in = getInputStream(); XMLEventReader er = XmlUtils.INPUT_FACTORY.createXMLEventReader(in, XmlUtils.STREAM_FACTORY_ENCODING); int elementDepth = 0; while (er.hasNext()) { XMLEvent e = er.nextEvent(); switch (e.getEventType()) { case XMLStreamConstants.START_ELEMENT: elementDepth++; StartElement el = e.asStartElement(); if (el.getName().equals(SchemaUtils.SCHEMA)) { Attribute a = el.getAttributeByName(SchemaUtils.TNS); if (a != null) { xsdTargetNamespace = a.getValue(); } Iterator<Namespace> nsIterator = el.getNamespaces(); while (nsIterator.hasNext() && StringUtils.isEmpty(xsdDefaultNamespace)) { Namespace ns = nsIterator.next(); if (StringUtils.isEmpty(ns.getPrefix())) { xsdDefaultNamespace = ns.getNamespaceURI(); } } } else if (el.getName().equals(SchemaUtils.IMPORT)) { Attribute a = el.getAttributeByName(SchemaUtils.NAMESPACE); if (a != null) { boolean skip = false; ArrayList ans = null; if (StringUtils.isNotEmpty(getImportedNamespacesToIgnore())) { ans = new ArrayList(Arrays.asList(getImportedNamespacesToIgnore().split(","))); } if (StringUtils.isNotEmpty(a.getValue()) && ans != null) { if (ans.contains(a.getValue())) { skip = true; } } if (!skip) { importedNamespaces.add(a.getValue()); } } } else if (el.getName().equals(SchemaUtils.ELEMENT)) { if (elementDepth == 2) { rootTags.add(el.getAttributeByName(SchemaUtils.NAME).getValue()); } } break; case XMLStreamConstants.END_ELEMENT: elementDepth--; break; } } this.targetNamespace = xsdTargetNamespace; if (namespace == null) { // In case WsdlXmlValidator doesn't have schemaLocation namespace = xsdTargetNamespace; } } catch (IOException e) { String message = "IOException reading XSD"; LOG.error(message, e); throw new ConfigurationException(message, e); } catch (XMLStreamException e) { String message = "XMLStreamException reading XSD"; LOG.error(message, e); throw new ConfigurationException(message, e); } }
From source file:nl.nn.adapterframework.validation.XSD.java
public Set<XSD> getXsdsRecursive(Set<XSD> xsds, boolean ignoreRedefine) throws ConfigurationException { try {/*from www .j av a 2s. c o m*/ InputStream in = getInputStream(); if (in == null) return null; XMLEventReader er = XmlUtils.INPUT_FACTORY.createXMLEventReader(in, XmlUtils.STREAM_FACTORY_ENCODING); while (er.hasNext()) { XMLEvent e = er.nextEvent(); switch (e.getEventType()) { case XMLStreamConstants.START_ELEMENT: StartElement el = e.asStartElement(); if (el.getName().equals(SchemaUtils.IMPORT) || el.getName().equals(SchemaUtils.INCLUDE) || (el.getName().equals(SchemaUtils.REDEFINE) && !ignoreRedefine)) { Attribute schemaLocationAttribute = el.getAttributeByName(SchemaUtils.SCHEMALOCATION); Attribute namespaceAttribute = el.getAttributeByName(SchemaUtils.NAMESPACE); String namespace = this.namespace; boolean addNamespaceToSchema = this.addNamespaceToSchema; if (el.getName().equals(SchemaUtils.IMPORT)) { if (namespaceAttribute == null && StringUtils.isEmpty(xsdDefaultNamespace) && StringUtils.isNotEmpty(xsdTargetNamespace)) { //TODO: concerning import without namespace when in head xsd default namespace doesn't exist and targetNamespace does) namespace = null; } else { if (namespaceAttribute != null) { namespace = namespaceAttribute.getValue(); } else { namespace = targetNamespace; } } } if (schemaLocationAttribute != null) { boolean skip = false; if (el.getName().equals(SchemaUtils.IMPORT) && namespaceAttribute == null) { if (StringUtils.isNotEmpty(xsdDefaultNamespace) && StringUtils.isNotEmpty(xsdTargetNamespace)) { //ignore import without namespace when in head xsd default namespace and targetNamespace exists) skip = true; } } if (!skip) { String sl = schemaLocationAttribute.getValue(); ArrayList aslti = null; if (StringUtils.isNotEmpty(getImportedSchemaLocationsToIgnore())) { aslti = new ArrayList( Arrays.asList(getImportedSchemaLocationsToIgnore().split(","))); } if (StringUtils.isNotEmpty(sl) && aslti != null) { if (isUseBaseImportedSchemaLocationsToIgnore()) { sl = FilenameUtils.getName(sl); } if (aslti.contains(sl)) { skip = true; } } } if (!skip) { ArrayList ans = null; if (StringUtils.isNotEmpty(getImportedNamespacesToIgnore())) { ans = new ArrayList(Arrays.asList(getImportedNamespacesToIgnore().split(","))); } if (StringUtils.isNotEmpty(namespace) && ans != null) { if (ans.contains(namespace)) { skip = true; } } } if (!skip) { XSD x = new XSD(); x.setClassLoader(classLoader); x.setNamespace(namespace); x.setResource(getResourceBase() + schemaLocationAttribute.getValue()); x.setAddNamespaceToSchema(addNamespaceToSchema); x.setImportedSchemaLocationsToIgnore(getImportedSchemaLocationsToIgnore()); x.setUseBaseImportedSchemaLocationsToIgnore( isUseBaseImportedSchemaLocationsToIgnore()); x.setImportedNamespacesToIgnore(getImportedNamespacesToIgnore()); x.setParentLocation(getResourceBase()); x.setRootXsd(false); x.init(); if (xsds.add(x)) { x.getXsdsRecursive(xsds, ignoreRedefine); } } } } break; } } } catch (IOException e) { String message = "IOException reading XSD"; LOG.error(message, e); throw new ConfigurationException(message, e); } catch (XMLStreamException e) { String message = "XMLStreamException reading XSD"; LOG.error(message, e); throw new ConfigurationException(message, e); } return xsds; }
From source file:org.apache.hadoop.util.ConfTest.java
private static List<NodeInfo> parseConf(InputStream in) throws XMLStreamException { QName configuration = new QName("configuration"); QName property = new QName("property"); List<NodeInfo> nodes = new ArrayList<NodeInfo>(); Stack<NodeInfo> parsed = new Stack<NodeInfo>(); XMLInputFactory factory = XMLInputFactory.newInstance(); XMLEventReader reader = factory.createXMLEventReader(in); while (reader.hasNext()) { XMLEvent event = reader.nextEvent(); if (event.isStartElement()) { StartElement currentElement = event.asStartElement(); NodeInfo currentNode = new NodeInfo(currentElement); if (parsed.isEmpty()) { if (!currentElement.getName().equals(configuration)) { return null; }// w w w . ja v a 2 s . c om } else { NodeInfo parentNode = parsed.peek(); QName parentName = parentNode.getStartElement().getName(); if (parentName.equals(configuration) && currentNode.getStartElement().getName().equals(property)) { @SuppressWarnings("unchecked") Iterator<Attribute> it = currentElement.getAttributes(); while (it.hasNext()) { currentNode.addAttribute(it.next()); } } else if (parentName.equals(property)) { parentNode.addElement(currentElement); } } parsed.push(currentNode); } else if (event.isEndElement()) { NodeInfo node = parsed.pop(); if (parsed.size() == 1) { nodes.add(node); } } else if (event.isCharacters()) { if (2 < parsed.size()) { NodeInfo parentNode = parsed.pop(); StartElement parentElement = parentNode.getStartElement(); NodeInfo grandparentNode = parsed.peek(); if (grandparentNode.getElement(parentElement) == null) { grandparentNode.setElement(parentElement, event.asCharacters()); } parsed.push(parentNode); } } } return nodes; }
From source file:org.apache.olingo.client.core.serialization.AtomDeserializer.java
private Object fromPrimitive(final XMLEventReader reader, final StartElement start, final EdmTypeInfo typeInfo) throws XMLStreamException, EdmPrimitiveTypeException { Object value = null;//from w w w . j a v a 2 s .c o m boolean foundEndProperty = false; while (reader.hasNext() && !foundEndProperty) { final XMLEvent event = reader.nextEvent(); if (event.isStartElement() && typeInfo != null && typeInfo.getPrimitiveTypeKind().isGeospatial()) { final EdmPrimitiveTypeKind geoType = EdmPrimitiveTypeKind .valueOfFQN(typeInfo.getFullQualifiedName().toString()); value = geoDeserializer.deserialize(reader, event.asStartElement(), geoType); } if (event.isCharacters() && !event.asCharacters().isWhiteSpace() && (typeInfo == null || !typeInfo.getPrimitiveTypeKind().isGeospatial())) { final String stringValue = event.asCharacters().getData(); if (typeInfo == null) { value = stringValue; } else { final EdmPrimitiveType primitiveType = (EdmPrimitiveType) typeInfo.getType(); final Class<?> returnType = primitiveType.getDefaultType().isAssignableFrom(Calendar.class) ? Timestamp.class : primitiveType.getDefaultType(); value = ((EdmPrimitiveType) typeInfo.getType()).valueOfString(stringValue, true, null, Constants.DEFAULT_PRECISION, Constants.DEFAULT_SCALE, true, returnType); } } if (event.isEndElement() && start.getName().equals(event.asEndElement().getName())) { foundEndProperty = true; } } return value; }
From source file:org.apache.olingo.client.core.serialization.AtomDeserializer.java
private Object fromComplexOrEnum(final XMLEventReader reader, final StartElement start) throws XMLStreamException, EdmPrimitiveTypeException { Object value = null;//from w ww . j av a 2 s . com boolean foundEndProperty = false; while (reader.hasNext() && !foundEndProperty) { final XMLEvent event = reader.nextEvent(); if (event.isStartElement()) { if (value == null) { value = new ComplexValue(); } if (Constants.QNAME_ATOM_ELEM_LINK.equals(event.asStartElement().getName())) { final Link link = new Link(); final Attribute rel = event.asStartElement() .getAttributeByName(QName.valueOf(Constants.ATTR_REL)); if (rel != null) { link.setRel(rel.getValue()); } final Attribute title = event.asStartElement() .getAttributeByName(QName.valueOf(Constants.ATTR_TITLE)); if (title != null) { link.setTitle(title.getValue()); } final Attribute href = event.asStartElement() .getAttributeByName(QName.valueOf(Constants.ATTR_HREF)); if (href != null) { link.setHref(href.getValue()); } final Attribute type = event.asStartElement() .getAttributeByName(QName.valueOf(Constants.ATTR_TYPE)); if (type != null) { link.setType(type.getValue()); } if (link.getRel().startsWith(Constants.NS_NAVIGATION_LINK_REL)) { ((ComplexValue) value).getNavigationLinks().add(link); inline(reader, event.asStartElement(), link); } else if (link.getRel().startsWith(Constants.NS_ASSOCIATION_LINK_REL)) { ((Valuable) value).asComplex().getAssociationLinks().add(link); } } else { ((ComplexValue) value).getValue().add(property(reader, event.asStartElement())); } } if (event.isCharacters() && !event.asCharacters().isWhiteSpace()) { value = event.asCharacters().getData(); } if (event.isEndElement() && start.getName().equals(event.asEndElement().getName())) { foundEndProperty = true; } } return value; }
From source file:org.apache.olingo.client.core.serialization.AtomDeserializer.java
private void fromCollection(final Valuable valuable, final XMLEventReader reader, final StartElement start, final EdmTypeInfo typeInfo) throws XMLStreamException, EdmPrimitiveTypeException { List<Object> values = new ArrayList<Object>(); ValueType valueType = ValueType.COLLECTION_PRIMITIVE; final EdmTypeInfo type = typeInfo == null ? null : new EdmTypeInfo.Builder().setTypeExpression(typeInfo.getFullQualifiedName().toString()).build(); boolean foundEndProperty = false; while (reader.hasNext() && !foundEndProperty) { final XMLEvent event = reader.nextEvent(); if (event.isStartElement()) { switch (guessPropertyType(reader, typeInfo)) { case COMPLEX: final Object complexValue = fromComplexOrEnum(reader, event.asStartElement()); valueType = ValueType.COLLECTION_COMPLEX; values.add(complexValue); break; case ENUM: valueType = ValueType.COLLECTION_ENUM; values.add(fromComplexOrEnum(reader, event.asStartElement())); break; case PRIMITIVE: final Object value = fromPrimitive(reader, event.asStartElement(), type); valueType = value instanceof Geospatial ? ValueType.COLLECTION_GEOSPATIAL : ValueType.COLLECTION_PRIMITIVE; values.add(value);/*www . j av a2 s. co m*/ break; default: // do not add null or empty values } } if (event.isEndElement() && start.getName().equals(event.asEndElement().getName())) { foundEndProperty = true; } } valuable.setValue(valueType, values); }