Source Code Cross Referenced for XMLUtil.java in  » Web-Framework » JAT » com » jat » util » xml » 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 » Web Framework » JAT » com.jat.util.xml 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package com.jat.util.xml;
002:
003:        import java.io.File;
004:        import java.io.FileInputStream;
005:        import java.io.IOException;
006:        import java.io.StringReader;
007:        import java.util.Vector;
008:        import javax.xml.parsers.ParserConfigurationException;
009:
010:        import org.w3c.dom.Attr;
011:        import org.w3c.dom.Document;
012:        import org.w3c.dom.NamedNodeMap;
013:        import org.w3c.dom.Node;
014:        import org.w3c.dom.NodeList;
015:        import org.xml.sax.InputSource;
016:        import org.xml.sax.SAXException;
017:        import com.jat.util.file.FileUtil;
018:
019:        /**
020:         * <p>Title: JAT</p>
021:         * <p>Description:  XMLUtil class contains several methods to read, write and format XML document.</p>
022:         * <p>Copyright: Copyright (c) 2004</p>
023:         *
024:         * @author stf
025:         * @version 1.1
026:         * @see org.w3c.dom.Document
027:         * @see org.w3c.dom.Node
028:         * @see com.jat.util.xml.XMLDocument
029:         * @see com.jat.util.file.FileUtil
030:         */
031:
032:        public class XMLUtil {
033:
034:            /**
035:             * return all Text node children of node 'node'
036:             *
037:             * @param node
038:             */
039:            public static Vector getAllTextNode(Node node) {
040:                return getAllNodes(node, Node.TEXT_NODE);
041:            }
042:
043:            /**
044:             * return all Comment node children of node 'node'
045:             *
046:             * @param node
047:             */
048:            public static Vector getAllCommentNode(Node node) {
049:                return getAllNodes(node, Node.COMMENT_NODE);
050:            }
051:
052:            /**
053:             * return all DataSection node children of node 'node'
054:             *
055:             * @param node
056:             */
057:            public static Vector getAllCDataSectionNode(Node node) {
058:                return getAllNodes(node, Node.CDATA_SECTION_NODE);
059:            }
060:
061:            /**
062:             * return all Attribute node children of node 'node'
063:             *
064:             * @param node
065:             */
066:            public static Vector getAllAttributeNode(Node node) {
067:                return getAllNodes(node, Node.ATTRIBUTE_NODE);
068:            }
069:
070:            /**
071:             * return all DocumentFragmenter node children of node 'node'
072:             *
073:             * @param node
074:             */
075:            public static Vector getAllDocumentFragmentNode(Node node) {
076:                return getAllNodes(node, Node.DOCUMENT_FRAGMENT_NODE);
077:            }
078:
079:            /**
080:             * return all Element node children of node 'node'
081:             *
082:             * @param node
083:             */
084:            public static Vector getAllElementNode(Node node) {
085:                return getAllNodes(node, Node.ELEMENT_NODE);
086:            }
087:
088:            /**
089:             * return all Entity node children of node 'node'
090:             *
091:             * @param node
092:             */
093:            public static Vector getAllEntityNode(Node node) {
094:                return getAllNodes(node, Node.ENTITY_NODE);
095:            }
096:
097:            /**
098:             * return all Notation node children of node 'node'
099:             *
100:             * @param node
101:             */
102:            public static Vector getAllNotationNode(Node node) {
103:                return getAllNodes(node, Node.NOTATION_NODE);
104:            }
105:
106:            /**
107:             * return all ProcessingInstruction node children of node 'node'
108:             *
109:             * @param node
110:             */
111:            public static Vector getAllProcessingInstructionNode(Node node) {
112:                return getAllNodes(node, Node.PROCESSING_INSTRUCTION_NODE);
113:            }
114:
115:            /**
116:             * return all node children of node 'node' of the type 'type'
117:             *
118:             * @param node
119:             * @param type
120:             */
121:            public static Vector getAllNodes(Node node, short type) {
122:                if (!checkNodeType(type))
123:                    return null;
124:                Vector ret = new Vector();
125:                getAllNodes(node, type, ret);
126:                return ret;
127:            }
128:
129:            /**
130:             * check if type 'type' is recgnized as Node type
131:             *
132:             * @param type
133:             */
134:            public static boolean checkNodeType(short type) {
135:                switch (type) {
136:                case Node.ATTRIBUTE_NODE:
137:                    return true;
138:                case Node.CDATA_SECTION_NODE:
139:                    return true;
140:                case Node.COMMENT_NODE:
141:                    return true;
142:                case Node.DOCUMENT_FRAGMENT_NODE:
143:                    return true;
144:                case Node.DOCUMENT_NODE:
145:                    return true;
146:                case Node.DOCUMENT_TYPE_NODE:
147:                    return true;
148:                case Node.ELEMENT_NODE:
149:                    return true;
150:                case Node.ENTITY_NODE:
151:                    return true;
152:                case Node.ENTITY_REFERENCE_NODE:
153:                    return true;
154:                case Node.NOTATION_NODE:
155:                    return true;
156:                case Node.PROCESSING_INSTRUCTION_NODE:
157:                    return true;
158:                case Node.TEXT_NODE:
159:                    return true;
160:                }
161:                return false;
162:            }
163:
164:            protected static void getAllNodes(Node node, short type,
165:                    Vector nodes) {
166:                NodeList children = node.getChildNodes();
167:                if (children != null) {
168:                    for (int i = 0; i < children.getLength(); i++) {
169:                        Node child = children.item(i);
170:                        int childType = child.getNodeType();
171:                        if (childType == type)
172:                            nodes.addElement(child);
173:                        getAllNodes(child, type, nodes);
174:                    }
175:                }
176:            }
177:
178:            public static Vector getAllChlidren(Node node, String childName) {
179:                Vector ret = new Vector();
180:                NodeList children = node.getChildNodes();
181:                if (children != null) {
182:                    for (int i = 0; i < children.getLength(); i++) {
183:                        Node child = children.item(i);
184:                        if (child.getNodeName().equals(childName))
185:                            ret.addElement(child);
186:                    }
187:                }
188:                return ret;
189:            }
190:
191:            public static Attr getAttribute(Node node, String attrName) {
192:                //System.out.println(getXMLString(node));
193:                NamedNodeMap attributes = node.getAttributes();
194:                //System.out.println("attributes "+attributes.getLength());
195:                if (attributes != null) {
196:                    for (int i = 0; i < attributes.getLength(); i++) {
197:                        Attr attr = (Attr) attributes.item(i);
198:                        //System.out.println("attribute "+attr);
199:                        if (attr.getName().equals(attrName))
200:                            return attr;
201:                    }
202:                }
203:                return null;
204:            }
205:
206:            public final static String STYLESHEET_NODE_NAME = "xml-stylesheet";
207:
208:            protected static void getAllNodes(Node node, short type,
209:                    String nodeName, Vector nodes) {
210:                Vector vett = new Vector();
211:                getAllNodes(node, type, vett);
212:                for (int i = 0; i < vett.size(); i++) {
213:                    Attr nd = (Attr) vett.elementAt(i);
214:                    if (nd.getNodeName().equals(nodeName))
215:                        nodes.addElement(nd);
216:                }
217:            }
218:
219:            /**
220:             * Remove all childern of node 'node'
221:             *
222:             * @param node
223:             */
224:            public static void removeAllChildNode(Node node) {
225:                node.setNodeValue(null);
226:                node.setPrefix(null);
227:                if (node.hasChildNodes()) {
228:                    NodeList childNodes = node.getChildNodes();
229:                    for (int i = 0; i < childNodes.getLength(); i++)
230:                        node.removeChild(childNodes.item(i));
231:                }
232:            }
233:
234:            /**
235:             * return the String representin gthe XML format from the node "node"
236:             *
237:             * @param node
238:             */
239:            public static String getXMLString(Node node) {
240:                if (DEBUG)
241:                    System.out.println("attri " + Node.ATTRIBUTE_NODE + "\n"
242:                            + "cdata " + Node.CDATA_SECTION_NODE + "\n"
243:                            + "comme " + Node.COMMENT_NODE + "\n" + "doc f "
244:                            + Node.DOCUMENT_FRAGMENT_NODE + "\n" + "doc   "
245:                            + Node.DOCUMENT_NODE + "\n" + "doc t "
246:                            + Node.DOCUMENT_TYPE_NODE + "\n" + "eleme "
247:                            + Node.ELEMENT_NODE + "\n" + "entit "
248:                            + Node.ENTITY_NODE + "\n" + "ent r "
249:                            + Node.ENTITY_REFERENCE_NODE + "\n" + "notat "
250:                            + Node.NOTATION_NODE + "\n" + "proce "
251:                            + Node.PROCESSING_INSTRUCTION_NODE + "\n"
252:                            + "text  " + Node.TEXT_NODE + "\n");
253:                xml = "";
254:                getXML(node);
255:                return xml;
256:            }
257:
258:            private static String xml;
259:
260:            protected static void getXML(Node node) {
261:                //if (DEBUG) System.out.println("Node:"+node.getNodeName()+" - "+node.getNodeValue()+" - "+node.getNodeType()+"\nxml:"+xml);
262:                short type = node.getNodeType();
263:                switch (type) {
264:                case Node.DOCUMENT_NODE: {
265:                    Document doc = (Document) node;
266:                    xml += "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>";
267:                    Vector children = getAllProcessingInstructionNode(doc);
268:                    if (children != null) {
269:                        if (DEBUG)
270:                            System.out.println(node.getNodeName()
271:                                    + " - processing instruction: "
272:                                    + children.size());
273:                        for (int i = 0; i < children.size(); i++)
274:                            getXML((Node) children.elementAt(i));
275:                    }
276:                    getXML(doc.getDocumentElement());
277:                    break;
278:                }
279:                case Node.ELEMENT_NODE: {
280:                    xml += "\n<" + node.getNodeName();
281:                    org.w3c.dom.NamedNodeMap attrs = node.getAttributes();
282:                    if (DEBUG)
283:                        System.out.println(node.getNodeName()
284:                                + " - attribute: " + attrs.getLength());
285:                    for (int i = 0; i < attrs.getLength(); i++) {
286:                        Node attr = attrs.item(i);
287:                        xml += " " + attr.getNodeName() + "=\""
288:                                + attr.getNodeValue().trim() + "\"";
289:                    }
290:                    NodeList children = node.getChildNodes();
291:                    if (DEBUG)
292:                        System.out.println(node.getNodeName() + " - children: "
293:                                + attrs.getLength());
294:                    int nc = (children != null ? children.getLength() : 0);
295:                    boolean nv = node.getNodeValue() != null;
296:
297:                    if (nc > 0 || nv)
298:                        xml += ">"
299:                                + (nv ? "\n" : "")
300:                                + (node.getNodeValue() != null ? node
301:                                        .getNodeValue().trim() : "");
302:                    else
303:                        xml += "/>";
304:                    if (children != null) {
305:                        int len = children.getLength();
306:                        for (int i = 0; i < len; i++) {
307:                            //if (i>0 && i<len) xml+="\n";
308:                            getXML(children.item(i));
309:                        }
310:                    }
311:                    if (nc > 0 || nv)
312:                        xml += (nc > 0 ? "\n" : "") + "</" + node.getNodeName()
313:                                + ">";
314:                    break;
315:                }
316:                case Node.ENTITY_REFERENCE_NODE: {
317:                    xml += "&" + node.getNodeName() + ";";
318:                    break;
319:
320:                }
321:                case Node.CDATA_SECTION_NODE: {
322:                    xml += "\n<![CDATA[" + node.getNodeValue().trim() + "]]>";
323:                    break;
324:                }
325:                case Node.COMMENT_NODE: {
326:                    xml += "<!--" + node.getNodeValue().trim() + "-->";
327:                }
328:
329:                case Node.TEXT_NODE: {
330:                    xml += node.getNodeValue().trim();
331:                    break;
332:                }
333:                case Node.PROCESSING_INSTRUCTION_NODE: {
334:                    String name = node.getNodeName();
335:                    String value = node.getNodeValue();
336:                    if (value != null)
337:                        value = value.trim();
338:                    xml += "\n<?" + name + " " + value + "?>";
339:                    break;
340:                }
341:                }
342:            }
343:
344:            /**
345:             * return the instance of XMLDocument
346:             *
347:             * @param xmlFile
348:             * @param validate
349:             * @exception java.io.IOException
350:             * @exception javax.xml.parsers.ParserConfigurationException
351:             * @exception org.xml.sax.SAXException
352:             */
353:            public static XMLDocument getXMLDocumentByFilename(String xmlFile,
354:                    boolean validate) throws IOException,
355:                    ParserConfigurationException, SAXException {
356:                return getXMLDocument(new File(xmlFile), validate, false);
357:            }
358:
359:            /**
360:             * return the instance of XMLDocument
361:             *
362:             * @param xmlFile
363:             * @param validate
364:             * @exception java.io.IOException
365:             * @exception javax.xml.parsers.ParserConfigurationException
366:             * @exception org.xml.sax.SAXException
367:             */
368:            public static XMLDocument getXMLDocument(File xmlFile,
369:                    boolean validate) throws IOException,
370:                    ParserConfigurationException, SAXException {
371:                return getXMLDocument(xmlFile, validate, false);
372:            }
373:
374:            /**
375:             * return the instance of XMLDocument
376:             *
377:             * @param xmlFile
378:             * @param validate
379:             * @param coalescing
380:             * @exception java.io.IOException
381:             * @exception javax.xml.parsers.ParserConfigurationException
382:             * @exception org.xml.sax.SAXException
383:             */
384:            public static XMLDocument getXMLDocument(File xmlFile,
385:                    boolean validate, boolean coalescing) throws IOException,
386:                    ParserConfigurationException, SAXException {
387:                FileInputStream fis = null;
388:                try {
389:                    fis = new FileInputStream(xmlFile);
390:                    InputSource is = new InputSource(fis);
391:                    return XMLDocument.getInstance(is, validate, coalescing);
392:                } finally {
393:                    try {
394:                        fis.close();
395:                    } catch (Exception ignored) {
396:                    }
397:                }
398:            }
399:
400:            /**
401:             * return the instance of XMLDocument
402:             *
403:             * @param xml
404:             * @param validate
405:             * @exception java.io.IOException
406:             * @exception javax.xml.parsers.ParserConfigurationException
407:             * @exception org.xml.sax.SAXException
408:             */
409:
410:            public static XMLDocument getXMLDocument(String xml,
411:                    boolean validate) throws IOException,
412:                    ParserConfigurationException, SAXException {
413:                return getXMLDocument(xml, validate, false);
414:            }
415:
416:            /**
417:             * return the instance of XMLDocument
418:             *
419:             * @param xml
420:             * @param validate
421:             * @param coalescing
422:             * @exception java.io.IOException
423:             * @exception javax.xml.parsers.ParserConfigurationException
424:             * @exception org.xml.sax.SAXException
425:             */
426:            public static XMLDocument getXMLDocument(String xml,
427:                    boolean validate, boolean coalescing) throws IOException,
428:                    ParserConfigurationException, SAXException {
429:                InputSource is = new InputSource(new StringReader(xml));
430:                return XMLDocument.getInstance(is, validate, coalescing);
431:            }
432:
433:            /**
434:             * save XML from node 'node' into the file 'filename'
435:             *
436:             * @param filename
437:             * @param node
438:             * @exception java.io.IOException
439:             */
440:            public static void saveXML(String filename, Node node)
441:                    throws IOException {
442:                FileUtil.saveFile(filename, getXMLString(node));
443:            }
444:
445:            public static String parseText(String _s) {
446:                String ret = _s;
447:                ret = replace(ret, "&", "&amp;");
448:                ret = replace(ret, "<", "&gt;");
449:                ret = replace(ret, ">", "&lt;");
450:                ret = replaceStranger(ret);
451:                return ret;
452:            }
453:
454:            public static String parseAttribute(String _s) {
455:                String ret = _s;
456:                ret = replace(ret, "\"", "&quot;");
457:                ret = replaceStranger(ret);
458:                return ret;
459:            }
460:
461:            protected static String replace(String str, String s1, String s2) {
462:                String ret = str;
463:                int index = str.indexOf(s1);
464:                if (index >= 0) {
465:                    ret = ret.substring(0, index)
466:                            + s2
467:                            + replace(ret.substring(index + s1.length()), s1,
468:                                    s2);
469:                }
470:                return ret;
471:            }
472:
473:            private static java.util.Hashtable strangeChar = new java.util.Hashtable();
474:
475:            /**
476:             * add character codification
477:             *
478:             * @param hashCode
479:             * @param replaceString
480:             */
481:            public static void addStrangeChar(int hashCode, String replaceString) {
482:                strangeChar.put(new Integer(hashCode), replaceString);
483:            }
484:
485:            protected static String replaceStranger(String line) {
486:                String ret = "";
487:                if (line == null || strangeChar.size() < 1)
488:                    return line;
489:                for (int i = 0; i < line.length(); i++) {
490:                    Character character = new Character(line.charAt(i));
491:                    Integer hashCode = new Integer(character.hashCode());
492:
493:                    String replacer = (String) strangeChar.get(hashCode);
494:                    if (replacer != null)
495:                        ret += replacer;
496:                    else
497:                        ret += character.charValue();
498:                }
499:                return ret;
500:            }
501:
502:            private XMLUtil() {
503:            }
504:
505:            protected final static boolean DEBUG = false;
506:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.