2ronwalf// The MIT License
2ronwalf//
2ronwalf// Copyright (c) 2004 Evren Sirin
2ronwalf//
2ronwalf// Permission is hereby granted, free of charge, to any person obtaining a copy
2ronwalf// of this software and associated documentation files (the "Software"), to
2ronwalf// deal in the Software without restriction, including without limitation the
2ronwalf// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
2ronwalf// sell copies of the Software, and to permit persons to whom the Software is
2ronwalf// furnished to do so, subject to the following conditions:
2ronwalf//
2ronwalf// The above copyright notice and this permission notice shall be included in
2ronwalf// all copies or substantial portions of the Software.
2ronwalf//
2ronwalf// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2ronwalf// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2ronwalf// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
2ronwalf// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2ronwalf// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
2ronwalf// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
2ronwalf// IN THE SOFTWARE.
2ronwalf
2ronwalfpackage org.mindswap.utils;
2ronwalf
2ronwalfimport java.io.BufferedReader;
2ronwalfimport java.io.FileNotFoundException;
2ronwalfimport java.io.FileReader;
2ronwalfimport java.io.IOException;
2ronwalfimport java.io.InputStreamReader;
2ronwalfimport java.io.Reader;
2ronwalfimport java.io.StringReader;
2ronwalfimport java.io.StringWriter;
2ronwalfimport java.net.URL;
2ronwalfimport java.util.Hashtable;
2ronwalfimport java.util.Iterator;
2ronwalfimport java.util.Map;
2ronwalf
2ronwalfimport org.apache.xml.serialize.OutputFormat;
2ronwalfimport org.apache.xml.serialize.XMLSerializer;
2ronwalfimport org.mindswap.owl.vocabulary.RDF;
2ronwalfimport org.w3c.dom.Attr;
2ronwalfimport org.w3c.dom.Document;
2ronwalfimport org.w3c.dom.Node;
2ronwalfimport org.w3c.dom.NodeList;
2ronwalfimport org.xml.sax.ErrorHandler;
2ronwalfimport org.xml.sax.SAXParseException;
2ronwalf
2ronwalfpublic class Utils {
2ronwalf public static boolean DEBUG = false;
2ronwalf
2ronwalf public static Object getHashtableKey(Hashtable h, Object value) {
2ronwalf Iterator e = h.entrySet().iterator();
2ronwalf while(e.hasNext()) {
2ronwalf Map.Entry entry = (Map.Entry) e.next();
2ronwalf
2ronwalf if(entry.getValue().equals(value))
2ronwalf return entry.getKey();
2ronwalf }
2ronwalf
2ronwalf return null;
2ronwalf }
2ronwalf
2ronwalf
2ronwalf public static void printTime(String msg) {
2ronwalf System.out.println("Time: (" + System.currentTimeMillis() + ") " + msg);
2ronwalf }
2ronwalf
2ronwalf public static String toString(Object[] array) {
2ronwalf String s = "[Array ";
2ronwalf if(array != null && array.length > 0) {
2ronwalf s += array[0];
2ronwalf for(int i = 1; i < array.length; i++)
2ronwalf s += "," + array[i];
2ronwalf }
2ronwalf s += "]";
2ronwalf
2ronwalf return s;
2ronwalf }
2ronwalf
2ronwalf public static Node getAsNode(String in) {
2ronwalf try {
2ronwalf org.apache.xerces.parsers.DOMParser parser =
2ronwalf new org.apache.xerces.parsers.DOMParser();
2ronwalf
2ronwalf parser.setErrorHandler(new ErrorHandler() {
2ronwalf public void warning(SAXParseException exception) {
2ronwalf if(DEBUG) System.err.println(exception);
2ronwalf }
2ronwalf
2ronwalf public void error(SAXParseException exception) {
2ronwalf if(DEBUG) System.err.println(exception);
2ronwalf }
2ronwalf
2ronwalf public void fatalError(SAXParseException exception) {
2ronwalf if(DEBUG) System.err.println(exception);
2ronwalf }
2ronwalf });
2ronwalf
2ronwalf parser.parse(new org.xml.sax.InputSource(
2ronwalf new StringReader(in)));
2ronwalf
2ronwalf return parser.getDocument().getDocumentElement();
2ronwalf } catch(Exception e) {
2ronwalf //System.out.println("Invalid XML " + in + " " + e);
2ronwalf //e.printStackTrace();
2ronwalf }
2ronwalf
2ronwalf return null;
2ronwalf }
2ronwalf
2ronwalf public static boolean getBoolean(String str) {
2ronwalf return (str == null) ? false :
2ronwalf str.toLowerCase().equals("true") ||
2ronwalf str.equals("1");
2ronwalf }
2ronwalf
2ronwalf public static String toString(boolean b) {
2ronwalf return b ? "true" : "false";
2ronwalf }
2ronwalf
2ronwalf public static String readURL(URL fileURL) throws IOException {
2ronwalf return readAll(new InputStreamReader(fileURL.openStream()));
2ronwalf }
2ronwalf
2ronwalf public static String readFile(String fileName) throws FileNotFoundException, IOException {
2ronwalf return readAll(new FileReader(fileName));
2ronwalf }
2ronwalf
2ronwalf public static String readAll(Reader reader) throws IOException {
2ronwalf StringBuffer buffer = new StringBuffer();
2ronwalf
2ronwalf BufferedReader in = new BufferedReader(reader);
2ronwalf int ch;
2ronwalf while ((ch = in.read()) > -1) {
2ronwalf buffer.append((char)ch);
2ronwalf }
2ronwalf in.close();
2ronwalf
2ronwalf return buffer.toString();
2ronwalf }
2ronwalf
2ronwalf public static String formatXML( Node node ) {
2ronwalf try {
2ronwalf StringWriter out = new StringWriter();
2ronwalf Document doc = node.getOwnerDocument();
2ronwalf OutputFormat format = new OutputFormat( doc );
2ronwalf format.setIndenting( true );
2ronwalf format.setLineWidth( 0 );
2ronwalf format.setPreserveSpace( false );
2ronwalf format.setOmitXMLDeclaration( false );
2ronwalf
2ronwalf XMLSerializer serial = new XMLSerializer( out, format );
2ronwalf serial.asDOMSerializer();
2ronwalf
2ronwalf serial.serialize( doc );
2ronwalf
2ronwalf return out.toString();
2ronwalf }
2ronwalf catch( IOException e ) {
2ronwalf System.err.println( "Problem serializing node " + e );
2ronwalf
2ronwalf return "Problem serializing node " + e;
2ronwalf }
2ronwalf }
2ronwalf
2ronwalf public static String formatRDF(String rdf) {
2ronwalf Node node = getAsNode(rdf);
2ronwalf
2ronwalf if(node == null)
2ronwalf return rdf;
2ronwalf else
2ronwalf return formatNode(node, " ").substring(System.getProperty("line.separator").length());
2ronwalf }
2ronwalf
2ronwalf
2ronwalf public static String formatNode(Node node, String indent) {
2ronwalf String str = "";
2ronwalf int type = node.getNodeType();
2ronwalf
2ronwalf if(type == Node.TEXT_NODE) {
2ronwalf str = node.getNodeValue().trim();
2ronwalf }
2ronwalf else if(type == Node.ELEMENT_NODE) {
2ronwalf if(!(node.getParentNode() instanceof org.w3c.dom.Document))
2ronwalf str = System.getProperty("line.separator") + indent + node.getLocalName() + ": ";
2ronwalf
2ronwalf NodeList children = node.getChildNodes();
2ronwalf int len = (children != null) ? children.getLength() : 0;
2ronwalf
2ronwalf for (int i = 0; i < len; i++) {
2ronwalf str += formatNode(children.item(i), indent + " ");
2ronwalf }
2ronwalf if(len == 0) {
2ronwalf Node rdfResource = node.getAttributes().getNamedItemNS(RDF.getURI().toString(), "resource");
2ronwalf
2ronwalf if(rdfResource instanceof Attr)
2ronwalf str += URIUtils.getLocalName(((Attr)rdfResource).getValue());
2ronwalf
2ronwalf }
2ronwalf }
2ronwalf
2ronwalf return str;
2ronwalf }
2ronwalf}