private StartElement withNamespace(StartElement startElement) { // otherwise, wrap the start element event to provide a default namespace mapping final List<Namespace> namespaces = new ArrayList<Namespace>(); namespaces.add( xmlEventFactory.createNamespace( "", namespaceUri ) ); Iterator<?> originalNamespaces = startElement.getNamespaces(); while ( originalNamespaces.hasNext() ) { namespaces.add( (Namespace) originalNamespaces.next() ); } return xmlEventFactory.createStartElement( new QName( namespaceUri, startElement.getName().getLocalPart() ), startElement.getAttributes(), namespaces.iterator() ); }
private void doWriteStartElement(StartElement startElement) throws XMLStreamException { this.eventWriter.add(startElement); this.endElements.add(this.eventFactory.createEndElement(startElement.getName(), startElement.getNamespaces())); }
@Override public void writeAttribute(String namespaceURI, String localName, String value) throws XMLStreamException { this.eventWriter.add(this.eventFactory.createAttribute(new QName(namespaceURI, localName), value)); }
final List<Namespace> targetNamespaces = new ArrayList<Namespace>(); if ( "".equals( startElement.getName().getNamespaceURI() ) ) { targetNamespaces.add( xmlEventFactory.createNamespace( MappingXsdSupport.INSTANCE.hbmXsd().getNamespaceUri() ) ); final Iterator<Namespace> originalNamespaces = startElement.getNamespaces(); while ( originalNamespaces.hasNext() ) { Namespace namespace = originalNamespaces.next(); if ( NAMESPACE_URIS_TO_MAP.contains( namespace.getNamespaceURI() ) ) { namespace = xmlEventFactory.createNamespace( namespace.getPrefix(), MappingXsdSupport.INSTANCE.hbmXsd().getNamespaceUri() ); xmlEventFactory.setLocation( startElement.getLocation() ); return xmlEventFactory.createStartElement( new QName( MappingXsdSupport.INSTANCE.hbmXsd().getNamespaceUri(), startElement.getName().getLocalPart() ), startElement.getAttributes(), targetNamespaces.iterator()
private StartElement wrap(StartElement startElement) { List<Attribute> newElementAttributeList = mapAttributes( startElement ); List<Namespace> newNamespaceList = mapNamespaces( startElement ); // Transfer the location info from the incoming event to the event factory // so that the event we ask it to generate for us has the same location info xmlEventFactory.setLocation( startElement.getLocation() ); return xmlEventFactory.createStartElement( new QName( MappingXsdSupport.INSTANCE.latestJpaDescriptor().getNamespaceUri(), startElement.getName().getLocalPart() ), newElementAttributeList.iterator(), newNamespaceList.iterator() ); }
private StartElement transform(StartElement startElement) { String elementName = startElement.getName().getLocalPart(); // use the start element to determine whether we have a persistence.xml or orm.xml if ( START_ELEMENT_TO_NAMESPACE_URI.containsKey( elementName ) ) { currentDocumentNamespaceUri = START_ELEMENT_TO_NAMESPACE_URI.get( elementName ); } List<Attribute> newElementAttributeList = updateElementAttributes( startElement ); List<Namespace> newNamespaceList = updateElementNamespaces( startElement ); // create the new element return xmlEventFactory.createStartElement( new QName( currentDocumentNamespaceUri, startElement.getName().getLocalPart() ), newElementAttributeList.iterator(), newNamespaceList.iterator() ); }
private XMLEvent wrap(EndElement endElement) { final List<Namespace> targetNamespaces = mapNamespaces( existingXmlNamespacesIterator( endElement ) ); // Transfer the location info from the incoming event to the event factory // so that the event we ask it to generate for us has the same location info xmlEventFactory.setLocation( endElement.getLocation() ); return xmlEventFactory.createEndElement( new QName( MappingXsdSupport.INSTANCE.latestJpaDescriptor().getNamespaceUri(), endElement.getName().getLocalPart() ), targetNamespaces.iterator() ); }
/** Write a new element with the specified name and text * @return the end element */ private XMLEvent getAddedEvent(XMLEvent event, XMLEventWriter writer, String elementName, String text) throws XMLStreamException { StartElement oldStartEvent = event.asStartElement(); StartElement newStartEvent = xmlEventFactory.createStartElement(new QName(elementName), null, oldStartEvent.getNamespaces()); writer.add(newStartEvent); writer.add(xmlEventFactory.createCharacters(text)); return xmlEventFactory.createEndElement(newStartEvent.getName(), newStartEvent.getNamespaces()); }
/** Write a new element with the specified name and attribute before the end element is written out * @return the end element */ private XMLEvent getAddedEventBeforeEndElement(XMLEvent event, XMLEventWriter writer, String elementName, String attributeName, String attributeValue) throws XMLStreamException { Attribute newAttribute = xmlEventFactory.createAttribute(attributeName, attributeValue); Set attributes = new HashSet(); attributes.add(newAttribute); EndElement oldEvent = event.asEndElement(); StartElement newStartEvent = xmlEventFactory.createStartElement(new QName(elementName), attributes.iterator(), oldEvent.getNamespaces()); writer.add(newStartEvent); return xmlEventFactory.createEndElement(newStartEvent.getName(), newStartEvent.getNamespaces()); }
@Override public void writeStartElement(String namespaceURI, String localName) throws XMLStreamException { closeEmptyElementIfNecessary(); doWriteStartElement(this.eventFactory.createStartElement(new QName(namespaceURI, localName), null, null)); }
/** Create a new start element based on the original but that does not include * the specified attribute. */ private StartElement getSkippedElementStartEvent(XMLEvent event) { Set attributes = new HashSet(); for(java.util.Iterator i = event.asStartElement().getAttributes(); i.hasNext();) { Attribute a = (Attribute) i.next(); if( !DISABLE_SUB_ELEMENTS.contains(a.getName().getLocalPart())) { attributes.add(a); } } StartElement oldStartEvent = event.asStartElement(); return xmlEventFactory.createStartElement(oldStartEvent.getName(), attributes.iterator(), oldStartEvent.getNamespaces()); }
XMLEventFactory events = XMLEventFactory.newInstance(); QName bar = new QName("urn:bar", "bar"); XMLOutputFactory factory = XMLOutputFactory.newInstance(); factory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true); XMLEventWriter writer = factory.createXMLEventWriter(System.out); writer.add(events.createStartDocument()); writer.setDefaultNamespace("urn:bar"); writer.add(events.createStartElement(bar, null, null)); writer.add(events.createEndDocument()); writer.flush();
private List<Attribute> updateElementAttributes(StartElement startElement) { // adjust the version attribute List<Attribute> newElementAttributeList = new ArrayList<Attribute>(); Iterator<?> existingAttributesIterator = startElement.getAttributes(); while ( existingAttributesIterator.hasNext() ) { Attribute attribute = (Attribute) existingAttributesIterator.next(); if ( VERSION_ATTRIBUTE_NAME.equals( attribute.getName().getLocalPart() ) ) { if ( !DEFAULT_VERSION.equals( attribute.getName().getPrefix() ) ) { newElementAttributeList.add( xmlEventFactory.createAttribute( attribute.getName(), DEFAULT_VERSION ) ); } } else { newElementAttributeList.add( attribute ); } } return newElementAttributeList; } }
private Attribute mapAttribute(StartElement startElement, Attribute originalAttribute) { // Here we look to see if this attribute is the JPA version attribute, and if so do 2 things: // 1) validate its version attribute is valid // 2) update its version attribute to the default version if not already // // NOTE : atm this is a very simple check using just the attribute's local name // rather than checking its qualified name. It is possibly (though unlikely) // that this could match on "other" version attributes in the same element if ( ROOT_ELEMENT_NAME.equals( startElement.getName().getLocalPart() ) ) { if ( VERSION_ATTRIBUTE_NAME.equals( originalAttribute.getName().getLocalPart() ) ) { final String specifiedVersion = originalAttribute.getValue(); if ( !LocalXsdResolver.isValidJpaVersion( specifiedVersion ) ) { throw new BadVersionException( specifiedVersion ); } return xmlEventFactory.createAttribute( VERSION_ATTRIBUTE_NAME, LocalXsdResolver.latestJpaVerison() ); } } return originalAttribute; }
XMLEventFactory EVT_FACTORY; XMLEventReader reader; XMLEventWriter writer; QName numberQName = new QName("number"); QName factorsQName = new QName("factors"); while(reader.hasNext()) { XMLEvent e = reader.nextEvent(); if(e.isAttribute() && ((Attribute)e).getName().equals(numberQName)) { String v = ((Attribute)e).getValue(); String factors = factorize(Integer.parseInt(v)); XMLEvent factorsAttr = EVT_FACTORY.createAttribute(factorsQName, factors); writer.add(factorsAttr); } // pass through writers.add(e); }
public void endCDATA() throws SAXException { eventFactory.setLocation(getCurrentLocation()); try { writer.add(eventFactory.createCData(CDATABuffer.toString())); } catch (XMLStreamException e) { throw new SAXException(e); } super.endCDATA(); }
@Override public void endDocument() throws SAXException { this.eventFactory.setLocation(this.getCurrentLocation()); try { this.consumer.add(this.eventFactory.createEndDocument()); } catch (XMLStreamException e) { throw new SAXException(e); } this.namespaces = null; // clear the namespaces this.namespaceStack.clear(); }
public void comment(char[] ch, int start, int length) throws SAXException { this.eventFactory.setLocation(this.getCurrentLocation()); try { this.consumer.add(this.eventFactory.createComment(new String(ch, start, length))); } catch (XMLStreamException e) { throw new SAXException(e); } }
public void endCDATA() throws SAXException { this.eventFactory.setLocation(this.getCurrentLocation()); try { this.consumer.add(this.eventFactory.createCData(this.CDATABuffer.toString())); } catch (XMLStreamException e) { throw new SAXException(e); } this.isCDATA = false; this.CDATABuffer.setLength(0); }
public void endDocument() throws SAXException { try { staxWriter.add(staxEventFactory.createEndDocument()); staxWriter.flush(); } catch (XMLStreamException e) { throw new SAXException(e); } }