List of usage examples for javax.xml.stream XMLStreamReader hasNext
public boolean hasNext() throws XMLStreamException;
From source file:org.auraframework.impl.factory.SVGParser.java
@Override public SVGDef getDefinition(DefDescriptor<SVGDef> descriptor, TextSource<SVGDef> source) throws SVGParserException, QuickFixException { if (descriptor.getDefType() == DefType.SVG) { XMLStreamReader reader = null; String contents = source.getContents(); //If the file is too big throw before we parse the whole thing. SVGDef ret = new SVGDefHandler<>(descriptor, source).createDefinition(); try (StringReader stringReader = new StringReader(contents)) { reader = xmlInputFactory.createXMLStreamReader(stringReader); if (reader != null) { LOOP: while (reader.hasNext()) { int type = reader.next(); switch (type) { case XMLStreamConstants.END_DOCUMENT: break LOOP; //This is plain text inside the file case XMLStreamConstants.CHARACTERS: if (DISSALOWED_LIST.matcher(reader.getText()).matches()) { throw new InvalidDefinitionException( String.format("Text contains disallowed symbols: %s", reader.getText()), XMLParser.getLocation(reader, source)); }/*from w w w. ja va 2 s . co m*/ break; case XMLStreamConstants.START_ELEMENT: String name = reader.getName().toString().toLowerCase(); if (!SVG_TAG_WHITELIST.contains(name)) { throw new InvalidDefinitionException( String.format("Invalid SVG tag specified: %s", name), XMLParser.getLocation(reader, source)); } for (int i = 0; i < reader.getAttributeCount(); i++) { QName qAttr = reader.getAttributeName(i); String attr = qAttr.getLocalPart(); if (SVG_ATTR_BLACKLIST.contains(attr)) { throw new InvalidDefinitionException( String.format("Invalid SVG attribute specified: %s", attr), XMLParser.getLocation(reader, source)); } } break; case XMLStreamConstants.END_ELEMENT: case XMLStreamConstants.COMMENT: case XMLStreamConstants.DTD: case XMLStreamConstants.SPACE: continue; default: throw new InvalidDefinitionException(String.format("Found unexpected element in xml."), XMLParser.getLocation(reader, source)); } } } } catch (XMLStreamException e) { throw new SVGParserException(StringEscapeUtils.escapeHtml4(e.getMessage())); } finally { if (reader != null) { try { reader.close(); } catch (XMLStreamException e) { //Well I tried to play nicely } } } return ret; } return null; }
From source file:org.auraframework.impl.svg.parser.SVGParser.java
@Override public SVGDef parse(DefDescriptor<SVGDef> descriptor, Source<SVGDef> source) throws SVGParserException, QuickFixException { if (descriptor.getDefType() == DefType.SVG) { XMLStreamReader reader = null; String contents = source.getContents(); //If the file is too big throw before we parse the whole thing. SVGDef ret = new SVGDefHandler<>(descriptor, source).createDefinition(); try (StringReader stringReader = new StringReader(contents)) { reader = xmlInputFactory.createXMLStreamReader(stringReader); if (reader != null) { LOOP: while (reader.hasNext()) { int type = reader.next(); switch (type) { case XMLStreamConstants.END_DOCUMENT: break LOOP; //This is plain text inside the file case XMLStreamConstants.CHARACTERS: if (DISSALOWED_LIST.matcher(reader.getText()).matches()) { throw new InvalidDefinitionException( String.format("Text contains disallowed symbols: %s", reader.getText()), XMLParser.getLocation(reader, source)); }/*from ww w . ja v a 2s . c om*/ break; case XMLStreamConstants.START_ELEMENT: String name = reader.getName().toString().toLowerCase(); if (!SVG_TAG_WHITELIST.contains(name)) { throw new InvalidDefinitionException( String.format("Invalid SVG tag specified: %s", name), XMLParser.getLocation(reader, source)); } for (int i = 0; i < reader.getAttributeCount(); i++) { QName qAttr = reader.getAttributeName(i); String attr = qAttr.getLocalPart(); if (SVG_ATTR_BLACKLIST.contains(attr)) { throw new InvalidDefinitionException( String.format("Invalid SVG attribute specified: %s", attr), XMLParser.getLocation(reader, source)); } } break; case XMLStreamConstants.END_ELEMENT: case XMLStreamConstants.COMMENT: case XMLStreamConstants.DTD: case XMLStreamConstants.SPACE: continue; default: throw new InvalidDefinitionException(String.format("Found unexpected element in xml."), XMLParser.getLocation(reader, source)); } } } } catch (XMLStreamException e) { throw new SVGParserException(StringEscapeUtils.escapeHtml4(e.getMessage())); } finally { if (reader != null) { try { reader.close(); } catch (XMLStreamException e) { //Well I tried to play nicely } } } return ret; } return null; }
From source file:org.codice.ddf.opensearch.source.OpenSearchSource.java
private InputTransformer getInputTransformer(InputStream inputStream) throws UnsupportedQueryException { XMLStreamReader xmlStreamReader = null; try {//from w w w .ja v a2s . co m xmlStreamReader = xmlInputFactory.createXMLStreamReader(inputStream); while (xmlStreamReader.hasNext()) { int next = xmlStreamReader.next(); if (next == XMLStreamConstants.START_ELEMENT) { String namespaceUri = xmlStreamReader.getNamespaceURI(); InputTransformer transformerReference = lookupTransformerReference(namespaceUri); if (transformerReference != null) { return transformerReference; } } } } catch (XMLStreamException | InvalidSyntaxException e) { LOGGER.debug("Failed to parse transformer namespace", e); } finally { try { if (xmlStreamReader != null) { xmlStreamReader.close(); } } catch (XMLStreamException e) { LOGGER.debug("Failed to close namespace reader", e); } } throw new UnsupportedQueryException( "Unable to find applicable InputTransformer for metacard content from Atom feed."); }
From source file:org.commonjava.maven.ext.manip.util.PomPeek.java
private void parseCoordElements(final File pom) { Reader reader = null;//w w w . j a v a 2 s . c om XMLStreamReader xml = null; try { reader = new FileReader(pom); xml = XMLInputFactory.newFactory().createXMLStreamReader(reader); final Stack<String> path = new Stack<String>(); while (xml.hasNext()) { final int evt = xml.next(); switch (evt) { case START_ELEMENT: { final String elem = xml.getLocalName(); path.push(elem); if (captureValue(elem, path, xml)) { // seems like xml.getElementText() traverses the END_ELEMENT event... path.pop(); } break; } case END_ELEMENT: { final String elem = xml.getLocalName(); if (MODULES_ELEM.equals(elem)) { modulesDone = true; } path.pop(); break; } default: { } } if (foundAll()) { return; } } } catch (final IOException e) { logger.warn("Failed to peek at POM coordinate for: " + pom + " Reason: " + e.getMessage() + "\nThis POM will NOT be available as an ancestor to other models during effective-model building.", e); } catch (final XMLStreamException e) { logger.warn("Failed to peek at POM coordinate for: " + pom + " Reason: " + e.getMessage() + "\nThis POM will NOT be available as an ancestor to other models during effective-model building.", e); } catch (final FactoryConfigurationError e) { logger.warn("Failed to peek at POM coordinate for: " + pom + " Reason: " + e.getMessage() + "\nThis POM will NOT be available as an ancestor to other models during effective-model building.", e); } finally { if (xml != null) { try { xml.close(); } catch (final XMLStreamException e) { } } try { reader.close(); } catch (final IOException ioe) { } } }
From source file:org.commonjava.maven.galley.maven.parse.PomPeek.java
private void parseCoordElements() { InputStream in = null;/* ww w . j a v a 2 s. c o m*/ XMLStreamReader xml = null; try { if (pom != null) { in = new FileInputStream(pom); } else if (transfer != null) { in = transfer.openInputStream(false); } else { in = stream; } xml = XMLInputFactory.newFactory().createXMLStreamReader(in); final Stack<String> path = new Stack<>(); while (xml.hasNext()) { final int evt = xml.next(); switch (evt) { case START_ELEMENT: { final String elem = xml.getLocalName(); if (captureValue(elem, path, xml)) { // seems like xml.getElementText() traverses the END_ELEMENT event... path.pop(); } break; } case END_ELEMENT: { path.pop(); break; } default: { } } if (foundAll()) { return; } } } catch (final IOException | FactoryConfigurationError | XMLStreamException e) { logger.warn("Failed to peek at POM coordinate for: " + pom + " Reason: " + e.getMessage() + "\nThis POM will NOT be available as an ancestor to other models during effective-model building.", e); } finally { if (xml != null) { try { xml.close(); } catch (final XMLStreamException e) { logger.warn("Failed to close XMLStreamReader: " + e.getMessage(), e); } } closeQuietly(in); } }
From source file:org.deegree.client.core.renderer.OutputXMLRenderer.java
private void encodeXML(ResponseWriter writer, String value) throws IOException { int depth = 0; try {//from w w w . j a v a2 s . com XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(new StringReader(value)); boolean lastWasEndElement = false; boolean lastWasComment = false; while (reader.hasNext()) { switch (reader.getEventType()) { case XMLStreamConstants.START_ELEMENT: if (!lastWasComment) { writer.startElement("br", null); writer.endElement("br"); } writer.write(getSpaces(depth)); writer.startElement("span", null); writer.writeAttribute("class", "sign", null); writer.write("<"); writer.endElement("span"); writer.startElement("span", null); writer.writeAttribute("class", "tag", null); String prefix = reader.getPrefix(); writer.write( (prefix != null && prefix.length() > 0 ? prefix + ":" : "") + reader.getLocalName()); writer.endElement("span"); for (int i = 0; i < reader.getAttributeCount(); i++) { writer.startElement("span", null); writer.writeAttribute("class", "attributeName", null); writer.write(" "); String attributePrefix = reader.getAttributePrefix(i); writer.write( (attributePrefix != null && attributePrefix.length() > 0 ? attributePrefix + ":" : "") + reader.getAttributeLocalName(i)); writer.endElement("span"); writer.startElement("span", null); writer.writeAttribute("class", "sign", null); writer.write("=\""); writer.endElement("span"); writer.startElement("span", null); writer.writeAttribute("class", "text", null); writer.write(encodeString(reader.getAttributeValue(i))); writer.endElement("span"); writer.startElement("span", null); writer.writeAttribute("class", "sign", null); writer.write("\""); writer.endElement("span"); } for (int i = 0; i < reader.getNamespaceCount(); i++) { writer.startElement("span", null); writer.writeAttribute("class", "attributeName", null); writer.write(" "); String nsPrefix = reader.getNamespacePrefix(i); writer.write("xmlns"); if (nsPrefix != null && !nsPrefix.isEmpty()) { writer.write(":" + nsPrefix); } writer.endElement("span"); writer.startElement("span", null); writer.writeAttribute("class", "sign", null); writer.write("=\""); writer.endElement("span"); writer.startElement("span", null); writer.writeAttribute("class", "text", null); writer.write(reader.getNamespaceURI(i)); writer.endElement("span"); writer.startElement("span", null); writer.writeAttribute("class", "sign", null); writer.write("\""); writer.endElement("span"); } writer.startElement("span", null); writer.writeAttribute("class", "sign", null); writer.write(">"); writer.endElement("span"); depth++; lastWasEndElement = false; lastWasComment = false; break; case XMLStreamConstants.CHARACTERS: String text = reader.getText(); if (text.trim().length() > 0) { writer.startElement("span", null); writer.writeAttribute("class", "text", null); writer.write(encodeString(text)); writer.endElement("span"); lastWasEndElement = false; lastWasComment = false; } break; case XMLStreamConstants.END_ELEMENT: depth--; if (lastWasEndElement) { writer.startElement("br", null); writer.endElement("br"); writer.write(getSpaces(depth)); } writer.startElement("span", null); writer.writeAttribute("class", "sign", null); writer.write("</"); writer.endElement("span"); writer.startElement("span", null); writer.writeAttribute("class", "tag", null); prefix = reader.getPrefix(); writer.write( (prefix != null && prefix.length() > 0 ? prefix + ":" : "") + reader.getLocalName()); writer.endElement("span"); writer.startElement("span", null); writer.writeAttribute("class", "sign", null); writer.write(">"); writer.endElement("span"); lastWasEndElement = true; lastWasComment = false; break; case XMLStreamConstants.COMMENT: writer.startElement("div", null); writer.writeAttribute("class", "comment", null); writer.write("</!--" + reader.getText() + "-->"); writer.endElement("div"); lastWasEndElement = false; lastWasComment = true; break; default: break; } reader.next(); } reader.close(); } catch (Throwable e) { if (depth == 0) { writer.writeText("Response could not parsed as XML.", null); } else { writer.writeText("... (if you want the complete document, please click the download button)", null); } } }
From source file:org.eclipse.smila.connectivity.framework.agent.jobfile.JobFileReader.java
/** * Parse JobFile, read Records from the XML stream. The stream must be currently at the RecordList start tag. * //from w w w . ja v a2 s.c o m * @param staxReader * source XML stream * @param url * the url of the job file * @throws XMLStreamException * StAX error. */ private void parse(final XMLStreamReader staxReader, final URL url) throws XMLStreamException { staxReader.nextTag(); if (isStartTag(staxReader, TAG_JOB_FILE)) { while (staxReader.hasNext()) { staxReader.nextTag(); if (isStartTag(staxReader, TAG_ADD)) { staxReader.nextTag(); do { final Record record = _recordReader.readRecord(staxReader); if (record != null) { try { loadAttachments(record); _jobFileHandler.add(record); } catch (IOException e) { final String msg = "Error loading attachments for record " + record.getId() + ". Record is skipped."; if (_log.isErrorEnabled()) { _log.error(msg, e); } } } // if staxReader.nextTag(); } while (staxReader.hasNext() && !isEndTag(staxReader, TAG_ADD)); } else if (isStartTag(staxReader, TAG_DELETE)) { staxReader.nextTag(); do { final Record record = _recordReader.readRecord(staxReader); _jobFileHandler.delete(record); staxReader.nextTag(); } while (staxReader.hasNext() && !isEndTag(staxReader, TAG_DELETE)); } else if (isEndTag(staxReader, TAG_JOB_FILE)) { break; } } // while } else { throw new XMLStreamException( "Invalid document " + url + ". Must begin with tag <" + TAG_JOB_FILE + ">"); } }
From source file:org.eclipse.sw360.licenseinfo.parsers.AbstractCLIParser.java
protected <T> boolean hasThisXMLRootElement(AttachmentContent content, String rootElementNamespace, String rootElementName, User user, T context) throws TException { XMLInputFactory xmlif = XMLInputFactory.newFactory(); XMLStreamReader xmlStreamReader = null; InputStream attachmentStream = null; try {/*from www . j av a 2 s . c om*/ attachmentStream = attachmentConnector.getAttachmentStream(content, user, context); xmlStreamReader = xmlif.createXMLStreamReader(attachmentStream); //skip to first element while (xmlStreamReader.hasNext() && xmlStreamReader.next() != XMLStreamConstants.START_ELEMENT) ; xmlStreamReader.require(XMLStreamConstants.START_ELEMENT, rootElementNamespace, rootElementName); return true; } catch (XMLStreamException | SW360Exception e) { return false; } finally { if (null != xmlStreamReader) { try { xmlStreamReader.close(); } catch (XMLStreamException e) { // ignore it } } closeQuietly(attachmentStream, log); } }
From source file:org.elissa.web.server.TransformerServlet.java
private void revisitSequenceFlows(Definitions def, String orig) { try {//from w ww . j ava2 s .co m Map<String, Map<String, String>> sequenceFlowMapping = new HashMap<String, Map<String, String>>(); XMLInputFactory factory = XMLInputFactory.newInstance(); XMLStreamReader reader = factory.createXMLStreamReader(new StringReader(orig)); while (reader.hasNext()) { if (reader.next() == XMLStreamReader.START_ELEMENT) { if ("sequenceFlow".equals(reader.getLocalName())) { String id = ""; String source = ""; String target = ""; for (int i = 0; i < reader.getAttributeCount(); i++) { if ("id".equals(reader.getAttributeLocalName(i))) { id = reader.getAttributeValue(i); } if ("sourceRef".equals(reader.getAttributeLocalName(i))) { source = reader.getAttributeValue(i); } if ("targetRef".equals(reader.getAttributeLocalName(i))) { target = reader.getAttributeValue(i); } } Map<String, String> valueMap = new HashMap<String, String>(); valueMap.put("sourceRef", source); valueMap.put("targetRef", target); sequenceFlowMapping.put(id, valueMap); } } } List<RootElement> rootElements = def.getRootElements(); for (RootElement root : rootElements) { if (root instanceof Process) { Process process = (Process) root; List<FlowElement> flowElements = process.getFlowElements(); for (FlowElement fe : flowElements) { if (fe instanceof SequenceFlow) { SequenceFlow sf = (SequenceFlow) fe; if (sequenceFlowMapping.containsKey(sf.getId())) { sf.setSourceRef( getFlowNode(def, sequenceFlowMapping.get(sf.getId()).get("sourceRef"))); sf.setTargetRef( getFlowNode(def, sequenceFlowMapping.get(sf.getId()).get("targetRef"))); } else { _logger.error("Could not find mapping for sequenceFlow: " + sf.getId()); } } } } } } catch (FactoryConfigurationError e) { _logger.error(e.getMessage()); e.printStackTrace(); } catch (Exception e) { _logger.error(e.getMessage()); e.printStackTrace(); } }
From source file:org.elissa.web.server.TransformerServlet.java
private void addBpmnDiInfo(Definitions def, String gpd) { try {/* w w w.j a va2 s. c o m*/ Map<String, Bounds> _bounds = new HashMap<String, Bounds>(); XMLInputFactory factory = XMLInputFactory.newInstance(); XMLStreamReader reader = factory.createXMLStreamReader(new StringReader(gpd)); while (reader.hasNext()) { if (reader.next() == XMLStreamReader.START_ELEMENT) { if ("node".equals(reader.getLocalName())) { Bounds b = DcFactory.eINSTANCE.createBounds(); String nodeName = null; String nodeX = null; String nodeY = null; String nodeWidth = null; String nodeHeight = null; for (int i = 0; i < reader.getAttributeCount(); i++) { if ("name".equals(reader.getAttributeLocalName(i))) { nodeName = reader.getAttributeValue(i); } else if ("x".equals(reader.getAttributeLocalName(i))) { nodeX = reader.getAttributeValue(i); } else if ("y".equals(reader.getAttributeLocalName(i))) { nodeY = reader.getAttributeValue(i); } else if ("width".equals(reader.getAttributeLocalName(i))) { nodeWidth = reader.getAttributeValue(i); } else if ("height".equals(reader.getAttributeLocalName(i))) { nodeHeight = reader.getAttributeValue(i); } } b.setX(new Float(nodeX).floatValue()); b.setY(new Float(nodeY).floatValue()); b.setWidth(new Float(nodeWidth).floatValue()); b.setHeight(new Float(nodeHeight).floatValue()); _bounds.put(nodeName, b); } } } for (RootElement rootElement : def.getRootElements()) { if (rootElement instanceof Process) { Process process = (Process) rootElement; BpmnDiFactory diFactory = BpmnDiFactory.eINSTANCE; BPMNDiagram diagram = diFactory.createBPMNDiagram(); BPMNPlane plane = diFactory.createBPMNPlane(); plane.setBpmnElement(process); diagram.setPlane(plane); for (FlowElement flowElement : process.getFlowElements()) { if (flowElement instanceof FlowNode) { Bounds b = _bounds.get(flowElement.getId()); if (b != null) { BPMNShape shape = diFactory.createBPMNShape(); shape.setBpmnElement(flowElement); shape.setBounds(b); plane.getPlaneElement().add(shape); } } else if (flowElement instanceof SequenceFlow) { SequenceFlow sequenceFlow = (SequenceFlow) flowElement; BPMNEdge edge = diFactory.createBPMNEdge(); edge.setBpmnElement(flowElement); DcFactory dcFactory = DcFactory.eINSTANCE; Point point = dcFactory.createPoint(); if (sequenceFlow.getSourceRef() != null) { Bounds sourceBounds = _bounds.get(sequenceFlow.getSourceRef().getId()); point.setX(sourceBounds.getX() + (sourceBounds.getWidth() / 2)); point.setY(sourceBounds.getY() + (sourceBounds.getHeight() / 2)); } edge.getWaypoint().add(point); // List<Point> dockers = _dockers.get(sequenceFlow.getId()); // for (int i = 1; i < dockers.size() - 1; i++) { // edge.getWaypoint().add(dockers.get(i)); // } point = dcFactory.createPoint(); if (sequenceFlow.getTargetRef() != null) { Bounds targetBounds = _bounds.get(sequenceFlow.getTargetRef().getId()); point.setX(targetBounds.getX() + (targetBounds.getWidth() / 2)); point.setY(targetBounds.getY() + (targetBounds.getHeight() / 2)); } edge.getWaypoint().add(point); plane.getPlaneElement().add(edge); } } def.getDiagrams().add(diagram); } } } catch (FactoryConfigurationError e) { _logger.error("Exception adding bpmndi info: " + e.getMessage()); } catch (Exception e) { _logger.error("Exception adding bpmndi info: " + e.getMessage()); } }