KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > xml > transform > impl > trax > TRAXTransformerServiceImpl


1 /***************************************************************************
2  * Copyright 2001-2005 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5
6 package org.exoplatform.services.xml.transform.impl.trax;
7
8 import java.io.IOException JavaDoc;
9 import javax.xml.transform.Source JavaDoc;
10 import javax.xml.transform.Templates JavaDoc;
11 import javax.xml.transform.TransformerConfigurationException JavaDoc;
12 import javax.xml.transform.TransformerException JavaDoc;
13 import javax.xml.transform.sax.SAXSource JavaDoc;
14 import javax.xml.transform.sax.SAXTransformerFactory JavaDoc;
15 import javax.xml.transform.sax.TemplatesHandler JavaDoc;
16 import org.xml.sax.InputSource JavaDoc;
17 import org.xml.sax.XMLReader JavaDoc;
18 import org.xml.sax.SAXException JavaDoc;
19
20 import org.exoplatform.services.xml.transform.impl.trax.TRAXTransformerImpl;
21 import org.exoplatform.services.xml.transform.NotSupportedIOTypeException;
22 import org.exoplatform.services.xml.transform.trax.TRAXTemplates;
23 import org.exoplatform.services.xml.transform.trax.TRAXTransformerService;
24 import org.exoplatform.services.xml.transform.trax.TRAXTransformer;
25 import org.exoplatform.services.xml.resolving.XMLResolvingService;
26
27 /**
28  * Created by The eXo Platform SARL .
29  *
30  * @author <a HREF="mailto:geaz@users.sourceforge.net">Gennady Azarenkov</a>
31  * @author <a HREF="mailto:alex.kravchuk@gmail.com">Alexander Kravchuk</a>
32  * @version $Id: TRAXTransformerServiceImpl.java 566 2005-01-25 12:50:49Z kravchuk $
33  */

34
35 public class TRAXTransformerServiceImpl implements TRAXTransformerService{
36
37     private XMLResolvingService resolvingService;
38
39     public TRAXTransformerServiceImpl(XMLResolvingService resolvingService){
40         this.resolvingService = resolvingService;
41     }
42
43     public TRAXTransformer getTransformer() throws
44             TransformerConfigurationException JavaDoc {
45         TRAXTransformerImpl transf = new TRAXTransformerImpl();
46         transf.resolvingService = resolvingService;
47         return transf;
48     }
49
50     public TRAXTransformer getTransformer(Source JavaDoc source) throws
51             TransformerConfigurationException JavaDoc {
52         TRAXTransformerImpl transf = new TRAXTransformerImpl(source);
53         transf.resolvingService = resolvingService;
54         return transf;
55     }
56
57     public TRAXTemplates getTemplates(Source JavaDoc source) throws
58             TransformerException JavaDoc,NotSupportedIOTypeException {
59         TRAXTemplatesImpl templates =
60             new TRAXTemplatesImpl(getXSLTemplates(source));
61         templates.resolvingService = resolvingService;
62         return templates;
63     }
64
65     private Templates JavaDoc getXSLTemplates(Source JavaDoc source) throws
66             TransformerException JavaDoc,NotSupportedIOTypeException {
67         SAXTransformerFactory JavaDoc saxTFactory =
68                 (SAXTransformerFactory JavaDoc) SAXTransformerFactory.
69                 newInstance();
70
71         TemplatesHandler JavaDoc templateHandler = saxTFactory.
72                                            newTemplatesHandler();
73         XMLReader JavaDoc xmlReader;
74         try {
75 // xmlReader = XMLReaderFactory.
76
// createXMLReader("org.apache.xerces.parsers.SAXParser");
77
xmlReader = TRAXTransformerImpl.getXMLReader();
78             //set default resolver
79
if (resolvingService != null) {
80                 xmlReader.setEntityResolver(
81                         resolvingService.getEntityResolver());
82             }
83
84         } catch (SAXException JavaDoc ex) {
85             throw new TransformerException JavaDoc(ex);
86         }
87
88         xmlReader.setContentHandler(templateHandler);
89         InputSource JavaDoc inputSource = SAXSource.sourceToInputSource(source);
90         if (inputSource == null) {
91             throw new NotSupportedIOTypeException(source);
92         }
93
94         try {
95             xmlReader.parse(inputSource);
96         } catch (SAXException JavaDoc ex) {
97             throw new TransformerException JavaDoc(ex);
98         } catch (IOException JavaDoc ex) {
99             throw new TransformerException JavaDoc(ex);
100         }
101
102         return templateHandler.getTemplates();
103
104     }
105
106
107 }
108
Popular Tags