1 16 19 20 import java.io.PrintWriter ; 21 import java.io.StringWriter ; 22 23 import javax.xml.transform.Transformer ; 24 import javax.xml.transform.TransformerFactory ; 25 import javax.xml.transform.ErrorListener ; 26 import javax.xml.transform.stream.StreamResult ; 27 import javax.xml.transform.stream.StreamSource ; 28 29 import javax.ejb.SessionBean ; 30 import javax.ejb.SessionContext ; 31 32 35 public class TransformBean implements SessionBean { 36 37 private SessionContext m_context = null; 38 39 private final static String nullErrorMsg = 40 "<h1>XSL transformation error</h1>"+ 41 "<p>'null' parameters sent to the XSL transformation bean's "+ 42 "<tt>transform(String document, String translet)</tt> method.</p>"; 43 44 private static final String NAMESPACE_FEATURE = 45 "http://xml.org/sax/features/namespaces"; 46 47 50 private void errorMsg(PrintWriter out, Exception e, String msg) { 51 out.println("<h1>Error</h1>"); 52 out.println("<p>"+msg+"</p><br>"); 53 out.println(e.toString()); 54 } 55 56 59 public String transform(String document, String transletName) { 60 61 final StringWriter sout = new StringWriter (); 63 final PrintWriter out = new PrintWriter (sout); 64 65 try { 66 if ((document == null) || (transletName == null)) { 67 out.println(nullErrorMsg); 68 } 69 else { 70 TransformerFactory tf = TransformerFactory.newInstance(); 71 try { 72 tf.setAttribute("use-classpath", Boolean.TRUE); 73 } catch (IllegalArgumentException iae) { 74 System.err.println( 75 "Could not set XSLTC-specific TransformerFactory " 76 + "attributes. Transformation failed."); 77 } 78 79 Transformer t = 80 tf.newTransformer(new StreamSource (transletName)); 81 82 final long start = System.currentTimeMillis(); 84 t.transform(new StreamSource (document), 85 new StreamResult (out)); 86 final long done = System.currentTimeMillis() - start; 87 out.println("<!-- transformed by XSLTC in "+done+"msecs -->"); 88 } 89 } 90 91 catch (Exception e) { 92 errorMsg(out, e, "Impossible state reached."); 93 } 94 95 out.close(); 98 return sout.toString(); 99 } 100 101 104 public void setSessionContext(SessionContext context) { 105 m_context = context; 106 } 107 108 public void ejbCreate() { } 110 public void ejbRemove() { } 111 public void ejbActivate() { } 112 public void ejbPassivate() { } 113 public void ejbLoad() { } 114 public void ejbStore() { } 115 } 116 | Popular Tags |