KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > axis > utils > DOM2Utils


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.jboss.axis.utils;
18
19 import org.jboss.logging.Logger;
20 import org.w3c.dom.Document JavaDoc;
21 import org.w3c.dom.Element JavaDoc;
22 import org.w3c.dom.Node JavaDoc;
23 import org.w3c.dom.Text JavaDoc;
24
25 import javax.xml.parsers.DocumentBuilder JavaDoc;
26 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
27 import javax.xml.parsers.ParserConfigurationException JavaDoc;
28 import javax.xml.soap.Name JavaDoc;
29
30 /**
31  * DOM2 utilites
32  *
33  * @author Thomas Diesler (thomas.diesler@jboss.org)
34  */

35 public final class DOM2Utils
36 {
37
38    private static Logger log = Logger.getLogger(DOM2Utils.class.getName());
39
40    // The DocumentBuilder
41
private static DocumentBuilder JavaDoc builder = getDocumentBuilder();
42
43    // Hide the constructor
44
private DOM2Utils()
45    {
46    }
47
48    /**
49     * Initialise the the DocumentBuilder
50     */

51    public static DocumentBuilder JavaDoc getDocumentBuilder()
52    {
53
54       if (builder == null)
55       {
56          try
57          {
58             DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
59             factory.setValidating(false);
60             factory.setNamespaceAware(true);
61             builder = factory.newDocumentBuilder();
62          }
63          catch (ParserConfigurationException JavaDoc e)
64          {
65             log.error(e);
66          }
67       }
68
69       return builder;
70    }
71
72    /**
73     * Create an Element for a given name
74     *
75     * @param parent optional parent element
76     */

77    public static Element JavaDoc createElement(Element JavaDoc parent, String JavaDoc localPart)
78    {
79       Document JavaDoc doc = getOwnerDocument(parent);
80       log.debug("createElement {}" + localPart);
81       return doc.createElement(localPart);
82    }
83
84    /**
85     * Create an Element for a given name and prefix
86     *
87     * @param parent optional parent element
88     */

89    public static Element JavaDoc createElement(Element JavaDoc parent, String JavaDoc localPart, String JavaDoc prefix)
90    {
91       Document JavaDoc doc = getOwnerDocument(parent);
92       log.debug("createElement {}" + prefix + ":" + localPart);
93       return doc.createElement(prefix + ":" + localPart);
94    }
95
96    /**
97     * Create an Element for a given name, prefix and uri
98     *
99     * @param parent optional parent element
100     */

101    public static Element JavaDoc createElement(Element JavaDoc parent, String JavaDoc uri, String JavaDoc prefix, String JavaDoc localPart)
102    {
103       Document JavaDoc doc = getOwnerDocument(parent);
104       if (prefix == null || prefix.length() == 0)
105       {
106          log.debug("createElement {" + uri + "}" + localPart);
107          return doc.createElementNS(uri, localPart);
108       }
109       else
110       {
111          log.debug("createElement {" + uri + "}" + prefix + ":" + localPart);
112          return doc.createElementNS(uri, prefix + ":" + localPart);
113       }
114    }
115
116    /**
117     * Create an Element for a given Name object
118     *
119     * @param parent optional parent element
120     */

121    public static Element JavaDoc createElement(Element JavaDoc parent, Name JavaDoc name)
122    {
123       return createElement(parent, name.getURI(), name.getPrefix(), name.getLocalName());
124    }
125
126    /**
127     * Create a org.w3c.dom.Text node
128     */

129    public static Text JavaDoc createTextNode(String JavaDoc value)
130    {
131       Document JavaDoc doc = builder.newDocument();
132       return doc.createTextNode(value);
133    }
134
135    private static Document JavaDoc getOwnerDocument(Node JavaDoc parent)
136    {
137       Document JavaDoc doc = null;
138       if (parent != null)
139          doc = parent.getOwnerDocument();
140       if (doc == null)
141          doc = builder.newDocument();
142       return doc;
143    }
144 }
145
Popular Tags