Source Code Cross Referenced for XMLDocumentFactory.java in  » Database-JDBC-Connection-Pool » octopus » org » enhydra » 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 » Database JDBC Connection Pool » octopus » org.enhydra.xml 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:            Copyright (C) 2003  Together
003:
004:            This library is free software; you can redistribute it and/or
005:            modify it under the terms of the GNU Lesser General Public
006:            License as published by the Free Software Foundation; either
007:            version 2.1 of the License, or (at your option) any later version.
008:
009:            This library is distributed in the hope that it will be useful,
010:            but WITHOUT ANY WARRANTY; without even the implied warranty of
011:            MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
012:            Lesser General Public License for more details.
013:
014:            You should have received a copy of the GNU Lesser General Public
015:            License along with this library; if not, write to the Free Software
016:            Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
017:         */
018:
019:        package org.enhydra.xml;
020:
021:        import java.io.File;
022:        import java.io.FileOutputStream;
023:        import java.io.IOException;
024:        import java.util.Properties;
025:
026:        //import org.apache.xerces.parsers.DOMParser;
027:        import javax.xml.parsers.DocumentBuilderFactory;
028:        import javax.xml.parsers.DocumentBuilder;
029:        import org.xml.sax.ErrorHandler;
030:        import org.xml.sax.SAXException;
031:        import org.xml.sax.SAXParseException;
032:        import javax.xml.parsers.ParserConfigurationException;
033:        import org.w3c.dom.Document;
034:        import org.w3c.dom.Node;
035:        import org.xml.sax.SAXException;
036:
037:        /**
038:         * @author Tweety
039:         *
040:         * A class for manipulating the entire xml file (reading, writing...).
041:         *
042:         * @version 1.0
043:         */
044:        public class XMLDocumentFactory {
045:
046:            private static String[] properties = { "method", "version",
047:                    "encoding", "omit-xml-declaration", "standalone",
048:                    "doctype-public", "doctype-system", "indent", "media-type" };
049:
050:            private static int METHOD = 0;
051:            private static int VERSION = 1;
052:            private static int ENCODING = 2;
053:            private static int OMIT_XML_DECLARATION = 3;
054:            private static int STANDALONE = 4;
055:            private static int DOCTYPE_PUBLIC = 5;
056:            private static int DOCTYPE_SYSTEM = 6;
057:            private static int CDATA_SECTION_ELEMENTS = 7;
058:            private static int INDENT = 8;
059:            private static int MEDIA_TYPE = 9;
060:
061:            /**
062:             * xml file name.
063:             */
064:            private String fileName;
065:
066:            /**
067:             * Constructs an empty <code>XMLDocumentFactory</code>
068:             */
069:            public XMLDocumentFactory() {
070:            }
071:
072:            /**
073:             * Constructs a <code>XMLDocumentFactory</code> with the given
074:             * @param fileName as <code>String</code>
075:             */
076:            public XMLDocumentFactory(String fileName) {
077:                this .fileName = fileName;
078:            }
079:
080:            /**
081:             * Returns xml file name.
082:             *
083:             * @return xml file name.
084:             */
085:            public String getFileName() {
086:                return this .fileName;
087:            }
088:
089:            /**
090:             * Parses xml file with the given name and creates <code>Document</code>.
091:             *
092:             * @param fileName xml file name.
093:             *
094:             * @return document.
095:             */
096:            public static Document parse(String fileName) {
097:                DocumentBuilderFactory factory = DocumentBuilderFactory
098:                        .newInstance();
099:                try {
100:                    DocumentBuilder builder = factory.newDocumentBuilder();
101:                    builder.setErrorHandler(new UtilErrorHandler());
102:                    Document doc = builder.parse(fileName);
103:                    return doc;
104:                } catch (SAXParseException e) {
105:                    e.printStackTrace();
106:                } catch (ParserConfigurationException e) {
107:                    e.printStackTrace();
108:                } catch (IOException e) {
109:                    e.printStackTrace();
110:                } catch (SAXException e) {
111:                    e.printStackTrace();
112:                }
113:                return null;
114:
115:                //OLD with apache xerces
116:                //		DOMParser parser = new DOMParser();
117:                //		try {
118:                //			parser.parse(fileName);
119:                //			return parser.getDocument();
120:                //		} catch (SAXException e) {
121:                //			e.printStackTrace();
122:                //		} catch (IOException e) {
123:                //			e.printStackTrace();
124:                //		}
125:                //		return null;
126:            }
127:
128:            /**
129:             * Parses xml file and creates creates <code>Document</code>.
130:             * @return object of document
131:             */
132:            public Document parse() {
133:                DocumentBuilderFactory factory = DocumentBuilderFactory
134:                        .newInstance();
135:                try {
136:                    DocumentBuilder builder = factory.newDocumentBuilder();
137:                    builder.setErrorHandler(new UtilErrorHandler());
138:                    Document doc = builder.parse(fileName);
139:                    return doc;
140:                } catch (SAXParseException e) {
141:                    e.printStackTrace();
142:                } catch (ParserConfigurationException e) {
143:                    e.printStackTrace();
144:                } catch (IOException e) {
145:                    e.printStackTrace();
146:                } catch (SAXException e) {
147:                    e.printStackTrace();
148:                }
149:                return null;
150:
151:                //OLD with apache xerces
152:                //		DOMParser parser = new DOMParser();
153:                //		try {
154:                //			parser.parse(this.fileName);
155:                //			return parser.getDocument();
156:                //		} catch (SAXException e) {
157:                //			System.err.println("SAXException - bad xml format");
158:                //		} catch (IOException e) {
159:                //		}
160:                //		return null;
161:            }
162:
163:            /**
164:             * Serializes node with all subnodes to the xml file with the given name,
165:             * and with the <code>Properties</code> of the xml declaration.
166:             *
167:             * @param node root node of the document.
168:             * @param fileName xml file name
169:             * @param prop <code>Properties</code> of the xml declaration.
170:             */
171:            public static void serialize(Node node, String fileName,
172:                    Properties prop) {
173:                String out = "<?xml version=\"1.0\"?>";
174:                File file = new File(fileName);
175:
176:                //serialize xml declaration
177:                if (prop != null) {
178:                    out = "<?xml";
179:                    String str = "";
180:                    for (int i = 0; i < properties.length; i++) {
181:                        str = (String) prop.get(properties[i]);
182:                        if (str != null)
183:                            out += " " + properties[i] + "=\"" + str + "\"";
184:                    }
185:                    out += "?>";
186:                }
187:
188:                //serialize document
189:                try {
190:                    FileOutputStream outStream = new FileOutputStream(file);
191:                    out += node.toString();
192:                    outStream.write(out.getBytes());
193:                    outStream.close();
194:                } catch (Exception e) {
195:                    System.err.println("Error serializing file");
196:                }
197:            }
198:
199:            /**
200:             * Serializes node with all subnodes to the xml file
201:             * with the default <code>Properties</code> of the xml declaration.
202:             *
203:             * @param node root node of the document.
204:             */
205:            public void serialize(Node node) {
206:
207:                File file = new File(fileName);
208:                try {
209:                    FileOutputStream outStream = new FileOutputStream(file);
210:                    outStream.write(node.toString().getBytes());
211:                    outStream.close();
212:                } catch (Exception e) {
213:                    System.err.println("Error serializing file");
214:                }
215:            }
216:
217:            static class UtilErrorHandler implements  ErrorHandler {
218:
219:                // throw SAXException for fatal errors
220:                public void fatalError(SAXParseException exception)
221:                        throws SAXException {
222:                    throw new SAXException(exception);
223:                }
224:
225:                public void error(SAXParseException errorException)
226:                        throws SAXException {
227:                    throw new SAXException(errorException);
228:                }
229:
230:                // print any warnings
231:                public void warning(SAXParseException warningError)
232:                        throws SAXException {
233:                    System.err.println("[Validation : Warning] URI = "
234:                            + warningError.getMessage());
235:                }
236:            }
237:
238:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.