List of usage examples for javax.xml.stream XMLStreamException XMLStreamException
public XMLStreamException(Throwable th)
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 ww . j a va 2 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.intermine.pathquery.PathQueryBinding.java
/** * Create XML for the constraints in a PathQuery. */// w w w . j a v a 2s. co m private void marshalPathQueryConstraints(PathQuery query, XMLStreamWriter writer, boolean hasMultipleConstraints) throws XMLStreamException { for (Map.Entry<PathConstraint, String> constraint : query.getConstraints().entrySet()) { boolean emptyElement = true; if (constraint.getKey() instanceof PathConstraintMultiValue) { emptyElement = false; } if (emptyElement) { writer.writeEmptyElement("constraint"); } else { writer.writeStartElement("constraint"); } writer.writeAttribute("path", constraint.getKey().getPath()); if ((constraint.getValue() != null) && (hasMultipleConstraints)) { writer.writeAttribute("code", constraint.getValue()); } doAdditionalConstraintStuff(query, constraint.getKey(), writer); if (constraint.getKey() instanceof PathConstraintAttribute) { writer.writeAttribute("op", "" + constraint.getKey().getOp()); String outputValue = ((PathConstraintAttribute) constraint.getKey()).getValue(); writer.writeAttribute("value", "" + outputValue); } else if (constraint.getKey() instanceof PathConstraintNull) { writer.writeAttribute("op", "" + constraint.getKey().getOp()); } else if (constraint.getKey() instanceof PathConstraintSubclass) { writer.writeAttribute("type", ((PathConstraintSubclass) constraint.getKey()).getType()); } else if (constraint.getKey() instanceof PathConstraintBag) { writer.writeAttribute("op", "" + constraint.getKey().getOp()); writer.writeAttribute("value", ((PathConstraintBag) constraint.getKey()).getBag()); } else if (constraint.getKey() instanceof PathConstraintIds) { writer.writeAttribute("op", "" + constraint.getKey().getOp()); StringBuilder sb = new StringBuilder(); boolean needComma = false; for (Integer id : ((PathConstraintIds) constraint.getKey()).getIds()) { if (needComma) { sb.append(", "); } needComma = true; sb.append("" + id); } writer.writeAttribute("ids", sb.toString()); } else if (constraint.getKey() instanceof PathConstraintMultiValue) { // Includes PathConstraintRange, which is serialised in the exact same manner. writer.writeAttribute("op", "" + constraint.getKey().getOp()); for (String value : ((PathConstraintMultiValue) constraint.getKey()).getValues()) { if (value == null) { writer.writeStartElement("nullValue"); writer.writeEndElement(); } else { if (!value.equals(value.trim())) { throw new XMLStreamException("Value in MultiValue starts or ends with " + "whitespace - this query cannot be represented in XML"); } writer.writeStartElement("value"); writer.writeCharacters(value); writer.writeEndElement(); } } } else if (constraint.getKey() instanceof PathConstraintLoop) { writer.writeAttribute("op", "" + constraint.getKey().getOp()); writer.writeAttribute("loopPath", ((PathConstraintLoop) constraint.getKey()).getLoopPath()); } else if (constraint.getKey() instanceof PathConstraintLookup) { writer.writeAttribute("op", "" + constraint.getKey().getOp()); writer.writeAttribute("value", ((PathConstraintLookup) constraint.getKey()).getValue()); String extraValue = ((PathConstraintLookup) constraint.getKey()).getExtraValue(); if (extraValue != null) { writer.writeAttribute("extraValue", extraValue); } } else { throw new IllegalStateException( "Unrecognised constraint type " + constraint.getKey().getClass().getName()); } if (!emptyElement) { writer.writeEndElement(); } } }
From source file:org.jmecsoftware.plugins.tests.utils.StaxParser.java
public void parse(File xmlFile) throws XMLStreamException { FileInputStream input = null; try {/* www.j a va2 s . c om*/ input = new FileInputStream(xmlFile); parse(input); } catch (FileNotFoundException e) { throw new XMLStreamException(e); } finally { IOUtils.closeQuietly(input); } }
From source file:org.jmecsoftware.plugins.tests.utils.StaxParser.java
public void parse(Reader xmlReader) throws XMLStreamException { if (isoControlCharsAwareParser) { throw new XMLStreamException("Method call not supported when isoControlCharsAwareParser=true"); }//from www. ja v a 2s.c o m parse(inf.rootElementCursor(xmlReader)); }
From source file:org.jmecsoftware.plugins.tests.utils.StaxParser.java
public void parse(URL xmlUrl) throws XMLStreamException { try {//from w w w . j av a 2 s .co m parse(xmlUrl.openStream()); } catch (IOException e) { throw new XMLStreamException(e); } }
From source file:org.kohsuke.maven.rewrite.XmlPatcher.java
/** * {@inheritDoc}/* w w w .j a v a2s. c o m*/ */ public XMLEvent nextTag() throws XMLStreamException { while (hasNext()) { XMLEvent e = nextEvent(); if (e.isCharacters() && !((Characters) e).isWhiteSpace()) { throw new XMLStreamException("Unexpected text"); } if (e.isStartElement() || e.isEndElement()) { return e; } } throw new XMLStreamException("Unexpected end of Document"); }
From source file:org.mule.module.xml.util.XMLUtils.java
/** * Returns an XMLStreamReader for an object of unknown type if possible. * @return null if no XMLStreamReader can be created for the object type * @throws XMLStreamException/* w w w . j ava 2 s. co m*/ */ public static javax.xml.stream.XMLStreamReader toXMLStreamReader(javax.xml.stream.XMLInputFactory factory, Object obj) throws XMLStreamException { if (obj instanceof javax.xml.stream.XMLStreamReader) { return (javax.xml.stream.XMLStreamReader) obj; } else if (obj instanceof org.mule.module.xml.stax.StaxSource) { return ((org.mule.module.xml.stax.StaxSource) obj).getXMLStreamReader(); } else if (obj instanceof javax.xml.transform.Source) { return factory.createXMLStreamReader((javax.xml.transform.Source) obj); } else if (obj instanceof org.xml.sax.InputSource) { return factory.createXMLStreamReader(((org.xml.sax.InputSource) obj).getByteStream()); } else if (obj instanceof org.w3c.dom.Document) { return factory.createXMLStreamReader(new javax.xml.transform.dom.DOMSource((org.w3c.dom.Document) obj)); } else if (obj instanceof org.dom4j.Document) { return factory.createXMLStreamReader(new org.dom4j.io.DocumentSource((org.dom4j.Document) obj)); } else if (obj instanceof java.io.InputStream) { final InputStream is = (java.io.InputStream) obj; XMLStreamReader xsr = factory.createXMLStreamReader(is); return new DelegateXMLStreamReader(xsr) { @Override public void close() throws XMLStreamException { super.close(); try { is.close(); } catch (IOException e) { throw new XMLStreamException(e); } } }; } else if (obj instanceof String) { return factory.createXMLStreamReader(new StringReader((String) obj)); } else if (obj instanceof byte[]) { // TODO Handle encoding/charset? return factory.createXMLStreamReader(new ByteArrayInputStream((byte[]) obj)); } else { return null; } }
From source file:org.mule.modules.jive.api.xml.XmlMapper.java
/**Maps an xml from a {@link Reader} to a {@link Map}. * @param reader The {@link Reader} with the xml data * @return The map with the entity data//from w ww . ja v a 2s . com * */ @SuppressWarnings("unchecked") public final Map<String, Object> xml2map(final Reader reader) { final Map<String, Object> ret = new HashMap<String, Object>(); final Stack<Map<String, Object>> maps = new Stack<Map<String, Object>>(); Map<String, Object> current = ret; try { final XMLStreamReader r = xmlInputFactory.createXMLStreamReader(reader); StringBuilder lastText = new StringBuilder(); String currentElement = null; int returnCount = 0; while (r.hasNext()) { final int eventType = r.next(); if (eventType == CHARACTERS || eventType == CDATA || eventType == SPACE || eventType == ENTITY_REFERENCE) { lastText.append(r.getText()); } else if (eventType == END_DOCUMENT) { break; } else if (eventType == START_ELEMENT) { if (currentElement != null) { maps.push(current); final Map<String, Object> map = new HashMap<String, Object>(); if (StringUtils.startsWith(currentElement, "return")) { currentElement = currentElement + "--" + String.valueOf(returnCount); returnCount++; } current.put(currentElement, map); current = map; } currentElement = r.getLocalName(); } else if (eventType == END_ELEMENT) { if (currentElement == null) { current = maps.pop(); } else { current.put(currentElement, lastText.toString().trim()); currentElement = null; lastText = new StringBuilder(); } } else { throw new XMLStreamException("Unexpected event type " + eventType); } } final Object obj = ret.get(ret.keySet().iterator().next()); if (obj instanceof String) { Map<String, Object> responseTag = new HashMap<String, Object>(); responseTag.put("response", ret.keySet().iterator().next().toString()); return responseTag; } else { final Map<String, Object> returnXMLElement = (Map<String, Object>) ret .get(ret.keySet().iterator().next()); if (returnXMLElement.keySet().contains("return--1")) { return returnXMLElement; } return (Map<String, Object>) returnXMLElement.get("return--0"); } } catch (XMLStreamException e) { throw new UnhandledException(e); } }
From source file:org.mule.transport.sap.transformer.XmlToJcoFunctionTransformer.java
public JCoFunction transform(InputStream stream, String encoding) throws XMLStreamException { XMLStreamReader reader = null; XMLInputFactory factory = XMLInputFactory.newInstance(); String functionName = null;/*from w w w .j ava 2 s .c o m*/ String tableName = null; String structureName = null; String rowId = null; String fieldName = null; String value = null; JCoFunction function = null; try { reader = factory.createXMLStreamReader(stream); String localName = null; while (reader.hasNext()) { int eventType = reader.next(); if (eventType == XMLStreamReader.START_DOCUMENT) { // find START_DOCUMENT } else if (eventType == XMLStreamReader.START_ELEMENT) { // find START_ELEMENT localName = reader.getLocalName(); logger.debug("START ELEMENT IS FOUND"); logger.debug("start localName = " + localName); if (localName.equals(MessageConstants.JCO)) { functionName = getAttributeValue(MessageConstants.JCO_ATTR_NAME, reader); try { function = this.connector.getAdapter().getFunction(functionName); } catch (JCoException e) { throw new XMLStreamException(e); } logger.debug("function name:" + functionName); } else if (functionName != null) { if (localName.equals(MessageConstants.IMPORT)) { //recordType = IMPORT; push(function.getImportParameterList()); } else if (localName.equals(MessageConstants.EXPORT)) { //recordType = EXPORT; push(function.getExportParameterList()); } else if (localName.equals(MessageConstants.TABLES)) { //recordType = TABLES; tableName = null; push(function.getTableParameterList()); } else if (localName.equals(MessageConstants.TABLE)) { if (tableName != null) { pop(); } tableName = getAttributeValue(MessageConstants.TABLE_ATTR_NAME, reader); logger.debug("tableName = " + tableName); push(this.record.getTable(tableName)); } else if (localName.equals(MessageConstants.STRUCTURE)) { structureName = getAttributeValue(MessageConstants.STRUCTURE_ATTR_NAME, reader); push(this.record.getStructure(structureName)); } else if (localName.equals(MessageConstants.ROW)) { rowId = getAttributeValue(MessageConstants.ROW_ATTR_ID, reader); logger.debug("rowId = " + rowId); if (this.record instanceof JCoTable) { ((JCoTable) this.record).appendRow(); } } else if (localName.equals(MessageConstants.FIELD)) { fieldName = getAttributeValue(MessageConstants.STRUCTURE_ATTR_NAME, reader); value = reader.getElementText().trim(); // get an element value logger.debug("FieldName = " + fieldName); logger.debug("value = " + value); this.record.setValue(fieldName, value); } } } else if (eventType == XMLStreamReader.END_DOCUMENT) { // find END_DOCUMENT logger.debug("END DOCUMENT IS FOUND"); } else if (eventType == XMLStreamReader.END_ELEMENT) { logger.debug("END ELEMENT IS FOUND"); logger.debug("end localName = " + localName); // find END_ELEMENT if (localName.equals(MessageConstants.IMPORT) || localName.equals(MessageConstants.EXPORT) || localName.equals(MessageConstants.TABLES) || localName.equals(MessageConstants.TABLE) || localName.equals(MessageConstants.STRUCTURE)) { pop(); } } } } catch (Exception e) { logger.fatal(e); throw new TransformerException(this, e); } finally { if (reader != null) { try { reader.close(); } catch (XMLStreamException ex) { } } if (stream != null) { try { stream.close(); } catch (IOException ex) { } } logger.debug("\n" + function.getImportParameterList().toXML()); logger.debug("\n" + function.getExportParameterList().toXML()); logger.debug("\n" + function.getTableParameterList().toXML()); return function; } }
From source file:org.orbisgis.core.layerModel.mapcatalog.Workspace.java
private int parsePublishResponse(XMLStreamReader parser) throws XMLStreamException { List<String> hierarchy = new ArrayList<String>(); for (int event = parser.next(); event != XMLStreamConstants.END_DOCUMENT; event = parser.next()) { // For each XML elements switch (event) { case XMLStreamConstants.START_ELEMENT: hierarchy.add(parser.getLocalName()); // Parse attributes if (RemoteCommons.endsWith(hierarchy, "context")) { for (int attributeId = 0; attributeId < parser.getAttributeCount(); attributeId++) { String attributeName = parser.getAttributeLocalName(attributeId); if (attributeName.equals("id")) { return Integer.parseInt(parser.getAttributeValue(attributeId)); }//from w ww . j a v a 2s . c o m } } break; case XMLStreamConstants.END_ELEMENT: hierarchy.remove(hierarchy.size() - 1); break; } } throw new XMLStreamException("Bad response on publishing a map context"); }