Source Code Cross Referenced for XMLDocumentFactory.java in  » J2EE » Enhydra-Application-Framework » 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 » J2EE » Enhydra Application Framework » org.enhydra.xml 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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