KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.BufferedReader JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.io.Reader JavaDoc;
21 import java.io.StringReader JavaDoc;
22
23 /**
24  * This class strips the doctype from an XML stream
25  * because java.xml.transform can't disable validation
26  * and/or loading of the DTD in a standard way, which
27  * causes problems for those running without a network.
28  * <p/>
29  * Line terminators are converted to a \n
30  */

31 public class DocTypeReader extends Reader JavaDoc {
32
33   private Reader JavaDoc reader;
34   private String JavaDoc docType;
35
36   public DocTypeReader(Reader JavaDoc in) throws IOException JavaDoc {
37     BufferedReader JavaDoc lineReader = new BufferedReader JavaDoc(in);
38     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
39     StringBuffer JavaDoc docBuffer = new StringBuffer JavaDoc();
40     String JavaDoc line = null;
41     while ((line = lineReader.readLine()) != null) {
42       if (line.indexOf("<!DOCTYPE") > -1) {
43         docBuffer.append(line);
44         while (line.indexOf(">") < 0) {
45           line = lineReader.readLine();
46           docBuffer.append(" ");
47           docBuffer.append(line.trim());
48         }
49         line = lineReader.readLine();
50       }
51       buffer.append(line);
52       buffer.append("\n");
53     }
54     reader = new StringReader JavaDoc(buffer.toString());
55     docType = docBuffer.toString();
56   }
57
58   public String JavaDoc getDocType() {
59     return docType;
60   }
61
62   public int read(char cbuf[], int off, int len) throws IOException JavaDoc {
63     return reader.read(cbuf, off, len);
64   }
65
66   public void close() throws IOException JavaDoc {
67     reader.close();
68   }
69
70 }
71
Popular Tags