KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibatis > db > sqlmap > upgrade > SqlMapXmlConverter


1 /*
2  * Copyright 2004 Clinton Begin
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 com.ibatis.db.sqlmap.upgrade;
17
18 import com.ibatis.common.exception.NestedRuntimeException;
19 import com.ibatis.common.resources.Resources;
20 import com.ibatis.sqlmap.client.SqlMapException;
21 import com.ibatis.sqlmap.engine.builder.xml.XmlConverter;
22
23 import javax.xml.transform.Transformer JavaDoc;
24 import javax.xml.transform.TransformerException JavaDoc;
25 import javax.xml.transform.TransformerFactory JavaDoc;
26 import javax.xml.transform.stream.StreamResult JavaDoc;
27 import javax.xml.transform.stream.StreamSource JavaDoc;
28 import java.io.*;
29
30 public class SqlMapXmlConverter implements XmlConverter {
31
32   public void convertXml(Reader reader, Writer writer) {
33     try {
34       DocTypeReader xml = new DocTypeReader(reader);
35       String JavaDoc docType = xml.getDocType();
36       Reader xsl = null;
37       if (docType == null) {
38         throw new SqlMapException("Could not convert document because DOCTYPE was null.");
39       } else {
40         if (docType.indexOf("sql-map-config") > -1) {
41           xsl = Resources.getResourceAsReader("com/ibatis/db/sqlmap/upgrade/SqlMapConfig.xsl");
42         } else if (docType.indexOf("sql-map") > -1) {
43           xsl = Resources.getResourceAsReader("com/ibatis/db/sqlmap/upgrade/SqlMap.xsl");
44         } else {
45           throw new SqlMapException("Could not convert document because DOCTYPE was not recognized: " + docType);
46         }
47       }
48       transformXml(xsl, xml, writer);
49     } catch (IOException e) {
50       throw new NestedRuntimeException("Error. Cause: " + e, e);
51     } catch (TransformerException JavaDoc e) {
52       throw new NestedRuntimeException("Error. Cause: " + e, e);
53     }
54   }
55
56   public Reader convertXml(Reader reader) {
57     StringWriter out = new StringWriter();
58     convertXml(reader, out);
59     return new StringReader(out.getBuffer().toString());
60   }
61
62   public void convertFile(String JavaDoc fromFileName, String JavaDoc toFileName) throws IOException {
63     File fromFile = new File(fromFileName);
64     File toFile = new File(toFileName);
65     convertFile(fromFile, toFile);
66   }
67
68   public void convertFile(File fromFile, File toFile) throws IOException {
69     Reader reader = new FileReader(fromFile);
70     Writer writer = new FileWriter(toFile);
71     convertXml(reader, writer);
72     writer.flush();
73     writer.close();
74     reader.close();
75   }
76
77   protected void transformXml(Reader xslReader, Reader xmlReader, Writer xmlWriter) throws TransformerException JavaDoc {
78     StreamSource JavaDoc xslSource = new StreamSource JavaDoc(xslReader);
79     StreamSource JavaDoc xmlSource = new StreamSource JavaDoc(xmlReader);
80     StreamResult JavaDoc xmlResult = new StreamResult JavaDoc(xmlWriter);
81
82     TransformerFactory JavaDoc transformerFactory = TransformerFactory.newInstance();
83     Transformer JavaDoc transformer = transformerFactory.newTransformer(xslSource);
84     transformer.setParameter("http://xml.org/sax/features/validation", new Boolean JavaDoc(false));
85     transformer.setParameter("http://apache.org/xml/features/nonvalidating/load-dtd", new Boolean JavaDoc(false));
86
87     transformer.transform(xmlSource, xmlResult);
88   }
89
90 }
91
92
Popular Tags