1 17 18 package org.sape.carbon.core.config.format.jdom; 19 20 import java.io.IOException ; 21 import java.io.InputStream ; 22 import java.io.InputStreamReader ; 23 import java.io.OutputStream ; 24 import java.io.StringReader ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 28 import javax.xml.parsers.SAXParser ; 29 import javax.xml.parsers.SAXParserFactory ; 30 31 import org.sape.carbon.core.config.format.ConfigurationDataFormatService; 32 import org.sape.carbon.core.config.format.ConfigurationFormatException; 33 import org.sape.carbon.core.exception.ExceptionUtility; 34 35 import org.apache.commons.logging.Log; 36 import org.apache.commons.logging.LogFactory; 37 import org.jdom.Document; 38 import org.jdom.Element; 39 import org.jdom.JDOMException; 40 import org.jdom.Namespace; 41 import org.jdom.input.SAXBuilder; 42 import org.jdom.output.XMLOutputter; 43 44 55 public class JDOMConfigurationFactory 56 implements ConfigurationDataFormatService { 57 58 61 private Log log = LogFactory.getLog(this.getClass()); 62 63 64 private static final String SCHEMA_URI = 65 "http://www.w3.org/2001/XMLSchema-instance"; 66 67 68 private static final String XERCES_SAX_PARSER = 69 "org.apache.xerces.jaxp.SAXParserImpl"; 70 71 84 public Document readConfigurationStreamToData(String name, InputStream in) 85 throws ConfigurationFormatException { 86 87 Document document = null; 88 ConfigEntityResolver configEntityResolver = 89 new ConfigEntityResolver(name); 90 91 String inputStreamString = convertStreamToString(name, in); 99 100 try { 101 SAXBuilder validatingBuilder = new SAXBuilder(true); 104 validatingBuilder.setEntityResolver(configEntityResolver); 105 106 if (XERCES_SAX_PARSER.equals(getSaxParserClassName())) { 108 validatingBuilder.setFeature( 109 "http://apache.org/xml/features/validation/schema", true); 110 } 111 112 113 StringReader stringReader = new StringReader (inputStreamString); 114 document = validatingBuilder.build(stringReader); 115 116 if (log.isTraceEnabled()) { 117 log.trace("Validated configuration [" + name 118 + "]"); 119 } 120 121 } catch (JDOMException jde) { 122 125 try { 126 SAXBuilder nonvalidatingBuilder = new SAXBuilder(false); 127 nonvalidatingBuilder.setEntityResolver(configEntityResolver); 128 StringReader stringReader = new StringReader (inputStreamString); 129 document = nonvalidatingBuilder.build(stringReader); 130 131 if (containsValidatingDocument(document)) { 137 if (log.isWarnEnabled()) { 138 log.warn( 139 "Validation failed for configuration [" + name 140 + "]: " + jde); 141 } 142 } else { 143 if (log.isTraceEnabled()) { 144 log.trace("No validating document supplied for " 145 + "configuration [" 146 + name 147 + "]"); 148 } 149 } 150 151 } catch (JDOMException jde2) { 152 throw new ConfigurationFormatException( 153 this.getClass(), 154 "Unable to parse Configuration Data from input stream.", 155 jde); 156 } 157 } 158 return document; 159 } 160 161 168 protected boolean containsValidatingDocument(Document document) { 169 return (containsDtd(document) || containsSchema(document)); 170 } 171 172 179 protected boolean containsDtd(Document document) { 180 return (document.getDocType() != null); 181 } 182 183 190 protected boolean containsSchema(Document document) { 191 boolean containsSchema = false; 192 193 Element rootElement = document.getRootElement(); 194 List additionalNamespaces = rootElement.getAdditionalNamespaces(); 195 if (additionalNamespaces != null) { 196 Iterator additionalNamespacesIterator = 197 additionalNamespaces.iterator(); 198 199 while (additionalNamespacesIterator.hasNext()) { 200 Namespace namespace = 201 (Namespace) additionalNamespacesIterator.next(); 202 203 if (SCHEMA_URI.equals(namespace.getURI())) { 204 208 String noNamespaceSchemaLocation = 209 rootElement.getAttributeValue( 210 "noNamespaceSchemaLocation", namespace); 211 212 String schemaLocation = 213 rootElement.getAttributeValue( 214 "schemaLocation", namespace); 215 216 if (noNamespaceSchemaLocation != null 217 || schemaLocation != null) { 218 219 containsSchema = true; 220 } 221 } 222 } 223 } 224 225 return containsSchema; 226 } 227 228 238 protected String convertStreamToString(String name, InputStream in) 239 throws ConfigurationFormatException { 240 241 InputStreamReader inputStreamReader = new 242 InputStreamReader (in); 243 244 StringBuffer inputStreamStringBuffer = new StringBuffer (); 245 246 try { 247 char[] cbuf = new char[8096]; 248 int charRead; 249 String characters; 250 251 252 charRead = inputStreamReader.read(cbuf, 0, cbuf.length); 253 while (charRead >= 0) { 254 characters = new String (cbuf, 0, charRead); 255 inputStreamStringBuffer.append(characters); 256 charRead = inputStreamReader.read(cbuf, 0, cbuf.length); 257 } 258 } catch (IOException ioe) { 259 throw new ConfigurationFormatException( 260 this.getClass(), 261 "Unable to convert configuration [" + name 262 + "] into a StringReader: " 263 + ExceptionUtility.printStackTracesToString(ioe)); 264 265 } 266 267 return inputStreamStringBuffer.toString(); 268 } 269 270 276 protected static String getSaxParserClassName() { 277 try { 278 SAXParserFactory saxParserFactory = SAXParserFactory.newInstance(); 279 SAXParser saxParser = saxParserFactory.newSAXParser(); 280 return saxParser.getClass().getName(); 281 } catch (Exception e) { 282 return null; 283 } 284 } 285 286 287 298 public void writeConfigurationStreamToData( 299 Document document, 300 OutputStream out) 301 throws ConfigurationFormatException { 302 303 try { 304 XMLOutputter outputter = new XMLOutputter(" ", true); 305 306 outputter.output(document, out); 307 } catch (IOException ioe) { 308 throw new ConfigurationFormatException( 309 this.getClass(), 310 "Failed to write configuration document to output stream " 311 + "in xml format.", ioe); 312 } 313 } 314 315 } | Popular Tags |