List of usage examples for javax.xml.stream XMLStreamException getMessage
public String getMessage()
From source file:org.docx4j.openpackaging.parts.JaxbXmlPart.java
/** * Unmarshal XML data from the specified InputStream and return the * resulting content tree. Validation event location information may be * incomplete when using this form of the unmarshal API. * * <p>/*from w ww . j a va2 s .co m*/ * Implements <a href="#unmarshalGlobal">Unmarshal Global Root Element</a>. * * @param is * the InputStream to unmarshal XML data from * @return the newly created root object of the java content tree * * @throws JAXBException * If any unexpected errors occur while unmarshalling */ public E unmarshal(java.io.InputStream is) throws JAXBException { try { /* To avoid possible XML External Entity Injection attack, * we need to configure the processor. * * We use STAX XMLInputFactory to do that. * * createXMLStreamReader(is) is 40% slower than unmarshal(is). * * But it seems to be the best we can do ... * * org.w3c.dom.Document doc = XmlUtils.getNewDocumentBuilder().parse(is) * unmarshal(doc) * * ie DOM is 5x slower than unmarshal(is) * */ XMLInputFactory xif = XMLInputFactory.newInstance(); xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false); xif.setProperty(XMLInputFactory.SUPPORT_DTD, false); // a DTD is merely ignored, its presence doesn't cause an exception XMLStreamReader xsr = xif.createXMLStreamReader(is); Unmarshaller u = jc.createUnmarshaller(); JaxbValidationEventHandler eventHandler = new JaxbValidationEventHandler(); if (is.markSupported()) { // Only fail hard if we know we can restart eventHandler.setContinue(false); } u.setEventHandler(eventHandler); try { jaxbElement = (E) XmlUtils.unwrap(u.unmarshal(xsr)); } catch (UnmarshalException ue) { if (ue.getLinkedException() != null && ue.getLinkedException().getMessage().contains("entity")) { /* Caused by: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[10,19] Message: The entity "xxe" was referenced, but not declared. at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.next(Unknown Source) at com.sun.xml.internal.bind.v2.runtime.unmarshaller.StAXStreamConnector.bridge(Unknown Source) */ log.error(ue.getMessage(), ue); throw ue; } if (is.markSupported()) { // When reading from zip, we use a ByteArrayInputStream, // which does support this. log.info("encountered unexpected content; pre-processing"); eventHandler.setContinue(true); try { Templates mcPreprocessorXslt = JaxbValidationEventHandler.getMcPreprocessor(); is.reset(); JAXBResult result = XmlUtils.prepareJAXBResult(jc); XmlUtils.transform(new StreamSource(is), mcPreprocessorXslt, null, result); jaxbElement = (E) XmlUtils.unwrap(result.getResult()); } catch (Exception e) { throw new JAXBException("Preprocessing exception", e); } } else { log.error(ue.getMessage(), ue); log.error(".. and mark not supported"); throw ue; } } } catch (XMLStreamException e1) { log.error(e1.getMessage(), e1); throw new JAXBException(e1); } return jaxbElement; }
From source file:org.esxx.Response.java
public static void writeObject(Object object, ContentType ct, OutputStream out) throws IOException { if (object == null) { return;//from w w w . j av a 2 s.c om } // Unwrap wrapped objects object = JS.toJavaObject(object); // Convert complex types to primitive types if (object instanceof Node) { ESXX esxx = ESXX.getInstance(); if (ct.match("message/rfc822")) { try { String xml = esxx.serializeNode((Node) object); org.esxx.xmtp.XMTPParser xmtpp = new org.esxx.xmtp.XMTPParser(); javax.mail.Message msg = xmtpp.convertMessage(new StringReader(xml)); object = new ByteArrayOutputStream(); msg.writeTo(new FilterOutputStream((OutputStream) object) { @Override public void write(int b) throws IOException { if (b == '\r') { return; } else if (b == '\n') { out.write('\r'); out.write('\n'); } else { out.write(b); } } }); } catch (javax.xml.stream.XMLStreamException ex) { throw new ESXXException("Failed to serialize Node as message/rfc822:" + ex.getMessage(), ex); } catch (javax.mail.MessagingException ex) { throw new ESXXException("Failed to serialize Node as message/rfc822:" + ex.getMessage(), ex); } } else { object = esxx.serializeNode((Node) object); } } else if (object instanceof Scriptable) { if (ct.match("application/x-www-form-urlencoded")) { String cs = Parsers.getParameter(ct, "charset", "UTF-8"); object = StringUtil.encodeFormVariables(cs, (Scriptable) object); } else if (ct.match("text/csv")) { object = jsToCSV(ct, (Scriptable) object); } else { object = jsToJSON(object).toString(); } } else if (object instanceof byte[]) { object = new ByteArrayInputStream((byte[]) object); } else if (object instanceof File) { object = new FileInputStream((File) object); } // Serialize primitive types if (object instanceof ByteArrayOutputStream) { ByteArrayOutputStream bos = (ByteArrayOutputStream) object; bos.writeTo(out); } else if (object instanceof ByteBuffer) { // Write result as-is to output stream WritableByteChannel wbc = Channels.newChannel(out); ByteBuffer bb = (ByteBuffer) object; bb.rewind(); while (bb.hasRemaining()) { wbc.write(bb); } wbc.close(); } else if (object instanceof InputStream) { IO.copyStream((InputStream) object, out); } else if (object instanceof Reader) { // Write stream as-is, using the specified charset (if present) String cs = Parsers.getParameter(ct, "charset", "UTF-8"); Writer ow = new OutputStreamWriter(out, cs); IO.copyReader((Reader) object, ow); } else if (object instanceof String) { // Write string as-is, using the specified charset (if present) String cs = Parsers.getParameter(ct, "charset", "UTF-8"); Writer ow = new OutputStreamWriter(out, cs); ow.write((String) object); ow.flush(); } else if (object instanceof RenderedImage) { Iterator<ImageWriter> i = ImageIO.getImageWritersByMIMEType(ct.getBaseType()); if (!i.hasNext()) { throw new ESXXException("No ImageWriter available for " + ct.getBaseType()); } ImageWriter writer = i.next(); writer.setOutput(ImageIO.createImageOutputStream(out)); writer.write((RenderedImage) object); } else { throw new UnsupportedOperationException("Unsupported object class type: " + object.getClass()); } }
From source file:org.geowebcache.georss.StaxGeoRSSReader.java
/** * @see org.geowebcache.georss.GeoRSSReader#nextEntry() *///from ww w .ja v a2 s .com public Entry nextEntry() throws IOException { if (reader == null) { // reached EOF return null; } try { return parseEntry(); } catch (XMLStreamException e) { throw (IOException) new IOException(e.getMessage()).initCause(e); } }
From source file:org.intalio.tempo.workflow.tas.nuxeo.NuxeoStorageStrategy.java
private static OMElement buildOMElement(String xmlData) throws IOException { ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xmlData.getBytes()); XMLStreamReader parser;/*w w w . ja v a 2s . c o m*/ try { parser = XMLInputFactory.newInstance().createXMLStreamReader(byteArrayInputStream); } catch (XMLStreamException e) { throw new IOException(e.getMessage()); } StAXOMBuilder builder = new StAXOMBuilder(parser); return builder.getDocumentElement(); }
From source file:org.jaggeryjs.hostobjects.ws.WSRequestCallback.java
public void onMessage(MessageContext messageContext) { try {//from w ww.jav a2 s. c om this.wsrequest.updateResponse(messageContext.getEnvelope().getBody().getFirstElement()); this.wsrequest.readyState = 4; if (this.wsrequest.onReadyStateChangeFunction != null) { Context cx = RhinoEngine.enterContext(this.wsrequest.context.getFactory()); this.wsrequest.onReadyStateChangeFunction.call(cx, wsrequest, wsrequest, new Object[0]); RhinoEngine.exitContext(); } } catch (XMLStreamException e) { log.error(e.getMessage(), e); } }
From source file:org.jaggeryjs.hostobjects.ws.WSRequestHostObject.java
private static void setCommonProperties(Context cx, WSRequestHostObject wsRequest, Object[] args, Scriptable optionsObj) throws ScriptException { wsRequest.responseText = null;//ww w . j a va 2 s .c o m wsRequest.responseXML = null; wsRequest.error = null; wsRequest.readyState = 1; // Calling onreadystatechange function if (wsRequest.onReadyStateChangeFunction != null) { wsRequest.onReadyStateChangeFunction.call(cx, wsRequest, wsRequest, new Object[0]); } Options options = wsRequest.sender.getOptions(); if (options == null) { options = new Options(); } int timeout = 60000; if (optionsObj != null) { Object mepObject = optionsObj.get("mep", optionsObj); if (mepObject != null && !(mepObject instanceof Undefined) && !(mepObject instanceof UniqueTag)) { String mepValue = mepObject.toString(); if (IN_OUT.equalsIgnoreCase(mepValue) || IN_ONLY.equalsIgnoreCase(mepValue)) { wsRequest.mep = mepValue; } else { throw new ScriptException("Invalid value for mep. Supported values are in-out and in-only"); } } Object timeoutObject = optionsObj.get("timeout", optionsObj); if (timeoutObject != null && !(timeoutObject instanceof Undefined) && !(timeoutObject instanceof UniqueTag)) { timeout = Integer.parseInt(timeoutObject.toString()); } Object soapHeadersObject = optionsObj.get("SOAPHeaders", optionsObj); if (soapHeadersObject != null && !(soapHeadersObject instanceof Undefined) && !(soapHeadersObject instanceof UniqueTag) && soapHeadersObject instanceof Scriptable) { wsRequest.soapHeaders = (Scriptable) soapHeadersObject; } Object httpHeadersObject = optionsObj.get("HTTPHeaders", optionsObj); if (httpHeadersObject != null && !(httpHeadersObject instanceof Undefined) && !(httpHeadersObject instanceof UniqueTag) && httpHeadersObject instanceof Scriptable) { wsRequest.httpHeaders = (Scriptable) httpHeadersObject; } Object rampartConfigObject = optionsObj.get("rampart", optionsObj); if (rampartConfigObject != null && !(rampartConfigObject instanceof Undefined) && !(rampartConfigObject instanceof UniqueTag) && rampartConfigObject instanceof NativeObject) { wsRequest.rampartConfig = (NativeObject) rampartConfigObject; } Object policyObject = optionsObj.get("policy", optionsObj); if (policyObject != null && !(policyObject instanceof Undefined) && !(policyObject instanceof UniqueTag) && policyObject instanceof XMLObject) { wsRequest.policy = (XMLObject) policyObject; } } options.setProperty(HTTPConstants.SO_TIMEOUT, timeout); options.setProperty(HTTPConstants.CONNECTION_TIMEOUT, timeout); if (wsRequest.httpHeaders != null) { //Sets HTTPHeaders specified in options object an array of name-value json objects List<Header> httpHeaders = new ArrayList<Header>(); String msg = "Invalid declaration for HTTPHeaders property"; for (Object id : wsRequest.httpHeaders.getIds()) { if (wsRequest.httpHeaders.get((Integer) id, wsRequest.httpHeaders) instanceof NativeObject) { NativeObject headerObject = (NativeObject) wsRequest.httpHeaders.get((Integer) id, wsRequest.httpHeaders); if (headerObject.get("name", headerObject) instanceof String && headerObject.get("value", headerObject) instanceof String) { httpHeaders.add(new Header((String) headerObject.get("name", headerObject), (String) headerObject.get("value", headerObject))); } else { log.error(msg); throw new ScriptException(msg); } } else { log.error(msg); throw new ScriptException(msg); } } options.setProperty(HTTPConstants.HTTP_HEADERS, httpHeaders); } if (wsRequest.soapHeaders != null) { //Sets SOAPHeaders speficed in options object an array of name-value json objects Object soapHeaderObject; for (Object id : wsRequest.soapHeaders.getIds()) { soapHeaderObject = wsRequest.soapHeaders.get((Integer) id, wsRequest.soapHeaders); if (soapHeaderObject instanceof String) { String header = (String) soapHeaderObject; try { OMElement soapHeaderOM = AXIOMUtil.stringToOM(header); wsRequest.sender.addHeader(soapHeaderOM); } catch (XMLStreamException e) { String message = "Error creating XML from the soap header : " + header; log.error(message, e); throw new ScriptException(message, e); } } else if (soapHeaderObject instanceof XMLObject) { try { wsRequest.sender.addHeader(AXIOMUtil.stringToOM(soapHeaderObject.toString())); } catch (XMLStreamException e) { throw new ScriptException(e); } } else if (soapHeaderObject instanceof NativeObject) { NativeObject soapHeader = (NativeObject) soapHeaderObject; String uri; String localName; if (soapHeader.get("qName", soapHeader) instanceof QName) { QName qName = (QName) soapHeader.get("qName", soapHeader); uri = (String) qName.getNamespaceURI(); localName = (String) qName.getLocalPart(); } else { throw new ScriptException("No qName property found for the soap headers"); } if (soapHeader.get("value", soapHeader) instanceof String) { try { wsRequest.sender.addStringHeader(new QName(uri, localName), (String) soapHeader.get("value", soapHeader)); } catch (AxisFault e) { log.error(e.getMessage(), e); throw new ScriptException(e); } } else if (soapHeader.get("value", soapHeader) instanceof XMLObject) { OMNamespace omNamespace = OMAbstractFactory.getOMFactory().createOMNamespace(uri, null); SOAPHeaderBlock headerBlock = OMAbstractFactory.getSOAP12Factory() .createSOAPHeaderBlock(localName, omNamespace); try { headerBlock .addChild(AXIOMUtil.stringToOM(soapHeader.get("value", soapHeader).toString())); } catch (XMLStreamException e) { throw new ScriptException(e); } wsRequest.sender.addHeader(headerBlock); } else { throw new ScriptException("Invalid property found for the soap headers"); } } } } }
From source file:org.jaggeryjs.modules.ws.WSRequestHostObject.java
private static void setCommonProperties(Context cx, WSRequestHostObject wsRequest, Object[] args, Scriptable optionsObj) throws ScriptException { wsRequest.responseText = null;/*w w w .j av a2 s . c om*/ wsRequest.responseXML = null; wsRequest.error = null; wsRequest.readyState = 1; // Calling onreadystatechange function if (wsRequest.onReadyStateChangeFunction != null) { wsRequest.onReadyStateChangeFunction.call(cx, wsRequest, wsRequest, new Object[0]); } Options options = wsRequest.sender.getOptions(); if (options == null) { options = new Options(); } int timeout = 60000; if (optionsObj != null) { Object mepObject = optionsObj.get("mep", optionsObj); if (mepObject != null && !(mepObject instanceof Undefined) && !(mepObject instanceof UniqueTag)) { String mepValue = mepObject.toString(); if (IN_OUT.equalsIgnoreCase(mepValue) || IN_ONLY.equalsIgnoreCase(mepValue)) { wsRequest.mep = mepValue; } else { throw new ScriptException("Invalid value for mep. Supported values are in-out and in-only"); } } Object timeoutObject = optionsObj.get("timeout", optionsObj); if (timeoutObject != null && !(timeoutObject instanceof Undefined) && !(timeoutObject instanceof UniqueTag)) { timeout = Integer.parseInt(timeoutObject.toString()); } Object robustObject = optionsObj.get("robust", optionsObj); if (robustObject != null && !(robustObject instanceof Undefined) && !(robustObject instanceof UniqueTag)) { wsRequest.robust = Boolean.valueOf(robustObject.toString()); } Object soapHeadersObject = optionsObj.get("SOAPHeaders", optionsObj); if (soapHeadersObject != null && !(soapHeadersObject instanceof Undefined) && !(soapHeadersObject instanceof UniqueTag) && soapHeadersObject instanceof Scriptable) { wsRequest.soapHeaders = (Scriptable) soapHeadersObject; } Object httpHeadersObject = optionsObj.get("HTTPHeaders", optionsObj); if (httpHeadersObject != null && !(httpHeadersObject instanceof Undefined) && !(httpHeadersObject instanceof UniqueTag) && httpHeadersObject instanceof Scriptable) { wsRequest.httpHeaders = (Scriptable) httpHeadersObject; } Object rampartConfigObject = optionsObj.get("rampart", optionsObj); if (rampartConfigObject != null && !(rampartConfigObject instanceof Undefined) && !(rampartConfigObject instanceof UniqueTag) && rampartConfigObject instanceof NativeObject) { wsRequest.rampartConfig = (NativeObject) rampartConfigObject; } Object policyObject = optionsObj.get("policy", optionsObj); if (policyObject != null && !(policyObject instanceof Undefined) && !(policyObject instanceof UniqueTag) && policyObject instanceof XMLObject) { wsRequest.policy = (XMLObject) policyObject; } } options.setProperty(HTTPConstants.SO_TIMEOUT, timeout); options.setProperty(HTTPConstants.CONNECTION_TIMEOUT, timeout); if (wsRequest.httpHeaders != null) { //Sets HTTPHeaders specified in options object an array of name-value json objects List<Header> httpHeaders = new ArrayList<Header>(); String msg = "Invalid declaration for HTTPHeaders property"; for (Object id : wsRequest.httpHeaders.getIds()) { if (wsRequest.httpHeaders.get((Integer) id, wsRequest.httpHeaders) instanceof NativeObject) { NativeObject headerObject = (NativeObject) wsRequest.httpHeaders.get((Integer) id, wsRequest.httpHeaders); if (headerObject.get("name", headerObject) instanceof String && headerObject.get("value", headerObject) instanceof String) { httpHeaders.add(new Header((String) headerObject.get("name", headerObject), (String) headerObject.get("value", headerObject))); } else { log.error(msg); throw new ScriptException(msg); } } else { log.error(msg); throw new ScriptException(msg); } } options.setProperty(HTTPConstants.HTTP_HEADERS, httpHeaders); } if (wsRequest.soapHeaders != null) { //Sets SOAPHeaders speficed in options object an array of name-value json objects Object soapHeaderObject; for (Object id : wsRequest.soapHeaders.getIds()) { soapHeaderObject = wsRequest.soapHeaders.get((Integer) id, wsRequest.soapHeaders); if (soapHeaderObject instanceof String) { String header = (String) soapHeaderObject; try { OMElement soapHeaderOM = AXIOMUtil.stringToOM(header); wsRequest.sender.addHeader(soapHeaderOM); } catch (XMLStreamException e) { String message = "Error creating XML from the soap header : " + header; log.error(message, e); throw new ScriptException(message, e); } } else if (soapHeaderObject instanceof XMLObject) { try { wsRequest.sender.addHeader(AXIOMUtil.stringToOM(soapHeaderObject.toString())); } catch (XMLStreamException e) { throw new ScriptException(e); } } else if (soapHeaderObject instanceof NativeObject) { NativeObject soapHeader = (NativeObject) soapHeaderObject; String uri; String localName; Object o = ((Wrapper) soapHeader).unwrap(); if (soapHeader.get("qName", soapHeader) instanceof QName) { QName qName = (QName) soapHeader.get("qName", soapHeader); uri = (String) qName.getNamespaceURI(); localName = (String) qName.getLocalPart(); } else { throw new ScriptException("No qName property found for the soap headers"); } if (soapHeader.get("value", soapHeader) instanceof String) { try { wsRequest.sender.addStringHeader(new QName(uri, localName), (String) soapHeader.get("value", soapHeader)); } catch (AxisFault e) { log.error(e.getMessage(), e); throw new ScriptException(e); } } else if (soapHeader.get("value", soapHeader) instanceof XMLObject) { OMNamespace omNamespace = OMAbstractFactory.getOMFactory().createOMNamespace(uri, null); SOAPHeaderBlock headerBlock = OMAbstractFactory.getSOAP12Factory() .createSOAPHeaderBlock(localName, omNamespace); try { headerBlock .addChild(AXIOMUtil.stringToOM(soapHeader.get("value", soapHeader).toString())); } catch (XMLStreamException e) { throw new ScriptException(e); } wsRequest.sender.addHeader(headerBlock); } else { throw new ScriptException("Invalid property found for the soap headers"); } } } } }
From source file:org.openanzo.services.serialization.XMLBackupReader.java
/** * Parse the data within the reader, passing the results to the IRepositoryHandler * // w ww. j a v a 2 s. com * @param reader * reader containing the data * @param handler * Handler which will handle the elements within the data * @throws AnzoException */ private void parseBackup(Reader reader, final IBackupHandler handler) throws AnzoException { try { XMLStreamReader parser = XMLFactoryFinder.getXMLInputFactory().createXMLStreamReader(reader); for (int event = parser.next(); event != XMLStreamConstants.END_DOCUMENT; event = parser.next()) { switch (event) { case XMLStreamConstants.START_ELEMENT: if (SerializationConstants.namedGraph.equals(parser.getLocalName())) { graphUri = parser.getAttributeValue(null, SerializationConstants.namedGraphUri); String meta = parser.getAttributeValue(null, SerializationConstants.metadataGraphUri); String uuidString = parser.getAttributeValue(null, SerializationConstants.namedGraphUUID); namedGraphUri = Constants.valueFactory.createURI(graphUri); metaURI = Constants.valueFactory.createURI(meta); uuid = Constants.valueFactory.createURI(uuidString); revisioned = Boolean .parseBoolean(parser.getAttributeValue(null, SerializationConstants.revisioned)); } else if (SerializationConstants.revisions.equals(parser.getLocalName())) { revisions = new ArrayList<BackupRevision>(); } else if (SerializationConstants.revisionInfo.equals(parser.getLocalName())) { long revision = Long .parseLong(parser.getAttributeValue(null, SerializationConstants.revision)); Long start = Long.valueOf(parser.getAttributeValue(null, SerializationConstants.start)); String endString = parser.getAttributeValue(null, SerializationConstants.end); Long end = (endString != null) ? Long.valueOf(endString) : -1; lastModified = Constants.valueFactory .createURI(parser.getAttributeValue(null, SerializationConstants.lastModifiedBy)); revisions.add(new BackupRevision(revision, start, end, lastModified)); } else if (SerializationConstants.statement.equals(parser.getLocalName())) { if (processGraph) { if (revisioned) { String startString = parser.getAttributeValue(null, SerializationConstants.start); String endString = parser.getAttributeValue(null, SerializationConstants.end); if (startString != null) start = Long.valueOf(startString); if (endString != null) end = Long.valueOf(endString); } } } else if (SerializationConstants.statements.equals(parser.getLocalName())) { if (processGraph) { metadata = !parser.getAttributeValue(null, SerializationConstants.namedGraphUri) .equals(graphUri); } } else if (SerializationConstants.subject.equals(parser.getLocalName())) { if (processGraph) { nodeType = parser.getAttributeValue(null, SerializationConstants.subjectType); } } else if (SerializationConstants.predicate.equals(parser.getLocalName())) { } else if (SerializationConstants.object.equals(parser.getLocalName())) { if (processGraph) { nodeType = parser.getAttributeValue(null, SerializationConstants.objectType); language = parser.getAttributeValue(null, SerializationConstants.language); datatype = parser.getAttributeValue(null, SerializationConstants.dataType); } } else if (SerializationConstants.namedGraphUri.equals(parser.getLocalName())) { } break; case XMLStreamConstants.END_ELEMENT: if (SerializationConstants.namedGraph.equals(parser.getLocalName())) { graphUri = null; namedGraphUri = null; metaURI = null; uuid = null; revisioned = true; revisions = null; } else if (SerializationConstants.revisionInfo.equals(parser.getLocalName())) { } else if (SerializationConstants.revisions.equals(parser.getLocalName())) { processGraph = handler.handleNamedGraph(revisioned, namedGraphUri, metaURI, uuid, revisions); revisions = null; } else if (SerializationConstants.statement.equals(parser.getLocalName())) { if (processGraph) { handler.handleStatement(metadata, revisioned, Constants.valueFactory.createStatement(currentSubject, currentPredicate, currentObject, currentNamedGraphURI), start, end); start = null; end = null; } } else if (SerializationConstants.subject.equals(parser.getLocalName())) { if (processGraph) { if (NodeType.BNODE.name().equals(nodeType)) { currentSubject = Constants.valueFactory.createBNode(currentValue); } else { currentSubject = Constants.valueFactory.createURI(currentValue); } currentValue = null; nodeType = null; } } else if (SerializationConstants.predicate.equals(parser.getLocalName())) { if (processGraph) { currentPredicate = Constants.valueFactory.createURI(currentValue); currentValue = null; } } else if (SerializationConstants.object.equals(parser.getLocalName())) { if (processGraph) { if (NodeType.BNODE.name().equals(nodeType)) { currentObject = Constants.valueFactory.createBNode(currentValue); } else if (NodeType.URI.name().equals(nodeType)) { currentObject = Constants.valueFactory.createURI(currentValue); } else if (NodeType.LITERAL.name().equals(nodeType)) { if (currentValue == null) { currentValue = ""; } else if (Base64 .isArrayByteBase64(currentValue.getBytes(Constants.byteEncoding))) { currentValue = new String( Base64.decodeBase64(currentValue.getBytes(Constants.byteEncoding)), Constants.byteEncoding); } if (datatype != null) { currentObject = Constants.valueFactory.createLiteral(currentValue, Constants.valueFactory.createURI(datatype)); } else if (language != null) { currentObject = Constants.valueFactory.createLiteral(currentValue, language); } else { currentObject = Constants.valueFactory.createLiteral(currentValue); } } currentValue = null; nodeType = null; language = null; datatype = null; } } else if (SerializationConstants.namedGraphUri.equals(parser.getLocalName())) { if (processGraph) { currentNamedGraphURI = Constants.valueFactory.createURI(currentValue); currentValue = null; } } break; case XMLStreamConstants.CHARACTERS: if (parser.hasText()) currentValue = parser.getText(); break; case XMLStreamConstants.CDATA: if (parser.hasText()) currentValue = parser.getText(); break; } } parser.close(); } catch (XMLStreamException ex) { throw new AnzoException(ExceptionConstants.IO.READ_ERROR, ex, ex.getMessage()); } catch (UnsupportedEncodingException uee) { throw new AnzoException(ExceptionConstants.IO.ENCODING_ERROR, uee); } }
From source file:org.openanzo.services.serialization.XMLNamedGraphUpdatesReader.java
/** * Parse the data within the reader, passing the results to the IRepositoryHandler * //from w w w. j ava 2 s .c o m * @param reader * reader containing the data * @param handler * Handler which will handle the elements within the data * @throws AnzoException */ public void parseUpdates(Reader reader, final INamedGraphUpdateHandler handler) throws AnzoException { try { XMLStreamReader parser = XMLFactoryFinder.getXMLInputFactory().createXMLStreamReader(reader); for (int event = parser.next(); event != XMLStreamConstants.END_DOCUMENT; event = parser.next()) { switch (event) { case XMLStreamConstants.START_ELEMENT: if (SerializationConstants.namedGraph.equals(parser.getLocalName())) { String uri = parser.getAttributeValue(null, SerializationConstants.namedGraphUri); String uuid = parser.getAttributeValue(null, SerializationConstants.namedGraphUUID); currentNamedGraphUpdate = new NamedGraphUpdate(MemURI.create(uri)); if (uuid != null) { currentNamedGraphUpdate.setUUID(MemURI.create(uuid)); } String revision = parser.getAttributeValue(null, SerializationConstants.revision); if (revision != null) { currentNamedGraphUpdate.setRevision(Long.parseLong(revision)); } } else if (SerializationConstants.additions.equals(parser.getLocalName())) { currentStatements = currentNamedGraphUpdate.getAdditions(); } else if (SerializationConstants.metaAdditions.equals(parser.getLocalName())) { currentStatements = currentNamedGraphUpdate.getMetaAdditions(); } else if (SerializationConstants.removals.equals(parser.getLocalName())) { currentStatements = currentNamedGraphUpdate.getRemovals(); } else if (SerializationConstants.metaRemovals.equals(parser.getLocalName())) { currentStatements = currentNamedGraphUpdate.getMetaRemovals(); } else if (SerializationConstants.statement.equals(parser.getLocalName())) { } else if (SerializationConstants.subject.equals(parser.getLocalName())) { nodeType = parser.getAttributeValue(null, SerializationConstants.subjectType); } else if (SerializationConstants.predicate.equals(parser.getLocalName())) { } else if (SerializationConstants.object.equals(parser.getLocalName())) { nodeType = parser.getAttributeValue(null, SerializationConstants.objectType); language = parser.getAttributeValue(null, SerializationConstants.language); datatype = parser.getAttributeValue(null, SerializationConstants.dataType); } else if (SerializationConstants.namedGraphUri.equals(parser.getLocalName())) { } break; case XMLStreamConstants.END_ELEMENT: if (SerializationConstants.namedGraph.equals(parser.getLocalName())) { handler.handleNamedGraphUpdate(currentNamedGraphUpdate); currentNamedGraphUpdate = null; } else if (SerializationConstants.additions.equals(parser.getLocalName())) { currentStatements = null; } else if (SerializationConstants.metaAdditions.equals(parser.getLocalName())) { currentStatements = null; } else if (SerializationConstants.removals.equals(parser.getLocalName())) { currentStatements = null; } else if (SerializationConstants.metaRemovals.equals(parser.getLocalName())) { currentStatements = null; } else if (SerializationConstants.statement.equals(parser.getLocalName())) { currentStatements.add(Constants.valueFactory.createStatement(currentSubject, currentPredicate, currentObject, currentNamedGraphURI)); } else if (SerializationConstants.subject.equals(parser.getLocalName())) { if (NodeType.BNODE.name().equals(nodeType)) { currentSubject = Constants.valueFactory.createBNode(currentValue); } else { currentSubject = Constants.valueFactory.createURI(currentValue); } currentValue = null; nodeType = null; } else if (SerializationConstants.predicate.equals(parser.getLocalName())) { currentPredicate = Constants.valueFactory.createURI(currentValue); currentValue = null; } else if (SerializationConstants.object.equals(parser.getLocalName())) { if (NodeType.BNODE.name().equals(nodeType)) { currentObject = Constants.valueFactory.createBNode(currentValue); } else if (NodeType.URI.name().equals(nodeType)) { currentObject = Constants.valueFactory.createURI(currentValue); } else if (NodeType.LITERAL.name().equals(nodeType)) { if (Base64.isArrayByteBase64(currentValue.getBytes(Constants.byteEncoding))) { currentValue = new String( Base64.decodeBase64(currentValue.getBytes(Constants.byteEncoding)), Constants.byteEncoding); } if (datatype != null) { currentObject = Constants.valueFactory.createLiteral(currentValue, Constants.valueFactory.createURI(datatype)); } else if (language != null) { currentObject = Constants.valueFactory.createLiteral(currentValue, language); } else { currentObject = Constants.valueFactory.createLiteral(currentValue); } } currentValue = null; nodeType = null; language = null; datatype = null; } else if (SerializationConstants.namedGraphUri.equals(parser.getLocalName())) { currentNamedGraphURI = Constants.valueFactory.createURI(currentValue); currentValue = null; } break; case XMLStreamConstants.CHARACTERS: currentValue = parser.getText(); break; case XMLStreamConstants.CDATA: currentValue = parser.getText(); break; } } parser.close(); } catch (XMLStreamException ex) { throw new AnzoException(ExceptionConstants.IO.READ_ERROR, ex, ex.getMessage()); } catch (UnsupportedEncodingException uee) { throw new AnzoException(ExceptionConstants.IO.ENCODING_ERROR, uee); } }
From source file:org.openanzo.services.serialization.XMLUpdatesReader.java
/** * Parse the data within the reader, passing the results to the IRepositoryHandler * // w w w. j a v a 2 s . com * @param reader * reader containing the data * @param handler * Handler which will handle the elements within the data * @throws AnzoException */ private void parseUpdateTransactions(Reader reader, final IUpdatesHandler handler) throws AnzoException { try { XMLStreamReader parser = XMLFactoryFinder.getXMLInputFactory().createXMLStreamReader(reader); for (int event = parser.next(); event != XMLStreamConstants.END_DOCUMENT; event = parser.next()) { switch (event) { case XMLStreamConstants.START_ELEMENT: if (SerializationConstants.transaction.equals(parser.getLocalName())) { currentTransaction = parseTransaction(parser); } else if (SerializationConstants.transactionContext.equals(parser.getLocalName())) { currentStatements = currentTransaction.getTransactionContext(); } else if (SerializationConstants.preconditions.equals(parser.getLocalName())) { currentStatements = new ArrayList<Statement>(); } else if (SerializationConstants.namedGraphUpdates.equals(parser.getLocalName())) { currentValue = null; } else if (SerializationConstants.namedGraph.equals(parser.getLocalName())) { String uri = parser.getAttributeValue(null, SerializationConstants.namedGraphUri); String uuid = parser.getAttributeValue(null, SerializationConstants.namedGraphUUID); currentNamedGraphUpdate = new NamedGraphUpdate(MemURI.create(uri)); if (uuid != null) { currentNamedGraphUpdate.setUUID(MemURI.create(uuid)); } String revision = parser.getAttributeValue(null, SerializationConstants.revision); if (revision != null) { currentNamedGraphUpdate.setRevision(Long.parseLong(revision)); } } else if (SerializationConstants.additions.equals(parser.getLocalName())) { currentStatements = currentNamedGraphUpdate.getAdditions(); } else if (SerializationConstants.metaAdditions.equals(parser.getLocalName())) { currentStatements = currentNamedGraphUpdate.getMetaAdditions(); } else if (SerializationConstants.removals.equals(parser.getLocalName())) { currentStatements = currentNamedGraphUpdate.getRemovals(); } else if (SerializationConstants.metaRemovals.equals(parser.getLocalName())) { currentStatements = currentNamedGraphUpdate.getMetaRemovals(); } else if (SerializationConstants.statement.equals(parser.getLocalName())) { } else if (SerializationConstants.subject.equals(parser.getLocalName())) { nodeType = parser.getAttributeValue(null, SerializationConstants.subjectType); } else if (SerializationConstants.predicate.equals(parser.getLocalName())) { } else if (SerializationConstants.object.equals(parser.getLocalName())) { nodeType = parser.getAttributeValue(null, SerializationConstants.objectType); language = parser.getAttributeValue(null, SerializationConstants.language); datatype = parser.getAttributeValue(null, SerializationConstants.dataType); } else if (SerializationConstants.namedGraphUri.equals(parser.getLocalName())) { } else if (SerializationConstants.errorResult.equals(parser.getLocalName())) { String errorCodeStr = parser.getAttributeValue(null, SerializationConstants.errorCode); errorCode = Long.parseLong(errorCodeStr); errorArgs = new ArrayList<String>(); } else if (SerializationConstants.errorMessageArg.equals(parser.getLocalName())) { } break; case XMLStreamConstants.END_ELEMENT: if (SerializationConstants.transaction.equals(parser.getLocalName())) { handler.handleTransaction(currentTransaction); currentTransaction = null; } else if (SerializationConstants.transactionContext.equals(parser.getLocalName())) { currentStatements = null; } else if (SerializationConstants.preconditions.equals(parser.getLocalName())) { currentTransaction.getPreconditions() .addAll(CommonSerializationUtils.parsePreconditionStatements(currentStatements)); } else if (SerializationConstants.namedGraphUpdates.equals(parser.getLocalName())) { if (currentValue != null) currentTransaction.getUpdatedNamedGraphRevisions() .putAll(CommonSerializationUtils.readNamedGraphRevisions(currentValue)); } else if (SerializationConstants.namedGraph.equals(parser.getLocalName())) { currentTransaction.addNamedGraphUpdate(currentNamedGraphUpdate); } else if (SerializationConstants.additions.equals(parser.getLocalName())) { currentStatements = null; } else if (SerializationConstants.metaAdditions.equals(parser.getLocalName())) { currentStatements = null; } else if (SerializationConstants.removals.equals(parser.getLocalName())) { currentStatements = null; } else if (SerializationConstants.metaRemovals.equals(parser.getLocalName())) { currentStatements = null; } else if (SerializationConstants.statement.equals(parser.getLocalName())) { currentStatements.add(Constants.valueFactory.createStatement(currentSubject, currentPredicate, currentObject, currentNamedGraphURI)); } else if (SerializationConstants.subject.equals(parser.getLocalName())) { if (NodeType.BNODE.name().equals(nodeType)) { currentSubject = Constants.valueFactory.createBNode(currentValue); } else { currentSubject = Constants.valueFactory.createURI(currentValue); } currentValue = null; nodeType = null; } else if (SerializationConstants.predicate.equals(parser.getLocalName())) { currentPredicate = Constants.valueFactory.createURI(currentValue); currentValue = null; } else if (SerializationConstants.object.equals(parser.getLocalName())) { if (NodeType.BNODE.name().equals(nodeType)) { currentObject = Constants.valueFactory.createBNode(currentValue); } else if (NodeType.URI.name().equals(nodeType)) { currentObject = Constants.valueFactory.createURI(currentValue); } else if (NodeType.LITERAL.name().equals(nodeType)) { if (currentValue == null) { currentValue = ""; } else if (Base64.isArrayByteBase64(currentValue.getBytes(Constants.byteEncoding))) { currentValue = new String( Base64.decodeBase64(currentValue.getBytes(Constants.byteEncoding)), Constants.byteEncoding); } if (datatype != null) { currentObject = Constants.valueFactory.createLiteral(currentValue, Constants.valueFactory.createURI(datatype)); } else if (language != null) { currentObject = Constants.valueFactory.createLiteral(currentValue, language); } else { currentObject = Constants.valueFactory.createLiteral(currentValue); } } currentValue = null; nodeType = null; language = null; datatype = null; } else if (SerializationConstants.namedGraphUri.equals(parser.getLocalName())) { currentNamedGraphURI = Constants.valueFactory.createURI(currentValue); currentValue = null; } else if (SerializationConstants.errorResult.equals(parser.getLocalName())) { currentTransaction.getErrors() .add(new AnzoException(errorCode, errorArgs.toArray(new String[0]))); errorCode = 0; errorArgs = null; } else if (SerializationConstants.errorMessageArg.equals(parser.getLocalName())) { errorArgs.add(currentValue); currentValue = null; } break; case XMLStreamConstants.CHARACTERS: if (parser.hasText()) currentValue = parser.getText(); break; case XMLStreamConstants.CDATA: if (parser.hasText()) currentValue = parser.getText(); break; } } parser.close(); } catch (XMLStreamException ex) { throw new AnzoException(ExceptionConstants.IO.READ_ERROR, ex, ex.getMessage()); } catch (UnsupportedEncodingException uee) { throw new AnzoException(ExceptionConstants.IO.ENCODING_ERROR, uee); } }