|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| XMLUtil.java | 62.5% | 71.4% | 81.8% | 71.1% |
|
||||||||||||||
| 1 |
package junit.extensions.xml;
|
|
| 2 |
|
|
| 3 |
import org.w3c.dom.Document;
|
|
| 4 |
import org.w3c.dom.Element;
|
|
| 5 |
import org.w3c.dom.Node;
|
|
| 6 |
|
|
| 7 |
import org.xml.sax.SAXException;
|
|
| 8 |
|
|
| 9 |
import java.io.File;
|
|
| 10 |
import java.io.FileInputStream;
|
|
| 11 |
import java.io.FileNotFoundException;
|
|
| 12 |
import java.io.IOException;
|
|
| 13 |
import java.io.InputStream;
|
|
| 14 |
import java.io.OutputStream;
|
|
| 15 |
import java.io.UnsupportedEncodingException;
|
|
| 16 |
|
|
| 17 |
import java.net.URL;
|
|
| 18 |
|
|
| 19 |
import javax.xml.parsers.DocumentBuilderFactory;
|
|
| 20 |
import javax.xml.parsers.ParserConfigurationException;
|
|
| 21 |
|
|
| 22 |
|
|
| 23 |
/**
|
|
| 24 |
* Interface layer to w3c dom.
|
|
| 25 |
*
|
|
| 26 |
* @author Kevin Wilson
|
|
| 27 |
*/
|
|
| 28 |
public final class XMLUtil implements XMLConstants { |
|
| 29 |
/**
|
|
| 30 |
* Private constructor: so that this class is not instantiated.
|
|
| 31 |
*/
|
|
| 32 | 0 |
private XMLUtil() {
|
| 33 |
} |
|
| 34 |
|
|
| 35 |
/**
|
|
| 36 |
* A utility method that will get the attribute value,
|
|
| 37 |
* and if not found, will return null.
|
|
| 38 |
*
|
|
| 39 |
* @param element The element to be searched
|
|
| 40 |
* @param name The name of the attribute to be searched for
|
|
| 41 |
* @return String The string value of the attribute, null
|
|
| 42 |
* if the value is not set or is an empty string
|
|
| 43 |
*/
|
|
| 44 | 8108 |
public static String getAttribute(final Element element, final String name) { |
| 45 | 8108 |
String value = element.getAttribute(name); |
| 46 |
|
|
| 47 | 8108 |
if ((value != null) && (value.length() > 0)) { |
| 48 | 5388 |
return value;
|
| 49 |
} |
|
| 50 |
|
|
| 51 | 2720 |
return null; |
| 52 |
} |
|
| 53 |
|
|
| 54 |
/**
|
|
| 55 |
* A utility method that will get the attribute value, and if not found, will return false.
|
|
| 56 |
*
|
|
| 57 |
* @param element The element to be searched
|
|
| 58 |
* @param name The name of the attribute to be searched for
|
|
| 59 |
* @return boolean The boolean value of the attribute, false if the value is not set or is an
|
|
| 60 |
* empty string
|
|
| 61 |
*/
|
|
| 62 | 2466 |
public static boolean getBooleanAttributeValue(final Element element, |
| 63 |
final String name) {
|
|
| 64 | 2466 |
String value = element.getAttribute(name); |
| 65 |
|
|
| 66 | 2466 |
return new Boolean(value).booleanValue(); |
| 67 |
} |
|
| 68 |
|
|
| 69 |
/**
|
|
| 70 |
* The name of the Node. First, checks the attribute value,
|
|
| 71 |
* and if that is null, uses the
|
|
| 72 |
* <code>getNodeName()</code> method on the Node interface.
|
|
| 73 |
*
|
|
| 74 |
* @param node The Node whose name is to be found.
|
|
| 75 |
* @return String The name of the node (priority is given to the
|
|
| 76 |
* attribute called 'name')
|
|
| 77 |
*/
|
|
| 78 | 964 |
public static String getName(final Node node) { |
| 79 | 964 |
String name = null;
|
| 80 |
|
|
| 81 | 964 |
if (node instanceof Element) { |
| 82 | 964 |
name = getAttribute(((Element) node), NAME); |
| 83 |
} |
|
| 84 |
|
|
| 85 | 964 |
if (name != null) { |
| 86 | 962 |
return name;
|
| 87 |
} |
|
| 88 |
|
|
| 89 | 2 |
return node.getNodeName();
|
| 90 |
} |
|
| 91 |
|
|
| 92 |
/**
|
|
| 93 |
* A utility method to build the path of the Node
|
|
| 94 |
* (with '.' are the separators).
|
|
| 95 |
*
|
|
| 96 |
* @param node The Node whose path is to be found.
|
|
| 97 |
* @return String The String representation of the path.
|
|
| 98 |
*/
|
|
| 99 | 963 |
public static String getPath(final Node node) { |
| 100 | 963 |
StringBuffer buf = new StringBuffer(500);
|
| 101 |
|
|
| 102 | 963 |
if ((node.getParentNode() != null) |
| 103 |
&& node.getParentNode() instanceof Element) {
|
|
| 104 | 202 |
buf.append(getPath(node.getParentNode())); |
| 105 | 202 |
buf.append(".");
|
| 106 |
} |
|
| 107 |
|
|
| 108 | 963 |
buf.append(getName(node)); |
| 109 |
|
|
| 110 | 963 |
return buf.toString();
|
| 111 |
} |
|
| 112 |
|
|
| 113 |
/**
|
|
| 114 |
* Resolve the path to the file.
|
|
| 115 |
* @param file File path to resolve.
|
|
| 116 |
* @return String absolute path or URL.
|
|
| 117 |
*/
|
|
| 118 | 0 |
public static String getURLFromClassPath(final String file) { |
| 119 | 0 |
URL url = Thread.currentThread().getContextClassLoader().getResource(file); |
| 120 |
|
|
| 121 | 0 |
if (url != null) { |
| 122 | 0 |
return url.toString();
|
| 123 |
} |
|
| 124 |
|
|
| 125 | 0 |
return new File(file).getAbsolutePath(); |
| 126 |
} |
|
| 127 |
|
|
| 128 |
/**
|
|
| 129 |
* Checks the element for an attribute with the given name.
|
|
| 130 |
* @param element element to be checked.
|
|
| 131 |
* @param name Name of the attribute.
|
|
| 132 |
* @return true if the attribute exists.
|
|
| 133 |
*/
|
|
| 134 | 5503 |
public static boolean hasAttribute(final Element element, final String name) { |
| 135 | 5503 |
return element.hasAttribute(name);
|
| 136 |
} |
|
| 137 |
|
|
| 138 |
/**
|
|
| 139 |
* This method is used to parse the xml file with the specified name.
|
|
| 140 |
*
|
|
| 141 |
* @param stream The input stream of the xml file to be parsed.
|
|
| 142 |
* @return Document The root Document object.
|
|
| 143 |
*/
|
|
| 144 | 317 |
public static Document parse(final InputStream stream) { |
| 145 | 317 |
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
| 146 |
|
|
| 147 |
//factory.setValidating(true);
|
|
| 148 |
//factory.setNamespaceAware(true);
|
|
| 149 | 317 |
try {
|
| 150 | 317 |
return factory.newDocumentBuilder().parse(stream);
|
| 151 |
} catch (SAXException sxe) {
|
|
| 152 |
// Error generated during parsing
|
|
| 153 | 0 |
Exception x = sxe; |
| 154 |
|
|
| 155 | 0 |
if (sxe.getException() != null) { |
| 156 | 0 |
x = sxe.getException(); |
| 157 |
} |
|
| 158 |
|
|
| 159 | 0 |
x.printStackTrace(); |
| 160 | 0 |
throw new RuntimeException(x.toString()); |
| 161 |
} catch (ParserConfigurationException pce) {
|
|
| 162 |
// Parser with specified options can't be built
|
|
| 163 | 0 |
pce.printStackTrace(); |
| 164 | 0 |
throw new XMLException("Parser configuration exception."); |
| 165 |
} catch (IOException ioe) {
|
|
| 166 |
// I/O error
|
|
| 167 | 0 |
ioe.printStackTrace(); |
| 168 | 0 |
throw new XMLException("IO Exception while parsing XML doc."); |
| 169 |
} finally {
|
|
| 170 | 317 |
if (stream != null) { |
| 171 | 317 |
try {
|
| 172 | 317 |
stream.close(); |
| 173 |
} catch (IOException ex) {
|
|
| 174 | 0 |
throw new XMLException( |
| 175 |
"IO Exception while closing stream: " + ex);
|
|
| 176 |
} |
|
| 177 |
} |
|
| 178 |
} |
|
| 179 |
} |
|
| 180 |
|
|
| 181 |
/**
|
|
| 182 |
* A utility method to find a file from the context of the specified class and read its
|
|
| 183 |
* contents. The difference between this method and the {@link #readFileFromClasspath(String)}
|
|
| 184 |
* method is that this one uses the classpath, and then appends the package information of the
|
|
| 185 |
* specified class and then looks for the specified file.
|
|
| 186 |
*
|
|
| 187 |
* @param clz Class whos resource loader should be used.
|
|
| 188 |
* @param fileName The name of the file to be read.
|
|
| 189 |
* @return InputStream The file contents as an InputStream.
|
|
| 190 |
*/
|
|
| 191 | 3 |
public static InputStream readFileFromClassContext(final Class clz, |
| 192 |
final String fileName) {
|
|
| 193 | 3 |
return clz.getResourceAsStream(fileName);
|
| 194 |
} |
|
| 195 |
|
|
| 196 |
/**
|
|
| 197 |
* A utility method to find a file from the classpath and read its contents.
|
|
| 198 |
*
|
|
| 199 |
* @param fileName The name of the file to be read.
|
|
| 200 |
* @return InputStream The file contents as an InputStream.
|
|
| 201 |
*/
|
|
| 202 | 317 |
public static InputStream readFileFromClasspath(final String fileName) { |
| 203 | 317 |
InputStream is = Thread.currentThread().getContextClassLoader() |
| 204 |
.getResourceAsStream(fileName); |
|
| 205 |
|
|
| 206 | 317 |
if (is == null) { |
| 207 |
// Try local directory
|
|
| 208 | 5 |
try {
|
| 209 | 5 |
return new FileInputStream(fileName); |
| 210 |
} catch (FileNotFoundException ex) {
|
|
| 211 | 2 |
throw new RuntimeException("Could not open input file:" |
| 212 |
+ fileName); |
|
| 213 |
} |
|
| 214 |
} |
|
| 215 |
|
|
| 216 | 312 |
return is;
|
| 217 |
} |
|
| 218 |
|
|
| 219 |
/**
|
|
| 220 |
* Write the XML Document with the given encoding to a file.
|
|
| 221 |
* @param encoding Encoding to use when writing the file.
|
|
| 222 |
* @param strm Stream to write the document.
|
|
| 223 |
* @param doc XML Document to be written.
|
|
| 224 |
* @throws UnsupportedEncodingException thrown if the implementation
|
|
| 225 |
* of the encoding cannot be found.
|
|
| 226 |
*/
|
|
| 227 | 10 |
public static void writeFile(final String encoding, |
| 228 |
final OutputStream strm, final Document doc) |
|
| 229 |
throws UnsupportedEncodingException {
|
|
| 230 | 10 |
XMLWriter wr = new XMLWriter(false); |
| 231 | 10 |
wr.setOutput(strm, encoding); |
| 232 | 10 |
wr.write(doc); |
| 233 |
} |
|
| 234 |
} |
|
| 235 |
|
|
||||||||||