001: package org.objectweb.celtix.helpers;
002:
003: import java.io.*;
004: import java.util.*;
005: import java.util.logging.Level;
006: import java.util.logging.Logger;
007:
008: import javax.wsdl.Definition;
009: import javax.xml.namespace.QName;
010: import javax.xml.parsers.DocumentBuilder;
011: import javax.xml.parsers.DocumentBuilderFactory;
012: import javax.xml.transform.OutputKeys;
013: import javax.xml.transform.Transformer;
014: import javax.xml.transform.TransformerConfigurationException;
015: import javax.xml.transform.TransformerFactory;
016: import javax.xml.transform.dom.DOMSource;
017: import javax.xml.transform.stream.StreamResult;
018:
019: import org.w3c.dom.*;
020: import org.xml.sax.SAXException;
021:
022: import org.objectweb.celtix.common.logging.LogUtils;
023:
024: public class XMLUtils {
025:
026: private static final Logger LOG = LogUtils
027: .getL7dLogger(XMLUtils.class);
028: private final DocumentBuilderFactory parserFactory;
029: private DocumentBuilder parser;
030: private final TransformerFactory transformerFactory;
031: private String omitXmlDecl = "no";
032: private String charset = "utf-8";
033: private int indent = -1;
034:
035: public XMLUtils() {
036: parserFactory = DocumentBuilderFactory.newInstance();
037: parserFactory.setNamespaceAware(true);
038:
039: transformerFactory = TransformerFactory.newInstance();
040: }
041:
042: private Transformer newTransformer()
043: throws TransformerConfigurationException {
044: return transformerFactory.newTransformer();
045: }
046:
047: private DocumentBuilder getParser() {
048: if (parser == null) {
049: try {
050: parser = parserFactory.newDocumentBuilder();
051: } catch (javax.xml.parsers.ParserConfigurationException e) {
052: LOG.log(Level.SEVERE,
053: "NEW_DOCUMENT_BUILDER_EXCEPTION_MSG");
054: }
055: }
056: return parser;
057: }
058:
059: public Document parse(InputStream in) throws SAXException,
060: IOException {
061: if (in == null && LOG.isLoggable(Level.FINE)) {
062: LOG.fine("XMLUtils trying to parse a null inputstream");
063: }
064: return getParser().parse(in);
065: }
066:
067: public Document parse(String in) throws SAXException, IOException {
068: return parse(in.getBytes());
069: }
070:
071: public Document parse(byte[] in) throws SAXException, IOException {
072: if (in == null && LOG.isLoggable(Level.FINE)) {
073: LOG.fine("XMLUtils trying to parse a null bytes");
074: }
075: return getParser().parse(new ByteArrayInputStream(in));
076: }
077:
078: public Document newDocument() {
079: return getParser().newDocument();
080: }
081:
082: public void setOmitXmlDecl(String value) {
083: this .omitXmlDecl = value;
084: }
085:
086: public void setCharsetEncoding(String value) {
087: this .charset = value;
088: }
089:
090: public void setIndention(int i) {
091: this .indent = i;
092: }
093:
094: private boolean indent() {
095: return this .indent != -1;
096: }
097:
098: public void writeTo(Node node, OutputStream os) {
099: try {
100: Transformer it = newTransformer();
101:
102: it.setOutputProperty(OutputKeys.METHOD, "xml");
103: if (indent()) {
104: it.setOutputProperty(OutputKeys.INDENT, "yes");
105: it.setOutputProperty(
106: "{http://xml.apache.org/xslt}indent-amount",
107: Integer.toString(this .indent));
108: }
109: it.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,
110: omitXmlDecl);
111: it.setOutputProperty(OutputKeys.ENCODING, charset);
112: it.transform(new DOMSource(node), new StreamResult(os));
113: } catch (Exception e) {
114: e.printStackTrace();
115: }
116: }
117:
118: public String toString(Node node) {
119: ByteArrayOutputStream out = new ByteArrayOutputStream();
120: writeTo(node, out);
121: return out.toString();
122: }
123:
124: public void printDOM(Node node) {
125: printDOM("", node);
126: }
127:
128: public void printDOM(String words, Node node) {
129: System.out.println(words);
130: System.out.println(toString(node));
131: }
132:
133: public Attr getAttribute(Element el, String attrName) {
134: return el.getAttributeNode(attrName);
135: }
136:
137: public void replaceAttribute(Element element, String attr,
138: String value) {
139: if (element.hasAttribute(attr)) {
140: element.removeAttribute(attr);
141: }
142: element.setAttribute(attr, value);
143: }
144:
145: public boolean hasAttribute(Element element, String value) {
146: NamedNodeMap attributes = element.getAttributes();
147: for (int i = 0; i < attributes.getLength(); i++) {
148: Node node = attributes.item(i);
149: if (value.equals(node.getNodeValue())) {
150: return true;
151: }
152: }
153: return false;
154: }
155:
156: public static void printAttributes(Element element) {
157: NamedNodeMap attributes = element.getAttributes();
158: for (int i = 0; i < attributes.getLength(); i++) {
159: Node node = attributes.item(i);
160: System.err.println("## prefix=" + node.getPrefix()
161: + " localname:" + node.getLocalName() + " value="
162: + node.getNodeValue());
163: }
164: }
165:
166: public QName getNamespace(Map namespaces, String str,
167: String defaultNamespace) {
168: String prefix = null;
169: String localName = null;
170:
171: StringTokenizer tokenizer = new StringTokenizer(str, ":");
172: if (tokenizer.countTokens() == 2) {
173: prefix = tokenizer.nextToken();
174: localName = tokenizer.nextToken();
175: } else if (tokenizer.countTokens() == 1) {
176: localName = tokenizer.nextToken();
177: }
178:
179: String namespceURI = defaultNamespace;
180: if (prefix != null) {
181: namespceURI = (String) namespaces.get(prefix);
182: }
183: return new QName(namespceURI, localName);
184: }
185:
186: public void generateXMLFile(Element element, Writer writer) {
187: try {
188: Transformer it = newTransformer();
189:
190: it.setOutputProperty(OutputKeys.METHOD, "xml");
191: it.setOutputProperty(OutputKeys.INDENT, "yes");
192: it.setOutputProperty(
193: "{http://xml.apache.org/xslt}indent-amount", "2");
194: it.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
195: it.transform(new DOMSource(element), new StreamResult(
196: writer));
197: } catch (Exception e) {
198: e.printStackTrace();
199: }
200: }
201:
202: public Element createElementNS(Node node, QName name) {
203: return createElementNS(node.getOwnerDocument(), name
204: .getNamespaceURI(), name.getLocalPart());
205: }
206:
207: public Element createElementNS(Document root, QName name) {
208: return createElementNS(root, name.getNamespaceURI(), name
209: .getLocalPart());
210: }
211:
212: public Element createElementNS(Document root, String namespaceURI,
213: String qualifiedName) {
214: return root.createElementNS(namespaceURI, qualifiedName);
215: }
216:
217: public Text createTextNode(Document root, String data) {
218: return root.createTextNode(data);
219: }
220:
221: public Text createTextNode(Node node, String data) {
222: return createTextNode(node.getOwnerDocument(), data);
223: }
224:
225: public void removeContents(Node node) {
226: NodeList list = node.getChildNodes();
227: for (int i = 0; i < list.getLength(); i++) {
228: Node entry = list.item(i);
229: node.removeChild(entry);
230: }
231: }
232:
233: public String writeQName(Definition def, QName qname) {
234: return def.getPrefix(qname.getNamespaceURI()) + ":"
235: + qname.getLocalPart();
236: }
237: }
|