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("<", "<"); //$NON-NLS-1$ //$NON-NLS-2$
074: string = string.replaceAll(">", ">"); //$NON-NLS-1$ //$NON-NLS-2$
075: string = string.replaceAll("&", "&"); //$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("<", "<"); //$NON-NLS-1$//$NON-NLS-2$
085: string = string.replaceAll(">", ">"); //$NON-NLS-1$//$NON-NLS-2$
086: string = string.replaceAll("&", "&"); //$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: }
|