KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > clipbuilder > html > util > JDomUtilities


1 package org.jahia.clipbuilder.html.util;
2
3 import org.jdom.*;
4 import org.jdom.output.*;
5 import org.jdom.xpath.*;
6
7 import java.util.*;
8
9 /**
10  * Utilities for JDOM
11  *
12  *@author Tlili Khaled
13  */

14 public abstract class JDomUtilities {
15     private static org.apache.log4j.Logger logger =
16             org.apache.log4j.Logger.getLogger(JDomUtilities.class);
17
18
19     /**
20      * Constructor for the JDomUtilities object
21      */

22     public JDomUtilities() {
23     }
24
25
26     /**
27      * Gets the Sibling attribute of the JDomUtilities class
28      *
29      *@param first Description of Parameter
30      *@param second Description of Parameter
31      *@return The Sibling value
32      */

33     public static boolean isSibling(Element first, Element second) {
34         boolean result = false;
35         // test if they have same parents
36
Parent parentFirst = first.getParent();
37         Parent parentSecond = second.getParent();
38         if (parentFirst.equals(parentSecond)) {
39             logger.debug("[ " + first.getName() + " and " + second.getName() +
40                     " are sibling]");
41             logger.debug("[Parent is " + first.getParentElement().getName() + "]");
42             result = true;
43         }
44         else {
45             logger.debug("[ " + first.getName() + " and " + second.getName() +
46                     " are not sibling]");
47             logger.debug("[Parent first is " + first.getParentElement().getName() +
48                     "]");
49             logger.debug("[Parent second is " + second.getParentElement().getName() +
50                     "]");
51
52             result = false;
53         }
54         return result;
55     }
56
57
58     /**
59      * Gets the DocumentAsString attribute of the JDomUtilities class
60      *
61      *@param doc Description of Parameter
62      *@return The DocumentAsString value
63      */

64     public static String JavaDoc getDocumentAsString(Document doc) {
65         String JavaDoc res = "<p> Error !!! </p>";
66         XMLOutputter serializer = new XMLOutputter();
67         serializer.setFormat(org.jdom.output.Format.getPrettyFormat());
68         res = serializer.outputString(doc);
69         logger.debug("[document from neko and jdom] " + doc);
70         //return res;
71
return res;
72     }
73
74
75     /**
76      * Gets the DocumentAsString attribute of the JDomUtilities class
77      *
78      *@param xmlContent Description of Parameter
79      *@return The DocumentAsString value
80      */

81     public static Document getDocument(String JavaDoc xmlContent) {
82         org.jdom.input.DOMBuilder builder = new org.jdom.input.DOMBuilder();
83         org.jdom.Document jdomDoc = builder.build(org.jahia.clipbuilder.html.util.DomUtilities.getDocument(xmlContent));
84         return jdomDoc;
85     }
86
87
88     /**
89      * Gets the ElementByXPath attribute of the JDomUtilities class
90      *
91      *@param doc Description of Parameter
92      *@param xPath Description of Parameter
93      *@return The ElementByXPath value
94      *@exception JDOMException Description of Exception
95      */

96     public static List getElementByXPath(Document doc, String JavaDoc xPath) throws JDOMException {
97         List result = null;
98         XPath x = XPath.newInstance(xPath);
99         result = null;
100         result = x.selectNodes(doc);
101         if (result.isEmpty()) {
102             logger.debug("Result set is empty for xpath = " + xPath);
103         }
104         else {
105             logger.debug("Result set is not empty for xpath = " + xPath);
106         }
107         return result;
108     }
109
110 }
111
Popular Tags