1 19 package org.netbeans.spi.xml.cookies; 20 21 import java.io.IOException ; 22 import java.io.PrintWriter ; 23 import java.io.StringWriter ; 24 import java.net.URL ; 25 import java.security.ProtectionDomain ; 26 import java.security.CodeSource ; 27 28 import org.xml.sax.EntityResolver ; 29 import org.xml.sax.XMLReader ; 30 import org.xml.sax.InputSource ; 31 import org.xml.sax.SAXException ; 32 import org.xml.sax.SAXParseException ; 33 import org.xml.sax.SAXNotRecognizedException ; 34 import org.xml.sax.SAXNotSupportedException ; 35 import javax.xml.parsers.SAXParser ; 36 import javax.xml.parsers.SAXParserFactory ; 37 import javax.xml.parsers.ParserConfigurationException ; 38 import javax.xml.transform.TransformerFactory ; 39 import javax.xml.transform.Transformer ; 40 import javax.xml.transform.Source ; 41 import javax.xml.transform.Result ; 42 import javax.xml.transform.ErrorListener ; 43 import javax.xml.transform.TransformerException ; 44 import javax.xml.transform.TransformerConfigurationException ; 45 import javax.xml.transform.URIResolver ; 46 import javax.xml.transform.sax.SAXSource ; 47 import javax.xml.transform.stream.StreamSource ; 48 49 import org.openide.filesystems.FileObject; 50 51 import org.netbeans.api.xml.cookies.*; 52 import org.netbeans.api.xml.services.UserCatalog; 53 54 60 public final class TransformableSupport implements TransformableCookie { 61 62 private final Source source; 64 65 private static TransformerFactory transformerFactory; 66 67 68 72 public TransformableSupport (Source source) { 73 if (source == null) throw new NullPointerException (); 74 this.source = source; 75 } 76 77 86 public void transform (Source transformSource, Result outputResult, CookieObserver notifier) throws TransformerException { 87 try { 88 if ( Util.THIS.isLoggable() ) { 89 Util.THIS.debug ("TransformableSupport.transform"); 90 Util.THIS.debug (" transformSource = " + transformSource.getSystemId()); 91 Util.THIS.debug (" outputResult = " + outputResult.getSystemId()); 92 } 93 94 95 Source xmlSource = source; 96 97 if ( Util.THIS.isLoggable() ) Util.THIS.debug (" xmlSource = " + xmlSource.getSystemId()); 98 99 Transformer transformer = newTransformer (transformSource); 101 102 if (notifier != null) { 104 105 107 ProtectionDomain domain = transformer.getClass().getProtectionDomain(); 108 CodeSource codeSource = domain.getCodeSource(); 109 if (codeSource == null) { 110 notifier.receive(new CookieMessage(Util.THIS.getString("BK000", transformer.getClass().getName()))); 111 } else { 112 URL location = codeSource.getLocation(); 113 notifier.receive(new CookieMessage(Util.THIS.getString("BK001", location, transformer.getClass().getName()))); 114 } 115 116 Proxy proxy = new Proxy (notifier); 117 transformer.setErrorListener (proxy); 118 } 119 transformer.transform (xmlSource, outputResult); 120 121 } catch (Exception exc) { if ( Util.THIS.isLoggable() ) { 123 Util.THIS.debug (" EXCEPTION during transformation: " + exc.getClass().getName(), exc); 124 Util.THIS.debug (" exception's message = " + exc.getLocalizedMessage()); 125 126 Throwable tempExc = unwrapException (exc); 127 Util.THIS.debug (" wrapped exception = " + tempExc.getLocalizedMessage()); 128 } 129 130 TransformerException transExcept = null; 131 Object detail = null; 132 133 if ( exc instanceof TransformerException ) { 134 transExcept = (TransformerException )exc; 135 if ( ( notifier != null ) && 136 ( exc instanceof TransformerConfigurationException ) ) { 137 detail = new DefaultXMLProcessorDetail (transExcept); 138 } 139 } else if ( exc instanceof SAXParseException ) { 140 transExcept = new TransformerException (exc); 141 if ( notifier != null ) { 142 detail = new DefaultXMLProcessorDetail ((SAXParseException )exc); 143 } 144 } else { 145 transExcept = new TransformerException (exc); 146 if ( notifier != null ) { 147 detail = new DefaultXMLProcessorDetail (transExcept); 148 } 149 } 150 151 if ( ( notifier != null ) && 152 ( detail != null ) ) { 153 CookieMessage message = new CookieMessage 154 (message(exc), 155 CookieMessage.FATAL_ERROR_LEVEL, 156 detail); 157 notifier.receive (message); 158 } 159 160 if ( Util.THIS.isLoggable() ) Util.THIS.debug ("--> throw transExcept: " + transExcept); 161 162 throw transExcept; 163 } } 165 166 167 171 private static Throwable unwrapException (Throwable exc) { 172 Throwable wrapped = null; 173 if (exc instanceof TransformerException ) { 174 wrapped = ((TransformerException ) exc).getException(); 175 } else if (exc instanceof SAXException ) { 176 wrapped = ((SAXException ) exc).getException(); 177 } else { 178 return exc; 179 } 180 181 if ( wrapped == null ) { 182 return exc; 183 } 184 185 return unwrapException (wrapped); 186 } 187 188 private static URIResolver getURIResolver () { 189 UserCatalog catalog = UserCatalog.getDefault(); 190 URIResolver res = (catalog == null ? null : catalog.getURIResolver()); 191 return res; 192 } 193 194 private static TransformerFactory getTransformerFactory () { 195 if ( transformerFactory == null ) { 196 transformerFactory = TransformerFactory.newInstance(); 197 transformerFactory.setURIResolver (getURIResolver()); } 199 return transformerFactory; 200 } 201 202 203 private static Transformer newTransformer (Source xsl) throws TransformerConfigurationException { 204 return getTransformerFactory().newTransformer (xsl); 205 } 206 207 210 private static String message(Throwable t) { 211 String msg = t.getLocalizedMessage(); 212 return (msg!=null ? msg : new ExceptionWriter(t).toString()); 213 } 214 215 218 private static class ExceptionWriter extends PrintWriter { 219 private int counter = 4; 220 private Throwable t; 221 222 public ExceptionWriter(Throwable t) { 223 super(new StringWriter ()); 224 this.t = t; 225 } 226 227 public void println(String s) { 228 if (counter-- > 0) super.println(s); 229 } 230 231 public void println(Object o) { 232 if (counter-- > 0) super.println(o); 233 } 234 235 public String toString() { 236 t.printStackTrace(this); 237 flush(); 238 return ((StringWriter )out).getBuffer().toString(); 239 } 240 } 241 242 246 private static class Proxy implements ErrorListener { 247 248 private final CookieObserver peer; 249 250 public Proxy (CookieObserver peer) { 251 if (peer == null) { 252 throw new NullPointerException (); 253 } 254 this.peer = peer; 255 } 256 257 public void error (TransformerException tex) throws TransformerException { 258 report (CookieMessage.ERROR_LEVEL, tex); 259 } 260 261 public void fatalError (TransformerException tex) throws TransformerException { 262 report (CookieMessage.FATAL_ERROR_LEVEL, tex); 263 264 throw tex; 265 } 266 267 268 public void warning (TransformerException tex) throws TransformerException { 269 report (CookieMessage.WARNING_LEVEL, tex); 270 } 271 272 private void report (int level, TransformerException tex) throws TransformerException { 273 if ( Util.THIS.isLoggable() ) { 274 Util.THIS.debug ("[TransformableSupport::Proxy]: report [" + level + "]: ", tex); 275 Util.THIS.debug (" exception's message = " + tex.getLocalizedMessage()); 276 277 Throwable tempExc = unwrapException (tex); 278 Util.THIS.debug (" wrapped exception = " + tempExc.getLocalizedMessage()); 279 } 280 281 Throwable unwrappedExc = unwrapException (tex); 282 CookieMessage message = new CookieMessage ( 283 message(unwrappedExc), 284 level, 285 new DefaultXMLProcessorDetail (tex) 286 ); 287 288 peer.receive (message); 289 } 290 291 } 293 } 294 | Popular Tags |