Source Code Cross Referenced for DOMUtils.java in  » Net » lucene-connector » org » apache » lucene » xmlparser » 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 » Net » lucene connector » org.apache.lucene.xmlparser 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.apache.lucene.xmlparser;
002:
003:        import java.io.Reader;
004:
005:        import javax.xml.parsers.DocumentBuilder;
006:        import javax.xml.parsers.DocumentBuilderFactory;
007:
008:        import org.w3c.dom.Document;
009:        import org.w3c.dom.Element;
010:        import org.w3c.dom.Node;
011:        import org.xml.sax.InputSource;
012:
013:        /**
014:         * Licensed to the Apache Software Foundation (ASF) under one or more
015:         * contributor license agreements.  See the NOTICE file distributed with
016:         * this work for additional information regarding copyright ownership.
017:         * The ASF licenses this file to You under the Apache License, Version 2.0
018:         * (the "License"); you may not use this file except in compliance with
019:         * the License.  You may obtain a copy of the License at
020:         *
021:         *     http://www.apache.org/licenses/LICENSE-2.0
022:         *
023:         * Unless required by applicable law or agreed to in writing, software
024:         * distributed under the License is distributed on an "AS IS" BASIS,
025:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
026:         * See the License for the specific language governing permissions and
027:         * limitations under the License.
028:         */
029:        public class DOMUtils {
030:            public static Element getChildByTagOrFail(Element e, String name)
031:                    throws ParserException {
032:                Element kid = getChildByTagName(e, name);
033:                if (null == kid) {
034:                    throw new ParserException(e.getTagName() + " missing \""
035:                            + name + "\" child element");
036:                }
037:                return kid;
038:            }
039:
040:            public static Element getFirstChildOrFail(Element e)
041:                    throws ParserException {
042:                Element kid = getFirstChildElement(e);
043:                if (null == kid) {
044:                    throw new ParserException(e.getTagName()
045:                            + " does not contain a child element");
046:                }
047:                return kid;
048:            }
049:
050:            public static String getAttributeOrFail(Element e, String name)
051:                    throws ParserException {
052:                String v = e.getAttribute(name);
053:                if (null == v) {
054:                    throw new ParserException(e.getTagName() + " missing \""
055:                            + name + "\" attribute");
056:                }
057:                return v;
058:            }
059:
060:            public static String getAttributeWithInheritanceOrFail(Element e,
061:                    String name) throws ParserException {
062:                String v = getAttributeWithInheritance(e, name);
063:                if (null == v) {
064:                    throw new ParserException(e.getTagName() + " missing \""
065:                            + name + "\" attribute");
066:                }
067:                return v;
068:            }
069:
070:            public static String getNonBlankTextOrFail(Element e)
071:                    throws ParserException {
072:                String v = getText(e);
073:                if (null != v)
074:                    v = v.trim();
075:                if (null == v || 0 == v.length()) {
076:                    throw new ParserException(e.getTagName() + " has no text");
077:                }
078:                return v;
079:            }
080:
081:            /* Convenience method where there is only one child Element of a given name */
082:            public static Element getChildByTagName(Element e, String name) {
083:                for (Node kid = e.getFirstChild(); kid != null; kid = kid
084:                        .getNextSibling()) {
085:                    if ((kid.getNodeType() == Node.ELEMENT_NODE)
086:                            && (name.equals(kid.getNodeName()))) {
087:                        return (Element) kid;
088:                    }
089:                }
090:                return null;
091:            }
092:
093:            /**
094:             * Returns an attribute value from this node, or first parent node with this attribute defined
095:             * @param element 
096:             * @param attributeName
097:             * @return A non-zero-length value if defined, otherwise null
098:             */
099:            public static String getAttributeWithInheritance(Element element,
100:                    String attributeName) {
101:                String result = element.getAttribute(attributeName);
102:                if ((result == null) || ("".equals(result))) {
103:                    Node n = element.getParentNode();
104:                    if ((n == element) || (n == null)) {
105:                        return null;
106:                    }
107:                    if (n instanceof  Element) {
108:                        Element parent = (Element) n;
109:                        return getAttributeWithInheritance(parent,
110:                                attributeName);
111:                    }
112:                    return null; //we reached the top level of the document without finding attribute
113:                }
114:                return result;
115:            }
116:
117:            /* Convenience method where there is only one child Element of a given name */
118:            public static String getChildTextByTagName(Element e, String tagName) {
119:                Element child = getChildByTagName(e, tagName);
120:                if (child != null) {
121:                    return getText(child);
122:                }
123:                return null;
124:            }
125:
126:            /* Convenience method to append a new child with text*/
127:            public static Element insertChild(Element parent, String tagName,
128:                    String text) {
129:                Element child = parent.getOwnerDocument()
130:                        .createElement(tagName);
131:                parent.appendChild(child);
132:                if (text != null) {
133:                    child.appendChild(child.getOwnerDocument().createTextNode(
134:                            text));
135:                }
136:                return child;
137:            }
138:
139:            public static String getAttribute(Element element,
140:                    String attributeName, String deflt) {
141:                String result = element.getAttribute(attributeName);
142:                if ((result == null) || ("".equals(result))) {
143:                    return deflt;
144:                }
145:                return result;
146:            }
147:
148:            public static float getAttribute(Element element,
149:                    String attributeName, float deflt) {
150:                String result = element.getAttribute(attributeName);
151:                if ((result == null) || ("".equals(result))) {
152:                    return deflt;
153:                }
154:                return Float.parseFloat(result);
155:            }
156:
157:            public static int getAttribute(Element element,
158:                    String attributeName, int deflt) {
159:                String result = element.getAttribute(attributeName);
160:                if ((result == null) || ("".equals(result))) {
161:                    return deflt;
162:                }
163:                return Integer.parseInt(result);
164:            }
165:
166:            public static boolean getAttribute(Element element,
167:                    String attributeName, boolean deflt) {
168:                String result = element.getAttribute(attributeName);
169:                if ((result == null) || ("".equals(result))) {
170:                    return deflt;
171:                }
172:                return Boolean.getBoolean(result);
173:            }
174:
175:            /* Returns text of node and all child nodes - without markup */
176:            //MH changed to Node from Element 25/11/2005
177:            public static String getText(Node e) {
178:                StringBuffer sb = new StringBuffer();
179:                getTextBuffer(e, sb);
180:                return sb.toString();
181:            }
182:
183:            public static Element getFirstChildElement(Element element) {
184:                for (Node kid = element.getFirstChild(); kid != null; kid = kid
185:                        .getNextSibling()) {
186:                    if (kid.getNodeType() == Node.ELEMENT_NODE) {
187:                        return (Element) kid;
188:                    }
189:                }
190:                return null;
191:            }
192:
193:            private static void getTextBuffer(Node e, StringBuffer sb) {
194:                for (Node kid = e.getFirstChild(); kid != null; kid = kid
195:                        .getNextSibling()) {
196:                    switch (kid.getNodeType()) {
197:                    case Node.TEXT_NODE: {
198:                        sb.append(kid.getNodeValue());
199:                        break;
200:                    }
201:                    case Node.ELEMENT_NODE: {
202:                        getTextBuffer(kid, sb);
203:                        break;
204:                    }
205:                    case Node.ENTITY_REFERENCE_NODE: {
206:                        getTextBuffer(kid, sb);
207:                        break;
208:                    }
209:                    }
210:                }
211:            }
212:
213:            /**
214:             * Helper method to parse an XML file into a DOM tree, given a reader.
215:             * @param is reader of the XML file to be parsed
216:             * @return an org.w3c.dom.Document object
217:             */
218:            public static Document loadXML(Reader is) {
219:
220:                DocumentBuilderFactory dbf = DocumentBuilderFactory
221:                        .newInstance();
222:                DocumentBuilder db = null;
223:
224:                try {
225:                    db = dbf.newDocumentBuilder();
226:                } catch (Exception se) {
227:                    throw new RuntimeException("Parser configuration error", se);
228:                }
229:
230:                // Step 3: parse the input file
231:                org.w3c.dom.Document doc = null;
232:                try {
233:                    doc = db.parse(new InputSource(is));
234:                    //doc = db.parse(is);
235:                } catch (Exception se) {
236:                    throw new RuntimeException("Error parsing file:" + se, se);
237:                }
238:
239:                return doc;
240:            }
241:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.