/** * Parses an {@link ProcessingInstruction XML event} of a * processing instruction. * @param pi the event to parse * @return the data from the processing instruction or <code>null</code> * if the event is {@link #isIgnoredPI(ProcessingInstruction) ignored} * by this parser */ public TocPIData parse(ProcessingInstruction pi) throws JAXBException { if (isIgnoredPI(pi)) return null; String rawData = pi.getData(); rawData = null == rawData ? "" : rawData.trim(); boolean closing = rawData.endsWith("/"); if (closing) rawData = rawData.substring(0, rawData.length() - 1).trim(); TocPIData data; if (0 < rawData.length()) data = parseAttrs(rawData); else if (closing) data = new TocPIData(); else throw new JAXBException("Processing instruction contains no data"); data.setClosing(closing); return data; }
private void handlePI(ProcessingInstruction event) throws XMLStreamException { try { _sax.processingInstruction( event.getTarget(), event.getData()); } catch (SAXException e) { throw new XMLStreamException(e); } }
@Override public String getPITarget() { if (this.event.isProcessingInstruction()) { return ((ProcessingInstruction) this.event).getTarget(); } else { throw new IllegalStateException(); } }
@Override public String getPIData() { if (this.event.isProcessingInstruction()) { return ((ProcessingInstruction) this.event).getData(); } else { throw new IllegalStateException(); } }
@Override public String getPITarget() { XMLSecEvent xmlSecEvent = getCurrentEvent(); if (xmlSecEvent.getEventType() != PROCESSING_INSTRUCTION) { throw new IllegalStateException(ERR_STATE_NOT_PI); } return ((ProcessingInstruction) xmlSecEvent).getTarget(); }
@Override public String getPIData() { XMLSecEvent xmlSecEvent = getCurrentEvent(); if (xmlSecEvent.getEventType() != PROCESSING_INSTRUCTION) { throw new IllegalStateException(ERR_STATE_NOT_PI); } return ((ProcessingInstruction) xmlSecEvent).getData(); }
public String getPITarget() { if (event.isProcessingInstruction()) { return ((ProcessingInstruction) event).getTarget(); } else { throw new IllegalStateException(); } }
public String getPIData() { if (event.isProcessingInstruction()) { return ((ProcessingInstruction) event).getData(); } else { throw new IllegalStateException(); } }
private void processEvent(XmlParserContext ctx, XMLEvent xmlEvent) throws BagriException { if (ctx.getDocRoot() == null) { ctx.firstEvents.add(xmlEvent); if (xmlEvent.getEventType() == XMLStreamConstants.START_ELEMENT) { String root = "/" + xmlEvent.asStartElement().getName(); ctx.addDocument(root); for (XMLEvent event: ctx.firstEvents) { processEvent(ctx, event); } } } else { switch (xmlEvent.getEventType()) { case XMLStreamConstants.START_ELEMENT: startElement(ctx, xmlEvent.asStartElement()); break; case XMLStreamConstants.CHARACTERS: if (!xmlEvent.asCharacters().isWhiteSpace()) { ctx.addCharacters(xmlEvent.asCharacters().getData()); } break; case XMLStreamConstants.END_ELEMENT: ctx.endElement(); break; case XMLStreamConstants.ATTRIBUTE: ctx.addAttribute(((Attribute) xmlEvent).getName(), ((Attribute) xmlEvent).getValue()); break; case XMLStreamConstants.COMMENT: ctx.addComment(((Comment) xmlEvent).getText()); break; case XMLStreamConstants.PROCESSING_INSTRUCTION: ctx.addProcessingInstruction(((ProcessingInstruction) xmlEvent).getTarget(), ((ProcessingInstruction) xmlEvent).getData()); break; default: break; } } }
public String getPITarget() { if (this.event.isProcessingInstruction()) { return ((ProcessingInstruction) this.event).getTarget(); } else { throw new IllegalStateException(); } }
public String getPIData() { if (this.event.isProcessingInstruction()) { return ((ProcessingInstruction) this.event).getData(); } else { throw new IllegalStateException(); } }
public void processingInstruction(ProcessingInstruction event) throws XMLStreamException { String data = event.getData(); if (data != null && data.length() > 0) { fStreamWriter.writeProcessingInstruction(event.getTarget(), data); } else { fStreamWriter.writeProcessingInstruction(event.getTarget()); } }
@Override public void add(XMLEvent event) throws XMLStreamException { if (event instanceof ProcessingInstruction) { if (null == piData) piData = parsePIEvent((ProcessingInstruction)event); } else piData = null; if (null != piData) { if (piData.isOpening()) { formatter = TocFormatter.forPI(piData, event.getLocation()); formatter.setXMLEventFactory(getXMLEventFactory()); } if (piData.isClosing()) super.add(getXMLEventFactory().createCharacters("\n")); } else if (state == State.INDEXED) index(event); else if (state == State.ROOT) { addDTD((StartElement)event); super.add(event); state = State.PASSTHROUGH; } }
protected void trackContext(XMLEvent event) { switch (event.getEventType()) { case START_ELEMENT: context.add(0, (StartElement)event); break; case END_DOCUMENT: case END_ELEMENT: if (!context.isEmpty()) { StartElement open = context.remove(0); EndElement close = event instanceof EndElement ? (EndElement)event : null; if (null == close || !open.getName().equals(close.getName())) throw new IllegalStateException("Unclosed " + describeEvent(open) + ' ' + describeLocation(open.getLocation())); } else throw new IllegalStateException("Unclosed " + describeEvent(origin) + ' ' + describeLocation(origin.getLocation())); break; case PROCESSING_INSTRUCTION: if (!TocPIParser.isIgnoredPI((ProcessingInstruction)event)) throw new IllegalStateException("Processing instructions <?" + TocPIParser.PI_TARGET + "?> are not allowed within the context of " + describeEvent(origin)); break; } }
protected TocPIData parsePIEvent(ProcessingInstruction event) throws XMLStreamException { try { return piParser().parse(event); } catch (JAXBException pierr) { throw new XMLStreamException("Error parsing " + describeEvent(event), event.getLocation(), pierr); } }
protected TocPIData filterPIEvent(ProcessingInstruction event) { try { return piParser().parse(event); } catch (JAXBException pierr) { throw new IllegalArgumentException("Error parsing " + describeEvent(event), pierr); } }
private void processProcessingInstruction(final ProcessingInstruction processingInstruction, final NodeLocation location) { // Create and append ProcessingInstruction node final org.w3c.dom.ProcessingInstruction processingInstructionNode = doc.createProcessingInstruction( processingInstruction.getTarget(), processingInstruction.getData()); state.getCurrentParentNode().appendChild(processingInstructionNode); state.createdNode(); }
public void parse(XMLEventReader input) throws XMLStreamException, XNIException { XMLEvent currentEvent = input.peek(); if (currentEvent != null) { int eventType = currentEvent.getEventType(); if (eventType != XMLStreamConstants.START_DOCUMENT && eventType != XMLStreamConstants.START_ELEMENT) { throw new XMLStreamException(); } fLocationWrapper.setLocation(currentEvent.getLocation()); fSchemaDOMParser.startDocument(fLocationWrapper, null, fNamespaceContext, null); loop: while (input.hasNext()) { currentEvent = input.nextEvent(); eventType = currentEvent.getEventType(); switch (eventType) { case XMLStreamConstants.START_ELEMENT: ++fDepth; StartElement start = currentEvent.asStartElement(); fillQName(fElementQName, start.getName()); fLocationWrapper.setLocation(start.getLocation()); fNamespaceContext.setNamespaceContext(start.getNamespaceContext()); fillXMLAttributes(start); fillDeclaredPrefixes(start); addNamespaceDeclarations(); fNamespaceContext.pushContext(); fSchemaDOMParser.startElement(fElementQName, fAttributes, null); break; case XMLStreamConstants.END_ELEMENT: EndElement end = currentEvent.asEndElement(); fillQName(fElementQName, end.getName()); fillDeclaredPrefixes(end); fLocationWrapper.setLocation(end.getLocation()); fSchemaDOMParser.endElement(fElementQName, null); fNamespaceContext.popContext(); --fDepth; if (fDepth <= 0) { break loop; } break; case XMLStreamConstants.CHARACTERS: sendCharactersToSchemaParser(currentEvent.asCharacters().getData(), false); break; case XMLStreamConstants.SPACE: sendCharactersToSchemaParser(currentEvent.asCharacters().getData(), true); break; case XMLStreamConstants.CDATA: fSchemaDOMParser.startCDATA(null); sendCharactersToSchemaParser(currentEvent.asCharacters().getData(), false); fSchemaDOMParser.endCDATA(null); break; case XMLStreamConstants.PROCESSING_INSTRUCTION: ProcessingInstruction pi = (ProcessingInstruction)currentEvent; fillProcessingInstruction(pi.getData()); fSchemaDOMParser.processingInstruction(pi.getTarget(), fTempString, null); break; case XMLStreamConstants.DTD: /* There shouldn't be a DTD in the schema */ break; case XMLStreamConstants.ENTITY_REFERENCE: /* Not needed for schemas */ break; case XMLStreamConstants.COMMENT: /* No point in sending comments */ break; case XMLStreamConstants.START_DOCUMENT: fDepth++; /* We automatically call startDocument before the loop */ break; case XMLStreamConstants.END_DOCUMENT: /* We automatically call endDocument after the loop */ break; } } fLocationWrapper.setLocation(null); fNamespaceContext.setNamespaceContext(null); fSchemaDOMParser.endDocument(null); } }
@Override public ProcessingInstruction createProcessingInstruction(String target, String data) { ProcessingInstructionEvent event = new ProcessingInstructionEvent(target, data); if(location != null)event.setLocation(location); return event; }
@Test public void testEvents() { XMLEventFactory f = XMLEventFactory.newInstance(); final String contents = "test <some> text & more! [[]] --"; final String prefix = "prefix"; final String uri = "http://foo"; final String localName = "elem"; try { StartDocument sd = f.createStartDocument(); writeAsEncodedUnicode(sd); Comment c = f.createComment("some comments"); writeAsEncodedUnicode(c); StartElement se = f.createStartElement(prefix, uri, localName); ProcessingInstruction pi = f.createProcessingInstruction("target", "data"); writeAsEncodedUnicode(pi); Namespace ns = f.createNamespace(prefix, uri); writeAsEncodedUnicode(ns); Characters characters = f.createCharacters(contents); writeAsEncodedUnicode(characters); // CData Characters cdata = f.createCData(contents); writeAsEncodedUnicode(cdata); // Attribute QName attrName = new QName("http://test.com", "attr", "ns"); Attribute attr = f.createAttribute(attrName, "value"); writeAsEncodedUnicode(attr); // prefix, uri, localName EndElement ee = f.createEndElement(prefix, uri, localName); writeAsEncodedUnicode(ee); EndDocument ed = f.createEndDocument(); writeAsEncodedUnicode(ed); } catch (Exception e) { Assert.fail(e.getMessage()); } }
@Override public ProcessingInstruction createProcessingInstruction(String target, String data) { return defaultImpl.createProcessingInstruction(target, data); }
@Override public ProcessingInstruction createProcessingInstruction(String target, String data) { return null; }
@Override protected void parseInternal() throws SAXException, XMLStreamException { boolean documentStarted = false; boolean documentEnded = false; int elementDepth = 0; while (this.reader.hasNext() && elementDepth >= 0) { XMLEvent event = this.reader.nextEvent(); if (!event.isStartDocument() && !event.isEndDocument() && !documentStarted) { handleStartDocument(event); documentStarted = true; } switch (event.getEventType()) { case XMLStreamConstants.START_DOCUMENT: handleStartDocument(event); documentStarted = true; break; case XMLStreamConstants.START_ELEMENT: elementDepth++; handleStartElement(event.asStartElement()); break; case XMLStreamConstants.END_ELEMENT: elementDepth--; if (elementDepth >= 0) { handleEndElement(event.asEndElement()); } break; case XMLStreamConstants.PROCESSING_INSTRUCTION: handleProcessingInstruction((ProcessingInstruction) event); break; case XMLStreamConstants.CHARACTERS: case XMLStreamConstants.SPACE: case XMLStreamConstants.CDATA: handleCharacters(event.asCharacters()); break; case XMLStreamConstants.END_DOCUMENT: handleEndDocument(); documentEnded = true; break; case XMLStreamConstants.NOTATION_DECLARATION: handleNotationDeclaration((NotationDeclaration) event); break; case XMLStreamConstants.ENTITY_DECLARATION: handleEntityDeclaration((EntityDeclaration) event); break; case XMLStreamConstants.COMMENT: handleComment((Comment) event); break; case XMLStreamConstants.DTD: handleDtd((DTD) event); break; case XMLStreamConstants.ENTITY_REFERENCE: handleEntityReference((EntityReference) event); break; } } if (documentStarted && !documentEnded) { handleEndDocument(); } }
private void handleProcessingInstruction(ProcessingInstruction pi) throws SAXException { if (getContentHandler() != null) { getContentHandler().processingInstruction(pi.getTarget(), pi.getData()); } }
public void add(XMLEvent event) throws XMLStreamException { QName name; String uri; switch (event.getEventType()) { case XMLStreamConstants.START_ELEMENT: StartElement startElement = event.asStartElement(); name = startElement.getName(); uri = name.getNamespaceURI(); if (uri != null && !"".equals(uri)) writer.writeStartElement(name.getPrefix(), name.getLocalPart(), uri); else writer.writeStartElement(name.getLocalPart()); break; case XMLStreamConstants.END_ELEMENT: writer.writeEndElement(); break; case XMLStreamConstants.ATTRIBUTE: Attribute attribute = (Attribute) event; name = attribute.getName(); uri = name.getNamespaceURI(); if (uri != null && !"".equals(uri)) writer.writeAttribute(name.getPrefix(), uri, name.getLocalPart(), attribute.getValue()); else writer.writeAttribute(name.getLocalPart(), attribute.getValue()); break; case XMLStreamConstants.NAMESPACE: Namespace namespace = (Namespace) event; uri = namespace.getNamespaceURI(); writer.writeNamespace(namespace.getPrefix(), uri); break; case XMLStreamConstants.PROCESSING_INSTRUCTION: ProcessingInstruction pi = (ProcessingInstruction) event; String data = pi.getData(); if (data == null) writer.writeProcessingInstruction(pi.getTarget()); else writer.writeProcessingInstruction(pi.getTarget(), data); break; case XMLStreamConstants.COMMENT: Comment comment = (Comment) event; writer.writeComment(comment.getText()); break; case XMLStreamConstants.START_DOCUMENT: StartDocument startDocument = (StartDocument) event; writer.writeStartDocument(startDocument.getVersion()); break; case XMLStreamConstants.END_DOCUMENT: writer.writeEndDocument(); break; case XMLStreamConstants.DTD: DTD dtd = (DTD) event; writer.writeDTD(dtd.getDocumentTypeDeclaration()); break; case XMLStreamConstants.CHARACTERS: case XMLStreamConstants.SPACE: Characters characters = event.asCharacters(); writer.writeCharacters(characters.getData()); break; case XMLStreamConstants.CDATA: Characters cdata = event.asCharacters(); writer.writeCData(cdata.getData()); break; } }