Source Code Cross Referenced for XmlHelper.java in  » Report » pentaho-report » org » pentaho » designstudio » util » 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 » Report » pentaho report » org.pentaho.designstudio.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2006 Pentaho Corporation.  All rights reserved. 
003:         * This software was developed by Pentaho Corporation and is provided under the terms 
004:         * of the Mozilla Public License, Version 1.1, or any later version. You may not use 
005:         * this file except in compliance with the license. If you need a copy of the license, 
006:         * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho 
007:         * BI Platform.  The Initial Developer is Pentaho Corporation.
008:         *
009:         * Software distributed under the Mozilla Public License is distributed on an "AS IS" 
010:         * basis, WITHOUT WARRANTY OF ANY KIND, either express or  implied. Please refer to 
011:         * the license for the specific language governing your rights and limitations.
012:         *
013:         * @created Jun 17, 2005 
014:         * @author James Dixon
015:         * @author Marc Batchelor
016:         */
017:
018:        package org.pentaho.designstudio.util;
019:
020:        import java.io.File;
021:        import java.io.FileReader;
022:        import java.io.Reader;
023:        import java.io.StringWriter;
024:        import java.util.List;
025:
026:        import org.dom4j.Document;
027:        import org.dom4j.DocumentException;
028:        import org.dom4j.DocumentHelper;
029:        import org.dom4j.Element;
030:        import org.dom4j.Node;
031:        import org.dom4j.io.OutputFormat;
032:        import org.dom4j.io.XMLWriter;
033:
034:        /**
035:         * @author mbatchel/jdixon
036:         *
037:         * TODO To change the template for this generated type comment go to
038:         * Window - Preferences - Java - Code Style - Code Templates
039:         */
040:        public class XmlHelper {
041:
042:            public static String getNodeText(String path, Node rootNode) {
043:                return (getNodeText(path, rootNode, null));
044:            }
045:
046:            public static long getNodeText(String path, Node rootNode,
047:                    long defaultValue) {
048:                String valueStr = getNodeText(path, rootNode, Long
049:                        .toString(defaultValue));
050:                try {
051:                    return Long.parseLong(valueStr);
052:                } catch (Exception e) {
053:                }
054:                return defaultValue;
055:            }
056:
057:            public static String getNodeText(String path, Node rootNode,
058:                    String defaultValue) {
059:                if (rootNode != null) {
060:                    Node node = rootNode.selectSingleNode(path);
061:                    if (node != null) {
062:                        return node.getText();
063:                    }
064:                }
065:                return (null);
066:            }
067:
068:            public static String decode(String string) {
069:                // TODO replace this is a more robust encoder
070:                if (string == null) {
071:                    return null;
072:                }
073:                string = string.replaceAll("&lt;", "<"); //$NON-NLS-1$ //$NON-NLS-2$
074:                string = string.replaceAll("&gt;", ">"); //$NON-NLS-1$ //$NON-NLS-2$
075:                string = string.replaceAll("&amp;", "&"); //$NON-NLS-1$ //$NON-NLS-2$
076:                return string;
077:            }
078:
079:            public static String encode(String string) {
080:                // TODO replace this is a more robust encoder
081:                if (string == null) {
082:                    return null;
083:                }
084:                string = string.replaceAll("<", "&lt;"); //$NON-NLS-1$//$NON-NLS-2$
085:                string = string.replaceAll(">", "&gt;"); //$NON-NLS-1$//$NON-NLS-2$
086:                string = string.replaceAll("&", "&amp;"); //$NON-NLS-1$//$NON-NLS-2$
087:                return string;
088:            }
089:
090:            public static String getNestedErrorMessage(Throwable e) {
091:                String rtn = ""; //$NON-NLS-1$
092:                while (e != null) {
093:                    rtn = e.getLocalizedMessage();
094:                    e = (e instanceof  DocumentException) ? ((DocumentException) e)
095:                            .getNestedException()
096:                            : null;
097:                }
098:                return (rtn);
099:            }
100:
101:            public static Document getDocFromString(String str) {
102:                Document document = null;
103:                try {
104:                    document = DocumentHelper.parseText(str);
105:                } catch (Exception e) {
106:                    e.printStackTrace();
107:                }
108:                return document;
109:            }
110:
111:            public static Document prettyPrint(Document document) {
112:                try {
113:                    OutputFormat format = OutputFormat.createPrettyPrint();
114:                    format.setEncoding(document.getXMLEncoding());
115:                    StringWriter stringWriter = new StringWriter();
116:                    XMLWriter writer = new XMLWriter(stringWriter, format);
117:                    // XMLWriter has a bug that is avoided if we reparse the document
118:                    // prior to calling XMLWriter.write()
119:                    writer.write(DocumentHelper.parseText(document.asXML()));
120:                    writer.close();
121:                    document = DocumentHelper
122:                            .parseText(stringWriter.toString());
123:                } catch (Exception e) {
124:                    e.printStackTrace();
125:                    return (null);
126:                }
127:                return (document);
128:            }
129:
130:            public static Document getDocFromFile(File f) {
131:                Document document = null;
132:                try {
133:                    Reader reader = new FileReader(f);
134:                    StringBuffer sb = new StringBuffer();
135:                    if (reader != null) {
136:                        char buffer[] = new char[1000];
137:                        int bytesRead;
138:                        while ((bytesRead = reader.read(buffer)) > 0) {
139:                            sb.append(buffer, 0, bytesRead);
140:                        }
141:                    }
142:                    document = DocumentHelper.parseText(sb.toString());
143:                } catch (Exception e) {
144:                    e.printStackTrace();
145:                }
146:                return document;
147:            }
148:
149:            /**
150:             * Sorts Element nodes in the order of the element names specified in sequenceList.  Any nodes not
151:             * in sequenceList will remain in their original order.  WARNING: Whitespace and text get pushed to the end 
152:             * of the sorted items.  Pretty print should probably be called after sorting.
153:             *  
154:             * @param top The top level element to do the sort within
155:             * @param sequenceList List of element names and the order they should appear in
156:             */
157:            public static void sortNodes(Element top, String sequenceList[]) {
158:                List contentList = top.content();
159:                int index = 0;
160:
161:                // walk to the first element node
162:                while ((index < contentList.size())
163:                        && (((Node) contentList.get(index)).getNodeType() != Node.ELEMENT_NODE)) {
164:                    ++index;
165:                }
166:
167:                // put each node from the sequenceList in order
168:                for (int i = 0; i < sequenceList.length; ++i) {
169:                    List nodeList = top.selectNodes(sequenceList[i]);
170:
171:                    for (int j = 0; j < nodeList.size(); ++j) {
172:                        Node aNode = (Node) nodeList.get(j);
173:                        aNode.detach();
174:                        contentList.add(index++, aNode);
175:                    }
176:                }
177:            }
178:
179:            /**
180:             * Appends a newline after each element with the passed in name starting with top elememt.
181:             * @param top The parent element within the tree to start looking
182:             * @param elementName The name of the element to append a newline to
183:             */
184:
185:            public static void appendNL(Element top, String elementName) {
186:                if (elementName == null) {
187:                    return;
188:                }
189:
190:                List contentList = top.content();
191:                int index = 0;
192:
193:                while (index < contentList.size()) {
194:                    if (elementName.equals(((Node) contentList.get(index))
195:                            .getName())) {
196:                        index++;
197:                        contentList.add(index, DocumentHelper.createText("\n")); //$NON-NLS-1$
198:                    }
199:                    ++index;
200:                }
201:            }
202:
203:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.