Source Code Cross Referenced for XMLUtils.java in  » ESB » celtix-1.0 » org » objectweb » celtix » helpers » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » ESB » celtix 1.0 » org.objectweb.celtix.helpers 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.