View Javadoc

1   /*
2    * Copyright 2005 Jukka Zitting <jz@yukatan.fi>
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package net.sf.exorcist.core;
17  
18  import java.io.File;
19  import java.io.FileInputStream;
20  import java.io.FileOutputStream;
21  import java.io.IOException;
22  import java.io.InputStream;
23  
24  import javax.xml.transform.Transformer;
25  import javax.xml.transform.TransformerException;
26  import javax.xml.transform.TransformerFactory;
27  import javax.xml.transform.stream.StreamResult;
28  import javax.xml.transform.stream.StreamSource;
29  
30  import net.sf.exorcist.api.ContentConverter;
31  import net.sf.exorcist.api.ContentException;
32  import net.sf.exorcist.api.ContentState;
33  
34  /***
35   * TODO
36   */
37  public class XsltConverter implements ContentConverter {
38  
39      private static final TransformerFactory FACTORY =
40          TransformerFactory.newInstance();
41  
42      private String stylesheet;
43  
44      public void setStylesheet(String stylesheet) {
45          this.stylesheet = stylesheet;
46      }
47  
48      public void convertContent(ContentState state) throws ContentException {
49          try {
50              File file = File.createTempFile("exorcist", ".xml");
51              file.deleteOnExit();
52  
53              InputStream input = state.getContent();
54              try {
55                  FileOutputStream output = new FileOutputStream(file);
56                  try {
57                      Transformer transformer = FACTORY.newTransformer(
58                              new StreamSource(new FileInputStream(stylesheet)));
59                      transformer.transform(
60                              new StreamSource(input), new StreamResult(output));
61                  } finally {
62                      try { output.close(); } catch (Exception e) {}
63                  }
64              } finally {
65                  try { input.close(); } catch (Exception e) {}
66              }
67  
68              InputStream xml = new FileInputStream(file);
69              try {
70                  state.setContent(xml);
71              } finally {
72                  try { xml.close(); } catch (Exception e) {}
73              }
74          } catch (IOException e) {
75              throw new ContentException(e);
76          } catch (TransformerException e) {
77              throw new ContentException(e);
78          }
79      }
80  
81  }