public String getPIData() { if (state == STATE_START_DOCUMENT || state == STATE_END_DOCUMENT) { throw new IllegalStateException(); } else { return parent.getPIData(); } }
private void handlePI() throws XMLStreamException { try { saxHandler.processingInstruction( staxStreamReader.getPITarget(), staxStreamReader.getPIData()); } catch (SAXException e) { throw new XMLStreamException2(e); } }
public CheckResult checkStream(String name, InputStream stream) throws IOException { XMLStreamReader reader = null; try { XMLInputFactory inputFactory = XMLInputFactory.newInstance(); inputFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, true); inputFactory.setProperty(XMLInputFactory.IS_VALIDATING, false); inputFactory.setProperty(XMLInputFactory.SUPPORT_DTD, false); reader = inputFactory.createXMLStreamReader(stream); ParserState state = ParserState.beforeRoot; while (reader.hasNext()) { int type = reader.next(); String localName; switch (type) { case PROCESSING_INSTRUCTION: if (PI_TARGET_STYLESHEET.equals(reader.getPITarget()) && PI_DATA_STYLESHEET.equals(reader.getPIData())) return new CheckResult(accept, "Recognised stylesheet", null); break; case START_ELEMENT: localName = reader.getLocalName(); switch (state) { case beforeRoot: String nsUri = reader.getNamespaceURI(); if (nsUri != null) return CheckResult.fromBool( NS_URI.equals(nsUri), "Correct namespace on root element", "Incorrect namespace on root element: " + nsUri, null); if (!TAG_REPOSITORY.equals(localName)) return new CheckResult(reject, "Incorrect root element name", null); state = ParserState.inRoot; break; case inRoot: if (TAG_RESOURCE.equals(localName)) { state = ParserState.inResource; } else if (!TAG_REFERRAL.equals(localName)) { return new CheckResult( reject, String.format( "Incorrect element '%s', expected '%s' or '%s'.", localName, TAG_RESOURCE, TAG_REFERRAL), null); } break; case inResource: if (TAG_CAPABILITY.equals(localName)) { state = ParserState.inCapability; } break; case inCapability: return CheckResult.fromBool( TAG_PROPERTY.equals(localName), "Found 'p' tag inside 'capability'", String.format( "Incorrect element '%s' inside '%s'; expected '%s'.", localName, TAG_CAPABILITY, TAG_PROPERTY), null); } break; case END_ELEMENT: localName = reader.getLocalName(); if (state == ParserState.inResource && TAG_RESOURCE.equals(localName)) state = ParserState.inRoot; if (state == ParserState.inCapability && TAG_CAPABILITY.equals(localName)) state = ParserState.inResource; break; } } return new CheckResult(undecided, "Reached end of stream", null); } catch (XMLStreamException e) { return new CheckResult(reject, "Invalid XML", e); } finally { if (reader != null) try { reader.close(); } catch (XMLStreamException e) { } } }
public String getPIData() { return streamReader.getPIData(); }
@Override protected ProcessingInstructionNode parseProcessingInstruction( XMLStreamReader reader, ElementNode parent) throws XMLStreamException { ProcessingInstructionNode node = null; String pi = reader.getPITarget(); Map<String, String> data = parseProcessingInstructionData(reader.getPIData()); if (pi.equals(TemplateParser.EXTENSIONS_PI)) { if (!data.isEmpty()) { throw new IllegalStateException( "<?" + TemplateParser.EXTENSIONS_PI + "?> should not take any data"); } if (extensionPlaceholder != null) { throw new IllegalStateException( "Can only have one occurrence of <?" + TemplateParser.EXTENSIONS_PI + "?>"); } node = new ProcessingInstructionNode(TemplateParser.EXTENSIONS_PI, null); extensionPlaceholder = node; } else if (pi.equals(TemplateParser.SUBSYSTEMS_PI)) { if (!parent.getName().equals("profile")) { throw new IllegalStateException( "<?" + TemplateParser.SUBSYSTEMS_PI + "?> must be a child of <profile> " + reader.getLocation()); } if (data.size() == 0 || !data.containsKey("socket-binding-group")) { throw new IllegalStateException( "Must have 'socket-binding-group' as <?" + TemplateParser.SUBSYSTEMS_PI + "?> data"); } if (data.size() > 1) { throw new IllegalStateException( "Only 'socket-binding-group' is valid <?" + TemplateParser.SUBSYSTEMS_PI + "?> data"); } String profileName = parent.getAttributeValue("name", ""); node = new ProcessingInstructionNode(profileName, data); subsystemPlaceHolders.put(profileName, node); } else if (pi.equals(TemplateParser.SOCKET_BINDINGS_PI)) { if (!parent.getName().equals("socket-binding-group")) { throw new IllegalStateException( "<?" + TemplateParser.SOCKET_BINDINGS_PI + "?> must be a child of <socket-binding-group> " + reader.getLocation()); } if (!data.isEmpty()) { throw new IllegalStateException( "<?" + TemplateParser.SOCKET_BINDINGS_PI + "?> should not take any data"); } String groupName = parent.getAttributeValue("name", ""); node = new ProcessingInstructionNode(TemplateParser.SOCKET_BINDINGS_PI, data); socketBindingsPlaceHolder.put(groupName, node); } else if (pi.equals(TemplateParser.INTERFACES_PI)) { if (!parent.getName().equals("interfaces")) { throw new IllegalStateException( "<?" + TemplateParser.INTERFACES_PI + "?> must be a child of <interfaces> " + reader.getLocation()); } if (!data.isEmpty()) { throw new IllegalStateException( "<?" + TemplateParser.INTERFACES_PI + "?> should not take any data"); } if (interfacesPlaceHolder != null) { throw new IllegalStateException( "Can only have one occurrence of <?" + TemplateParser.INTERFACES_PI + "?>"); } node = new ProcessingInstructionNode(TemplateParser.INTERFACES_PI, data); interfacesPlaceHolder = node; } else { throw new IllegalStateException( "Unknown processing instruction <?" + reader.getPITarget() + "?>" + reader.getLocation()); } return node; }
public void parse(XMLStreamReader input) throws XMLStreamException, XNIException { if (input.hasNext()) { int eventType = input.getEventType(); if (eventType != XMLStreamConstants.START_DOCUMENT && eventType != XMLStreamConstants.START_ELEMENT) { throw new XMLStreamException(); } fLocationWrapper.setLocation(input.getLocation()); fSchemaDOMParser.startDocument(fLocationWrapper, null, fNamespaceContext, null); boolean first = true; loop: while (input.hasNext()) { if (!first) { eventType = input.next(); } else { first = false; } switch (eventType) { case XMLStreamConstants.START_ELEMENT: ++fDepth; fLocationWrapper.setLocation(input.getLocation()); fNamespaceContext.setNamespaceContext(input.getNamespaceContext()); fillQName( fElementQName, input.getNamespaceURI(), input.getLocalName(), input.getPrefix()); fillXMLAttributes(input); fillDeclaredPrefixes(input); addNamespaceDeclarations(); fNamespaceContext.pushContext(); fSchemaDOMParser.startElement(fElementQName, fAttributes, null); break; case XMLStreamConstants.END_ELEMENT: fLocationWrapper.setLocation(input.getLocation()); fNamespaceContext.setNamespaceContext(input.getNamespaceContext()); fillQName( fElementQName, input.getNamespaceURI(), input.getLocalName(), input.getPrefix()); fillDeclaredPrefixes(input); fSchemaDOMParser.endElement(fElementQName, null); fNamespaceContext.popContext(); --fDepth; if (fDepth <= 0) { break loop; } break; case XMLStreamConstants.CHARACTERS: fTempString.setValues( input.getTextCharacters(), input.getTextStart(), input.getTextLength()); fSchemaDOMParser.characters(fTempString, null); break; case XMLStreamConstants.SPACE: fTempString.setValues( input.getTextCharacters(), input.getTextStart(), input.getTextLength()); fSchemaDOMParser.ignorableWhitespace(fTempString, null); break; case XMLStreamConstants.CDATA: fSchemaDOMParser.startCDATA(null); fTempString.setValues( input.getTextCharacters(), input.getTextStart(), input.getTextLength()); fSchemaDOMParser.characters(fTempString, null); fSchemaDOMParser.endCDATA(null); break; case XMLStreamConstants.PROCESSING_INSTRUCTION: fillProcessingInstruction(input.getPIData()); fSchemaDOMParser.processingInstruction(input.getPITarget(), 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); } }
public String getPIData() { return reader.getPIData(); }