Source Code Cross Referenced for XmlUtil.java in  » Testing » mockrunner-0.4 » com » mockrunner » util » web » 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 » Testing » mockrunner 0.4 » com.mockrunner.util.web 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package com.mockrunner.util.web;
002:
003:        import java.io.StringReader;
004:        import java.util.List;
005:
006:        import org.apache.xerces.parsers.DOMParser;
007:        import org.cyberneko.html.HTMLConfiguration;
008:        import org.jdom.Element;
009:        import org.jdom.input.DOMBuilder;
010:        import org.jdom.output.XMLOutputter;
011:        import org.xml.sax.InputSource;
012:
013:        import com.mockrunner.base.NestedApplicationException;
014:
015:        /**
016:         * Util class for HTML and XML parsing.
017:         */
018:        public class XmlUtil {
019:            /**
020:             * Convinience method for HTML fragments. Returns the body
021:             * as JDOM <code>Element</code>.
022:             * 
023:             * If an HTML documents looks like this:
024:             * <pre>
025:             * &lt;html&gt;
026:             * &lt;head&gt;
027:             * &lt;/head&gt;
028:             * &lt;body&gt;
029:             * &lt;h1&gt;
030:             * &lt;/h1&gt;
031:             * &lt;/body&gt;
032:             * &lt;/html&gt;
033:             * </pre>
034:             * 
035:             * the method returns the h1 tag as <code>Element</code>.
036:             * @param document the <code>org.jdom.Document</code>
037:             * @return the body <code>Element</code>
038:             */
039:            public static Element getBodyFragmentFromJDOMDocument(
040:                    org.jdom.Document document) {
041:                Element element = document.getRootElement().getChild("BODY");
042:                if (null == element) {
043:                    element = document.getRootElement().getChild("body");
044:                }
045:                if (null != element) {
046:                    List childs = element.getChildren();
047:                    if (null != childs && childs.size() > 0)
048:                        return (Element) childs.get(0);
049:                }
050:                return null;
051:            }
052:
053:            /**
054:             * @deprecated use {@link #getBodyFragmentFromJDOMDocument}
055:             */
056:            public static Element getBodyFragmentJDOMDocument(
057:                    org.jdom.Document document) {
058:                return getBodyFragmentFromJDOMDocument(document);
059:            }
060:
061:            /**
062:             * Returns the documents XML content as a string.
063:             * @param document the <code>org.jdom.Document</code>
064:             * @return the output as string
065:             */
066:            public static String createStringFromJDOMDocument(
067:                    org.jdom.Document document) {
068:                try {
069:                    return new XMLOutputter().outputString(document);
070:                } catch (Exception exc) {
071:                    throw new NestedApplicationException(exc);
072:                }
073:            }
074:
075:            /**
076:             * Creates a JDOM <code>Document</code> from a specified
077:             * W3C <code>Document</code>.
078:             * @param document the <code>org.w3c.dom.Document</code>
079:             * @return the <code>org.jdom.Document</code>
080:             */
081:            public static org.jdom.Document createJDOMDocument(
082:                    org.w3c.dom.Document document) {
083:                return new DOMBuilder().build(document);
084:            }
085:
086:            /**
087:             * Returns a parser suitable for parsing HTML documents.
088:             * The NekoHTML parser is used with some settings to
089:             * preserve case of tag names and disable namespace processing. 
090:             * This method is used by {@link #parseHTML}.
091:             * @return instance of <code>org.apache.xerces.parsers.DOMParser</code>
092:             *         with Neko configuration
093:             */
094:            public static DOMParser getHTMLParser() {
095:                try {
096:                    HTMLConfiguration config = new HTMLConfiguration();
097:                    config.setProperty(
098:                            "http://cyberneko.org/html/properties/names/elems",
099:                            "match");
100:                    config.setProperty(
101:                            "http://cyberneko.org/html/properties/names/attrs",
102:                            "no-change");
103:                    DOMParser parser = new DOMParser(config);
104:                    return parser;
105:                } catch (Exception exc) {
106:                    throw new NestedApplicationException(exc);
107:                }
108:            }
109:
110:            /**
111:             * Parses the specified HTML with the NekoHTML parser.
112:             * If you want to use another HTML parser or configure
113:             * the NekoHTML parser with special features, you can use
114:             * the <code>parse</code> method.
115:             * @param source the HTML as String
116:             * @return the parsed document as org.w3c.dom.Document
117:             */
118:            public static org.w3c.dom.Document parseHTML(String source) {
119:                try {
120:                    return parse(getHTMLParser(), source);
121:                } catch (Exception exc) {
122:                    throw new NestedApplicationException(exc);
123:                }
124:            }
125:
126:            /**
127:             * Parses the specified XML with the specified parser.
128:             * The main purpose of this method is to use the NekoHTML 
129:             * parser with custom features and properties. If you can live
130:             * with the settings provided by Mockrunner, you can use 
131:             * {@link #parseHTML}.
132:             * @param parser the parser (must extend 
133:             *               <code>org.apache.xerces.parsers.DOMParser</code>), 
134:             *               e.g. the one returned by {@link #getHTMLParser}
135:             * @param source the XML as String
136:             * @return the parsed document as org.w3c.dom.Document
137:             */
138:            public static org.w3c.dom.Document parse(DOMParser parser,
139:                    String source) {
140:                try {
141:                    parser.parse(new InputSource(new StringReader(source)));
142:                    return parser.getDocument();
143:                } catch (Exception exc) {
144:                    throw new NestedApplicationException(exc);
145:                }
146:            }
147:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.