return readElement(reader); } else if (event.isCharacters()) { return readCharacters(reader); } else if (event.isStartDocument()) { return readDocument(reader); } else if (event.isProcessingInstruction()) { return readProcessingInstruction(reader); } else if (event.isEntityReference()) { return readEntityReference(reader); } else if (event.isAttribute()) { return readAttribute(reader); } else if (event.isNamespace()) { return readNamespace(reader); } else { throw new XMLStreamException("Unsupported event: " + event);
Element elem = createElement(startTag); Node child = readNode(eventReader); elem.add(child);
/** * Constructs a DOM4J Attribute from the provided event stream. The stream * must be positioned before an {@link Attribute}event. * * @param reader * The event stream from which to read the Attribute. * * @return The Attribute that was read from the stream. * * @throws XMLStreamException * If an error occured reading events from the stream, or the * stream was not positioned before an {@link Attribute}event. */ public org.dom4j.Attribute readAttribute(XMLEventReader reader) throws XMLStreamException { XMLEvent event = reader.peek(); if (event.isAttribute()) { Attribute attr = (Attribute) reader.nextEvent(); return createAttribute(null, attr); } else { throw new XMLStreamException("Expected Attribute event, found: " + event); } }
@Override public Node readNode(XMLEventReader reader) throws XMLStreamException { // dom4j's reader misses handling of XML comments. So if the document we // are trying to read has comments this process will blow up. So we // override that to add that support as best we can XMLEvent event = reader.peek(); if ( javax.xml.stream.events.Comment.class.isInstance( event ) ) { return super.readComment( reader ); } return super.readNode( reader ); } };
@Override public Document convert(InputStream original) { try { return new STAXEventReader().readDocument(new InputStreamReader(original, Charset.forName("UTF-8"))); } catch (XMLStreamException e) { throw new CannotConvertBetweenTypesException("Cannot convert from InputStream to dom4j Document.", e); } } }
private Document toDom4jDocument(XMLEventReader jpaOrmXmlEventReader, Origin origin) { // todo : do we need to build a DocumentFactory instance for use here? // historically we did that to set TCCL since, iirc, dom4j uses TCCL org.dom4j.io.STAXEventReader staxToDom4jReader = new STAXEventReader() { @Override public Node readNode(XMLEventReader reader) throws XMLStreamException { // dom4j's reader misses handling of XML comments. So if the document we // are trying to read has comments this process will blow up. So we // override that to add that support as best we can XMLEvent event = reader.peek(); if ( javax.xml.stream.events.Comment.class.isInstance( event ) ) { return super.readComment( reader ); } return super.readNode( reader ); } }; try { return staxToDom4jReader.readDocument( jpaOrmXmlEventReader ); } catch (XMLStreamException e) { throw new MappingException( "An error occurred transforming orm.xml document from StAX to dom4j representation ", e, origin ); } } }
/** * Constructs a new DOM4J Attribute from the provided StAX Attribute event. * * @param elem * DOCUMENT ME! * @param attr * The Attribute event from which to construct the new DOM4J * Attribute. * * @return The Attribute constructed from the provided Attribute event. */ public org.dom4j.Attribute createAttribute(Element elem, Attribute attr) { return factory.createAttribute(elem, createQName(attr.getName()), attr .getValue()); }
/** * Constructs a DOM4J Comment from the provided event stream. The stream * must be positioned before a {@link Comment}event. * * @param reader * The event stream from which to read the Comment. * * @return The Comment that was read from the stream. * * @throws XMLStreamException * If an error occured reading events from the stream, or the * stream was not positioned before a {@link Comment}event. */ public org.dom4j.Comment readComment(XMLEventReader reader) throws XMLStreamException { XMLEvent event = reader.peek(); if (event instanceof Comment) { return createComment((Comment) reader.nextEvent()); } else { throw new XMLStreamException("Expected Comment event, found: " + event); } }
/** * Constructs a DOM4J Namespace from the provided event stream. The stream * must be positioned before a {@link Namespace}event. * * @param reader * The event stream from which to read the Namespace. * * @return The Namespace that was read from the stream. * * @throws XMLStreamException * If an error occured reading events from the stream, or the * stream was not positioned before a {@link Namespace}event. */ public org.dom4j.Namespace readNamespace(XMLEventReader reader) throws XMLStreamException { XMLEvent event = reader.peek(); if (event.isNamespace()) { Namespace ns = (Namespace) reader.nextEvent(); return createNamespace(ns); } else { throw new XMLStreamException("Expected Namespace event, found: " + event); } }
/** * Constructs a DOM4J ProcessingInstruction from the provided event stream. * The stream must be positioned before a {@link ProcessingInstruction} * event. * * @param reader * The event stream from which to read the ProcessingInstruction. * * @return The ProcessingInstruction that was read from the stream. * * @throws XMLStreamException * If an error occured reading events from the stream, or the * stream was not positioned before a {@link * ProcessingInstruction} event. */ public org.dom4j.ProcessingInstruction readProcessingInstruction( XMLEventReader reader) throws XMLStreamException { XMLEvent event = reader.peek(); if (event.isProcessingInstruction()) { ProcessingInstruction pi = (ProcessingInstruction) reader .nextEvent(); return createProcessingInstruction(pi); } else { throw new XMLStreamException("Expected PI event, found: " + event); } }
/** * Constructs a DOM4J Text or CDATA section from the provided event stream. * The stream must be positioned before a {@link Characters}event. * * @param reader * The event stream from which to read the Text or CDATA. * * @return The Text or CDATA that was read from the stream. * * @throws XMLStreamException * If an error occured reading events from the stream, or the * stream was not positioned before a {@link Characters}event. */ public CharacterData readCharacters(XMLEventReader reader) throws XMLStreamException { XMLEvent event = reader.peek(); if (event.isCharacters()) { Characters characters = reader.nextEvent().asCharacters(); return createCharacterData(characters); } else { throw new XMLStreamException("Expected Characters event, found: " + event); } }
/** * Constructs a DOM4J Entity from the provided event stream. The stream must * be positioned before an {@link EntityReference}event. * * @param reader * The event stream from which to read the {@link * EntityReference}. * * @return The {@link org.dom4j.Entity}that was read from the stream. * * @throws XMLStreamException * If an error occured reading events from the stream, or the * stream was not positioned before an {@link EntityReference} * event. */ public Entity readEntityReference(XMLEventReader reader) throws XMLStreamException { XMLEvent event = reader.peek(); if (event.isEntityReference()) { EntityReference entityRef = (EntityReference) reader.nextEvent(); return createEntity(entityRef); } else { throw new XMLStreamException("Expected EntityRef event, found: " + event); } }
Node n = readNode(reader); doc.add(n);
/** * Constructs a StAX event stream from the provided I/O stream and reads a * DOM4J document from it. * * @param is * The I/O stream from which the Document will be read. * * @return The Document that was read from the stream. * * @throws XMLStreamException * If an error occurs reading content from the stream. */ public Document readDocument(InputStream is) throws XMLStreamException { return readDocument(is, null); }
/** * Constructs a new DOM4J Element from the provided StartElement event. All * attributes and namespaces will be added to the returned element. * * @param startEvent * The StartElement event from which to construct the new DOM4J * Element. * * @return The Element constructed from the provided StartElement event. */ public Element createElement(StartElement startEvent) { QName qname = startEvent.getName(); org.dom4j.QName elemName = createQName(qname); Element elem = factory.createElement(elemName); // create attributes for (Iterator<Attribute> i = startEvent.getAttributes(); i.hasNext();) { Attribute attr = i.next(); elem.addAttribute(createQName(attr.getName()), attr.getValue()); } // create namespaces for (Iterator<Namespace> i = startEvent.getNamespaces(); i.hasNext();) { Namespace ns = i.next(); elem.addNamespace(ns.getPrefix(), ns.getNamespaceURI()); } return elem; }
@Override public Document convert(InputStream original) { try { return new STAXEventReader().readDocument(new InputStreamReader(original, Charset.forName("UTF-8"))); } catch (XMLStreamException e) { throw new CannotConvertBetweenTypesException("Cannot convert from InputStream to dom4j Document.", e); } } }
/** * Constructs a DOM4J Comment from the provided event stream. The stream * must be positioned before a {@link Comment}event. * * @param reader * The event stream from which to read the Comment. * * @return The Comment that was read from the stream. * * @throws XMLStreamException * If an error occured reading events from the stream, or the * stream was not positioned before a {@link Comment}event. */ public org.dom4j.Comment readComment(XMLEventReader reader) throws XMLStreamException { XMLEvent event = reader.peek(); if (event instanceof Comment) { return createComment((Comment) reader.nextEvent()); } else { throw new XMLStreamException("Expected Comment event, found: " + event); } }
/** * Constructs a DOM4J Namespace from the provided event stream. The stream * must be positioned before a {@link Namespace}event. * * @param reader * The event stream from which to read the Namespace. * * @return The Namespace that was read from the stream. * * @throws XMLStreamException * If an error occured reading events from the stream, or the * stream was not positioned before a {@linkNamespace}event. */ public org.dom4j.Namespace readNamespace(XMLEventReader reader) throws XMLStreamException { XMLEvent event = reader.peek(); if (event.isNamespace()) { Namespace ns = (Namespace) reader.nextEvent(); return createNamespace(ns); } else { throw new XMLStreamException("Expected Namespace event, found: " + event); } }
/** * Constructs a DOM4J ProcessingInstruction from the provided event stream. * The stream must be positioned before a {@link ProcessingInstruction} * event. * * @param reader * The event stream from which to read the ProcessingInstruction. * * @return The ProcessingInstruction that was read from the stream. * * @throws XMLStreamException * If an error occured reading events from the stream, or the * stream was not positioned before a {@link * ProcessingInstruction} event. */ public org.dom4j.ProcessingInstruction readProcessingInstruction( XMLEventReader reader) throws XMLStreamException { XMLEvent event = reader.peek(); if (event.isProcessingInstruction()) { ProcessingInstruction pi = (ProcessingInstruction) reader .nextEvent(); return createProcessingInstruction(pi); } else { throw new XMLStreamException("Expected PI event, found: " + event); } }
/** * Constructs a DOM4J Text or CDATA section from the provided event stream. * The stream must be positioned before a {@link Characters}event. * * @param reader * The event stream from which to read the Text or CDATA. * * @return The Text or CDATA that was read from the stream. * * @throws XMLStreamException * If an error occured reading events from the stream, or the * stream was not positioned before a {@linkCharacters}event. */ public CharacterData readCharacters(XMLEventReader reader) throws XMLStreamException { XMLEvent event = reader.peek(); if (event.isCharacters()) { Characters characters = reader.nextEvent().asCharacters(); return createCharacterData(characters); } else { throw new XMLStreamException("Expected Characters event, found: " + event); } }