Source Code Cross Referenced for XML.java in  » Portal » uPortal_rel-2-6-1-GA » org » jasig » portal » utils » 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 » Portal » uPortal_rel 2 6 1 GA » org.jasig.portal.utils 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /* Copyright 2001 The JA-SIG Collaborative.  All rights reserved.
002:         *  See license distributed with this file and
003:         *  available online at http://www.uportal.org/license.html
004:         */
005:
006:        package org.jasig.portal.utils;
007:
008:        import java.io.IOException;
009:        import java.io.StringWriter;
010:
011:        import javax.xml.transform.Transformer;
012:        import javax.xml.transform.TransformerConfigurationException;
013:        import javax.xml.transform.TransformerException;
014:        import javax.xml.transform.TransformerFactory;
015:        import javax.xml.transform.dom.DOMSource;
016:        import javax.xml.transform.sax.SAXResult;
017:
018:        import org.jasig.portal.serialize.OutputFormat;
019:        import org.jasig.portal.serialize.XMLSerializer;
020:        import org.w3c.dom.Document;
021:        import org.w3c.dom.Element;
022:        import org.w3c.dom.Node;
023:        import org.xml.sax.ContentHandler;
024:
025:        /**
026:         * This utility provides useful XML helper methods.
027:         *
028:         * @author Ken Weiner, kweiner@unicon.net
029:         * @version $Revision: 34797 $
030:         */
031:        public class XML {
032:            /**
033:             * Gets the text value of an Element. For example, if an element nameElement
034:             * looks like this: <name>Fred</name>, then getElementText(nameElement) would
035:             * return "Fred".  An empty String is returned in the case that there is no text
036:             * under the element.
037:             * @param e the Element with a text value
038:             * @return the the text value of the element
039:             */
040:            public static String getElementText(Element e) {
041:                String val = "";
042:                for (Node n = e.getFirstChild(); n != null; n = n
043:                        .getNextSibling()) {
044:                    if (n.getNodeType() == Node.TEXT_NODE) {
045:                        val = n.getNodeValue();
046:                        break;
047:                    }
048:                }
049:                return val;
050:            }
051:
052:            /**
053:             * Gets the text value of a child Element.  For example, if an element nameElement
054:             * looks like this: <name><first>Fred</first><last>Flinstone</last></name>, then
055:             * getChildElementText(nameElement, "first") would return "Fred".  An empty String
056:             * is returned in the case that there is no text under the child Element.
057:             * @param e the Element to search under
058:             * @param childElementName the name of the child Element
059:             * @return the text value of the child element
060:             */
061:            public static String getChildElementText(Element e,
062:                    String childElementName) {
063:                String val = "";
064:                for (Node n = e.getFirstChild(); n != null; n = n
065:                        .getNextSibling()) {
066:                    if (n.getNodeType() == Node.ELEMENT_NODE
067:                            && n.getNodeName() != null
068:                            && n.getNodeName().equals(childElementName)) {
069:                        Element childElement = (Element) n;
070:                        val = getElementText(childElement);
071:                    }
072:                }
073:                return val;
074:            }
075:
076:            /**
077:             * Gets the contents of an XML Document or Element as a nicely formatted string.
078:             * This method is useful for debugging.
079:             * @param node the Node to print; must be of type Document or Element
080:             * @return a nicely formatted String suitable for printing
081:             */
082:            public static String serializeNode(Node node) {
083:                OutputFormat format = new OutputFormat();
084:                format.setOmitXMLDeclaration(true);
085:                format.setIndenting(true);
086:                return serializeNode(node, format);
087:            }
088:
089:            /**
090:             * Gets the contents of an XML Document or Element as a formatted string.
091:             * This method is useful for debugging.
092:             * @param node the Node to print; must be of type Document or Element
093:             * @param format controls the formatting of the string
094:             * @return a nicely formatted String suitable for printing
095:             */
096:            public static String serializeNode(Node node, OutputFormat format) {
097:                String returnString = null;
098:                StringWriter outString = new StringWriter();
099:                XMLSerializer xsl = new XMLSerializer(outString, format);
100:                try {
101:                    if (node.getNodeType() == Node.DOCUMENT_NODE) {
102:                        xsl.serialize((Document) node);
103:                        returnString = outString.toString();
104:                    } else if (node.getNodeType() == Node.ELEMENT_NODE) {
105:                        xsl.serialize((Element) node);
106:                        returnString = outString.toString();
107:                    } else {
108:                        returnString = "The node you passed to getNodeAsString() must be of type org.w3c.dom.Document or org.w3c.dom.Element in order to be serialized.";
109:                    }
110:                } catch (IOException ioe) {
111:                    returnString = "Error occurred while trying to serialize node: "
112:                            + ioe.getMessage();
113:                }
114:
115:                return returnString;
116:            }
117:
118:            /**
119:             * This is only being kept around for backward compatibility. Callers
120:             * should now be using Document.cloneNode(true).
121:             * @param olddoc the original document
122:             * @return a clone of the original document with preserved ID tables
123:             */
124:            public static Document cloneDocument(Document olddoc) {
125:                return (Document) olddoc.cloneNode(true);
126:            }
127:
128:            /**
129:             * Outputs a dom document into a sax stream.
130:             *
131:             * @param dom a dom <code>Node</code> value
132:             * @param sax a sax <code>ContentHandler</code> value
133:             */
134:            public static void dom2sax(Node dom, ContentHandler sax)
135:                    throws TransformerConfigurationException,
136:                    TransformerException {
137:                TransformerFactory tFactory = TransformerFactory.newInstance();
138:                Transformer emptytr = tFactory.newTransformer();
139:                emptytr.transform(new DOMSource(dom), new SAXResult(sax));
140:            }
141:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.