/* * Copyright 2010 NCHOVY * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.krakenapps.util; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.StringWriter; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.TransformerFactoryConfigurationError; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathExpression; import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.xml.sax.SAXException; public class XmlUtil { private static Logger logger = LoggerFactory.getLogger(XmlUtil.class); public static Document read(InputStream inputStream) { try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); return builder.parse(inputStream); } catch (ParserConfigurationException e) { logger.error(e.getMessage()); } catch (SAXException e) { logger.error(e.getMessage()); } catch (IOException e) { logger.error(e.getMessage()); } finally { if (inputStream != null) try { inputStream.close(); } catch (IOException e) { // ignore. } } return null; } public static Document parse(String buffer) { try { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder(); InputStream is = new ByteArrayInputStream(buffer.getBytes("utf-8")); return builder.parse(is); } catch (ParserConfigurationException e) { logger.error(e.getMessage()); } catch (SAXException e) { logger.error(e.getMessage()); } catch (IOException e) { logger.error(e.getMessage()); } return null; } public static Document parse(byte[] buffer) { try { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder(); InputStream is = new ByteArrayInputStream(buffer); return builder.parse(is); } catch (ParserConfigurationException e) { logger.error(e.getMessage()); } catch (SAXException e) { logger.error(e.getMessage()); } catch (IOException e) { logger.error(e.getMessage()); } return null; } public static Document newDocument() { try { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder(); return builder.newDocument(); } catch (ParserConfigurationException e) { logger.error(e.getMessage()); } return null; } public static XPathExpression compileXPath(String expression) { try { XPathFactory xPathFactory = XPathFactory.newInstance(); XPath xPath = xPathFactory.newXPath(); return xPath.compile(expression); } catch (XPathExpressionException e) { logger.error("Invalid XPath expression [" + expression + "]"); return null; } } public static String getAttribute(Node node, String attributeName) { Node attribute = node.getAttributes().getNamedItem(attributeName); if (attribute == null) { return null; } return attribute.getTextContent(); } public static String toXmlString(Document document) { StringWriter sw = new StringWriter(); StreamResult result = new StreamResult(sw); Transformer transformer; try { transformer = TransformerFactory.newInstance().newTransformer(); transformer.transform(new DOMSource(document.getLastChild()), result); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerFactoryConfigurationError e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } return sw.toString(); } }