KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > web > util > DjenericXSLTransform


1 package com.genimen.djeneric.web.util;
2
3 import java.io.BufferedWriter JavaDoc;
4 import java.io.ByteArrayOutputStream JavaDoc;
5 import java.io.FileOutputStream JavaDoc;
6 import java.io.IOException JavaDoc;
7 import java.io.OutputStreamWriter JavaDoc;
8 import java.util.HashMap JavaDoc;
9
10 import javax.servlet.ServletException JavaDoc;
11 import javax.xml.transform.OutputKeys JavaDoc;
12 import javax.xml.transform.Result JavaDoc;
13 import javax.xml.transform.Source JavaDoc;
14 import javax.xml.transform.Templates JavaDoc;
15 import javax.xml.transform.Transformer JavaDoc;
16 import javax.xml.transform.TransformerConfigurationException JavaDoc;
17 import javax.xml.transform.TransformerException JavaDoc;
18 import javax.xml.transform.TransformerFactory JavaDoc;
19 import javax.xml.transform.TransformerFactoryConfigurationError JavaDoc;
20 import javax.xml.transform.dom.DOMSource JavaDoc;
21 import javax.xml.transform.stream.StreamResult JavaDoc;
22
23 import org.w3c.dom.Document JavaDoc;
24
25 public class DjenericXSLTransform
26 {
27   private static HashMap JavaDoc _cachedTemplates = new HashMap JavaDoc();
28   private static boolean _caching = false;
29   private static String JavaDoc _viewDumpLocation = "/temp/dump";
30   private String JavaDoc _templateRoot;
31
32   public DjenericXSLTransform(String JavaDoc templateRoot)
33   {
34     if (!templateRoot.endsWith("/") && !templateRoot.endsWith("\\")) templateRoot += "/";
35     _templateRoot = templateRoot;
36   }
37
38   public String JavaDoc transform(Document JavaDoc doc, String JavaDoc path) throws IOException JavaDoc, ServletException JavaDoc
39   {
40     try
41     {
42       path = _templateRoot + path;
43
44       DOMSource JavaDoc input = new DOMSource JavaDoc(doc);
45
46       Transformer JavaDoc trans = this.createTransformer(path);
47       ByteArrayOutputStream JavaDoc bos = new ByteArrayOutputStream JavaDoc(50000);
48       Result JavaDoc res = new StreamResult JavaDoc(bos);
49
50       trans.transform(input, res);
51
52       String JavaDoc result = bos.toString("UTF-8");
53
54       if (_viewDumpLocation != null)
55       {
56         String JavaDoc fn = _viewDumpLocation;
57         if (!fn.endsWith("/") && !fn.endsWith("\\")) fn += "/";
58
59         String JavaDoc name = path;
60         int idx = path.lastIndexOf("/");
61         if (idx != -1) name = name.substring(idx + 1);
62
63         BufferedWriter JavaDoc w = new BufferedWriter JavaDoc(
64             new OutputStreamWriter JavaDoc(new FileOutputStream JavaDoc(fn + name + ".html"), "UTF-8"));
65         w.write(result);
66         w.close();
67
68         w = new BufferedWriter JavaDoc(new OutputStreamWriter JavaDoc(new FileOutputStream JavaDoc(fn + name + ".xml"), "UTF-8"));
69         w.write(doc2String(doc));
70         w.close();
71       }
72
73       return result;
74     }
75     catch (TransformerException JavaDoc ex)
76     {
77       throw new ServletException JavaDoc(ex);
78     }
79   }
80
81   public static void clearCache()
82   {
83     _cachedTemplates = new HashMap JavaDoc();
84   }
85
86   private String JavaDoc doc2String(Document JavaDoc doc) throws TransformerConfigurationException JavaDoc,
87       TransformerFactoryConfigurationError JavaDoc, TransformerException JavaDoc
88   {
89     ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
90
91     Transformer JavaDoc serializer = TransformerFactory.newInstance().newTransformer();
92     //serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
93
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
94     serializer.transform(new DOMSource JavaDoc(doc), new StreamResult JavaDoc(baos));
95     return baos.toString();
96   }
97
98   private Transformer JavaDoc createTransformer(String JavaDoc path) throws TransformerException JavaDoc
99   {
100     Templates JavaDoc result = (Templates JavaDoc) _cachedTemplates.get(path);
101     if (result == null || !_caching)
102     {
103       TransformerFactory JavaDoc tFactory = DjenericTransformerFactory.newInstance();
104
105       Source JavaDoc src = tFactory.getURIResolver().resolve(path, "");
106
107       result = tFactory.newTemplates(src);
108       _cachedTemplates.put(path, result);
109     }
110     return result.newTransformer();
111   }
112
113   public static void setViewDumpLocation(String JavaDoc viewDumpLocation)
114   {
115     _viewDumpLocation = viewDumpLocation;
116   }
117
118   public static void setXSLCaching(boolean caching)
119   {
120     _caching = caching;
121   }
122 }
Popular Tags