Project: bml_lib License: BSD Dependencies: Used by: |
bml_lib/src/edu/wpi/hri/bml/XMLInterface.javaGo to the documentation of this file.00001 /* 00002 * Software License Agreement (BSD License) 00003 * 00004 * Copyright (c) 2010, Worcester Polytechnic Institute 00005 * All rights reserved. 00006 * 00007 * Redistribution and use in source and binary forms, with or without 00008 * modification, are permitted provided that the following conditions 00009 * are met: 00010 * 00011 * * Redistributions of source code must retain the above copyright 00012 * notice, this list of conditions and the following disclaimer. 00013 * * Redistributions in binary form must reproduce the above 00014 * copyright notice, this list of conditions and the following 00015 * disclaimer in the documentation and/or other materials provided 00016 * with the distribution. 00017 * * Neither the name of Worcester Polytechnic Institute. nor the names 00018 * of its contributors may be used to endorse or promote products 00019 * derived from this software without specific prior written permission. 00020 * 00021 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00022 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00023 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00024 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00025 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00026 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00027 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00028 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00029 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00030 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00031 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00032 * POSSIBILITY OF SUCH DAMAGE. 00033 */ 00034 package edu.wpi.hri.bml; 00035 00036 import java.io.File; 00037 import java.io.Reader; 00038 import java.io.StringWriter; 00039 00040 import javax.xml.parsers.DocumentBuilderFactory; 00041 import javax.xml.transform.OutputKeys; 00042 import javax.xml.transform.Transformer; 00043 import javax.xml.transform.TransformerFactory; 00044 import javax.xml.transform.dom.DOMResult; 00045 import javax.xml.transform.dom.DOMSource; 00046 import javax.xml.transform.stream.StreamResult; 00047 import javax.xml.transform.stream.StreamSource; 00048 00049 import org.w3c.dom.Document; 00050 00051 import edu.wpi.hri.log.Logger; 00052 import edu.wpi.hri.log.Logger.Colors; 00053 import edu.wpi.hri.log.Logger.LoggerLevel; 00054 00063 public class XMLInterface { 00064 00065 private final Logger logger; 00066 private final TransformerFactory transFact; 00067 private final DocumentBuilderFactory dbFact; 00068 00077 public XMLInterface(Logger logger, File schema) { 00078 this.logger = logger.sub(Colors.XML, "XML"); 00079 this.transFact = TransformerFactory.newInstance(); 00080 00081 this.dbFact = DocumentBuilderFactory.newInstance(); 00082 this.dbFact.setNamespaceAware(true); 00083 this.dbFact.setValidating(true); 00084 try { 00085 this.dbFact.setAttribute( 00086 "http://java.sun.com/xml/jaxp/properties/schemaLanguage", 00087 "http://www.w3.org/2001/XMLSchema"); 00088 this.dbFact.setAttribute( 00089 "http://java.sun.com/xml/jaxp/properties/schemaSource", 00090 schema); 00091 } catch (IllegalArgumentException x) { 00092 // Happens if the parser does not support JAXP 1.2 00093 } 00094 this.logger.debug(LoggerLevel.INIT, "Created xml interface successfully"); 00095 } 00096 00106 public Document loadDocument(Reader reader) { 00107 try { 00108 DOMResult result = new DOMResult(); 00109 this.transFact.newTransformer().transform(new StreamSource(reader), 00110 result); 00111 return (Document) result.getNode(); 00112 } catch (Exception e) { 00113 this.logger.error("Error in loading document: " + e.getMessage()); 00114 this.logger.printThrowable(LoggerLevel.ALWAYS, e); 00115 } 00116 return null; 00117 } 00118 00127 public Document createDocument() { 00128 try { 00129 return dbFact.newDocumentBuilder().newDocument(); 00130 } catch (Exception e) { 00131 this.logger.error("Error in creating document: " + e.getMessage()); 00132 this.logger.printThrowable(LoggerLevel.ALWAYS, e); 00133 } 00134 return null; 00135 } 00136 00146 public String convertDocumentToString(Document doc) { 00147 try { 00148 StringWriter stringWriter = new StringWriter(); 00149 Transformer trans = this.transFact.newTransformer(); 00150 trans.setOutputProperty(OutputKeys.INDENT, "yes"); 00151 trans.transform(new DOMSource(doc), new StreamResult(stringWriter)); 00152 return stringWriter.getBuffer().toString(); 00153 } catch (Exception e) { 00154 this.logger.error("Error in to string: " + e.getMessage()); 00155 this.logger.printThrowable(LoggerLevel.ALWAYS, e); 00156 } 00157 return null; 00158 } 00159 } |