1 package org.objectweb.celtix.helpers; 2 3 import java.io.*; 4 import java.util.*; 5 import java.util.logging.Level ; 6 import java.util.logging.Logger ; 7 8 import javax.wsdl.Definition; 9 import javax.xml.namespace.QName ; 10 import javax.xml.parsers.DocumentBuilder ; 11 import javax.xml.parsers.DocumentBuilderFactory ; 12 import javax.xml.transform.OutputKeys ; 13 import javax.xml.transform.Transformer ; 14 import javax.xml.transform.TransformerConfigurationException ; 15 import javax.xml.transform.TransformerFactory ; 16 import javax.xml.transform.dom.DOMSource ; 17 import javax.xml.transform.stream.StreamResult ; 18 19 import org.w3c.dom.*; 20 import org.xml.sax.SAXException ; 21 22 import org.objectweb.celtix.common.logging.LogUtils; 23 24 25 public class XMLUtils { 26 27 private static final Logger LOG = LogUtils.getL7dLogger(XMLUtils.class); 28 private final DocumentBuilderFactory parserFactory; 29 private DocumentBuilder parser; 30 private final TransformerFactory transformerFactory; 31 private String omitXmlDecl = "no"; 32 private String charset = "utf-8"; 33 private int indent = -1; 34 35 public XMLUtils() { 36 parserFactory = DocumentBuilderFactory.newInstance(); 37 parserFactory.setNamespaceAware(true); 38 39 transformerFactory = TransformerFactory.newInstance(); 40 } 41 42 private Transformer newTransformer() throws TransformerConfigurationException { 43 return transformerFactory.newTransformer(); 44 } 45 46 private DocumentBuilder getParser() { 47 if (parser == null) { 48 try { 49 parser = parserFactory.newDocumentBuilder(); 50 } catch (javax.xml.parsers.ParserConfigurationException e) { 51 LOG.log(Level.SEVERE, "NEW_DOCUMENT_BUILDER_EXCEPTION_MSG"); 52 } 53 } 54 return parser; 55 } 56 57 public Document parse(InputStream in) throws SAXException , IOException { 58 if (in == null && LOG.isLoggable(Level.FINE)) { 59 LOG.fine("XMLUtils trying to parse a null inputstream"); 60 } 61 return getParser().parse(in); 62 } 63 64 public Document parse(String in) throws SAXException , IOException { 65 return parse(in.getBytes()); 66 } 67 68 public Document parse(byte[] in) throws SAXException , IOException { 69 if (in == null && LOG.isLoggable(Level.FINE)) { 70 LOG.fine("XMLUtils trying to parse a null bytes"); 71 } 72 return getParser().parse(new ByteArrayInputStream(in)); 73 } 74 75 public Document newDocument() { 76 return getParser().newDocument(); 77 } 78 79 public void setOmitXmlDecl(String value) { 80 this.omitXmlDecl = value; 81 } 82 83 public void setCharsetEncoding(String value) { 84 this.charset = value; 85 } 86 87 public void setIndention(int i) { 88 this.indent = i; 89 } 90 91 private boolean indent() { 92 return this.indent != -1; 93 } 94 95 public void writeTo(Node node, OutputStream os) { 96 try { 97 Transformer it = newTransformer(); 98 99 it.setOutputProperty(OutputKeys.METHOD, "xml"); 100 if (indent()) { 101 it.setOutputProperty(OutputKeys.INDENT, "yes"); 102 it.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", 103 Integer.toString(this.indent)); 104 } 105 it.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, omitXmlDecl); 106 it.setOutputProperty(OutputKeys.ENCODING, charset); 107 it.transform(new DOMSource (node), new StreamResult (os)); 108 } catch (Exception e) { 109 e.printStackTrace(); 110 } 111 } 112 113 public String toString(Node node) { 114 ByteArrayOutputStream out = new ByteArrayOutputStream(); 115 writeTo(node, out); 116 return out.toString(); 117 } 118 119 public void printDOM(Node node) { 120 printDOM("", node); 121 } 122 123 public void printDOM(String words, Node node) { 124 System.out.println(words); 125 System.out.println(toString(node)); 126 } 127 128 public Attr getAttribute(Element el, String attrName) { 129 return el.getAttributeNode(attrName); 130 } 131 132 public void replaceAttribute(Element element, String attr, String value) { 133 if (element.hasAttribute(attr)) { 134 element.removeAttribute(attr); 135 } 136 element.setAttribute(attr, value); 137 } 138 139 public boolean hasAttribute(Element element, String value) { 140 NamedNodeMap attributes = element.getAttributes(); 141 for (int i = 0; i < attributes.getLength(); i++) { 142 Node node = attributes.item(i); 143 if (value.equals(node.getNodeValue())) { 144 return true; 145 } 146 } 147 return false; 148 } 149 150 public static void printAttributes(Element element) { 151 NamedNodeMap attributes = element.getAttributes(); 152 for (int i = 0; i < attributes.getLength(); i++) { 153 Node node = attributes.item(i); 154 System.err.println("## prefix=" + node.getPrefix() + " localname:" 155 + node.getLocalName() + " value=" + node.getNodeValue()); 156 } 157 } 158 159 public QName getNamespace(Map namespaces, String str, String defaultNamespace) { 160 String prefix = null; 161 String localName = null; 162 163 StringTokenizer tokenizer = new StringTokenizer(str, ":"); 164 if (tokenizer.countTokens() == 2) { 165 prefix = tokenizer.nextToken(); 166 localName = tokenizer.nextToken(); 167 } else if (tokenizer.countTokens() == 1) { 168 localName = tokenizer.nextToken(); 169 } 170 171 String namespceURI = defaultNamespace; 172 if (prefix != null) { 173 namespceURI = (String )namespaces.get(prefix); 174 } 175 return new QName (namespceURI, localName); 176 } 177 178 public void generateXMLFile(Element element, Writer writer) { 179 try { 180 Transformer it = newTransformer(); 181 182 it.setOutputProperty(OutputKeys.METHOD, "xml"); 183 it.setOutputProperty(OutputKeys.INDENT, "yes"); 184 it.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); 185 it.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); 186 it.transform(new DOMSource (element), new StreamResult (writer)); 187 } catch (Exception e) { 188 e.printStackTrace(); 189 } 190 } 191 192 public Element createElementNS(Node node, QName name) { 193 return createElementNS(node.getOwnerDocument(), name.getNamespaceURI(), name.getLocalPart()); 194 } 195 196 public Element createElementNS(Document root, QName name) { 197 return createElementNS(root, name.getNamespaceURI(), name.getLocalPart()); 198 } 199 200 public Element createElementNS(Document root, String namespaceURI, String qualifiedName) { 201 return root.createElementNS(namespaceURI, qualifiedName); 202 } 203 204 public Text createTextNode(Document root, String data) { 205 return root.createTextNode(data); 206 } 207 208 public Text createTextNode(Node node, String data) { 209 return createTextNode(node.getOwnerDocument(), data); 210 } 211 212 public void removeContents(Node node) { 213 NodeList list = node.getChildNodes(); 214 for (int i = 0; i < list.getLength(); i++) { 215 Node entry = list.item(i); 216 node.removeChild(entry); 217 } 218 } 219 220 public String writeQName(Definition def, QName qname) { 221 return def.getPrefix(qname.getNamespaceURI()) + ":" + qname.getLocalPart(); 222 } 223 } 224 | Popular Tags |