DBVIEW
|
00001 /* 00002 DbView - Graph Visualization 00003 Copyright (C) 2012 Denis BEURIVE 00004 00005 This program is free software: you can redistribute it and/or modify 00006 it under the terms of the GNU General Public License as published by 00007 the Free Software Foundation, either version 3 of the License, or 00008 (at your option) any later version. 00009 00010 This program is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 GNU General Public License for more details. 00014 00015 You should have received a copy of the GNU General Public License 00016 along with this program. If not, see <http://www.gnu.org/licenses/>. 00017 */ 00018 00019 /** 00020 * @author Denis Beurive 00021 */ 00022 package org.dbview.utils; 00023 00024 import java.util.List; 00025 00026 import org.jdom.*; 00027 import org.jdom.filter.*; 00028 import org.jdom.output.XMLOutputter; 00029 00030 /** 00031 * This class implements utilities used to manipulate XML through JDOM. 00032 * @author Denis Beurive 00033 */ 00034 public class Jdom 00035 { 00036 /** 00037 * Filter for TEXT. 00038 * @see JDOM documentation. 00039 */ 00040 private static Filter __filter_text = new ContentFilter(ContentFilter.TEXT); 00041 00042 /** 00043 * Filter for CDATA. 00044 * @see JDOM documentation. 00045 */ 00046 private static Filter __filter_cdata = new ContentFilter(ContentFilter.CDATA); 00047 00048 /** 00049 * This method extracts the content of a given element. 00050 * It assumes that it is a CDTA or a TEXT. 00051 * @param in_element Element 00052 * @return The method may return: 00053 * o A string that represents the content of the element. 00054 * o null, if the element is empty or if the specified element is null. 00055 */ 00056 public static String getTextOrCdata(Element in_element) throws Exception 00057 { 00058 List<Content> c = null; 00059 00060 if (null == in_element) { return null; } 00061 00062 // First, we try to get a content which is CDATA. This may fail for two reasons : 00063 // o The content may not be a CDATA. 00064 // o The content may be "empty". 00065 c = (List<Content>)in_element.getContent(Jdom.__filter_cdata); 00066 if (1 == c.size()) { return ((CDATA)c.get(0)).getText(); } 00067 if (1 < c.size()) { throw new Exception("Invalid XML element: " + in_element.toString()); } 00068 00069 // This is not a CDATA.. So it should be a TEXT. 00070 c = (List<Content>)in_element.getContent(Jdom.__filter_text); 00071 if (0 == c.size()) { return null; } 00072 if (1 < c.size()) { throw new Exception("Invalid XML element: " + in_element.toString()); } 00073 return ((Text)c.get(0)).getTextTrim(); 00074 } 00075 00076 /** 00077 * Prints a XML element. 00078 * @param in_element XML element to print. 00079 * @return The method returns a string that represents the XML element. 00080 * @throws Exception 00081 */ 00082 public static String print(Element in_element) throws Exception 00083 { 00084 XMLOutputter outputter = new XMLOutputter(); 00085 outputter.setFormat(org.jdom.output.Format.getPrettyFormat()); 00086 return outputter.outputString(in_element); 00087 } 00088 00089 /** 00090 * Prints a XML document. 00091 * @param in_doc XML document to print. 00092 * @return The method returns a string that represents the XML document. 00093 */ 00094 public static String print(Document in_doc) throws Exception 00095 { 00096 XMLOutputter outputter = new XMLOutputter(); 00097 outputter.setFormat(org.jdom.output.Format.getPrettyFormat()); 00098 return outputter.outputString(in_doc); 00099 } 00100 }