KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > util > DocumentCleaner


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 XQuark Group.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23 package org.xquark.util;
24
25 import java.io.*;
26 import java.io.ByteArrayOutputStream JavaDoc;
27 import java.io.OutputStream JavaDoc;
28 import java.io.UnsupportedEncodingException JavaDoc;
29
30 import org.xml.sax.*;
31 import org.xml.sax.ErrorHandler JavaDoc;
32 import org.xml.sax.SAXException JavaDoc;
33 import org.xml.sax.SAXParseException JavaDoc;
34 import org.xquark.schema.validation.SchemaValidationContext;
35 import org.xquark.schema.validation.ValidatingSchemaHandler;
36 import org.xquark.serialize.XMLSerializer;
37
38 /**
39  * This handler processes specific XML documents that under the root element
40  * have multiple elements. It dispatches these subelements into 2 streams
41  * according to the fact that these elements are valid or not. When not, the
42  * schema error message is written inline as a comment.
43  */

44 public class DocumentCleaner extends ValidatingSchemaHandler
45 {
46     // output streams
47
private OutputStream JavaDoc validOutput = null;
48     private OutputStream JavaDoc invalidOutput = null;
49     
50     private XMLSerializer serializer = null;
51     private ByteArrayOutputStream JavaDoc buffer = null;
52     
53     private byte depth = 0;
54     private SAXParseException JavaDoc lastElementException = null;
55
56     public DocumentCleaner(OutputStream JavaDoc validOutput, OutputStream JavaDoc invalidOutput,
57                            SchemaValidationContext context, String JavaDoc encoding) {
58         super(context);
59         this.validOutput = validOutput;
60         this.invalidOutput = invalidOutput;
61         buffer = new ByteArrayOutputStream JavaDoc();
62         try {
63             serializer = new XMLSerializer(buffer, encoding);
64         }
65         catch (UnsupportedEncodingException JavaDoc e) {
66         }
67         setErrorHandler(new MyErrorHandler());
68         setContentHandler(serializer);
69         setLexicalHandler(serializer);
70     }
71     
72     public DocumentCleaner(OutputStream JavaDoc validOutput, OutputStream JavaDoc invalidOutput) {
73         this(validOutput, invalidOutput, new SchemaValidationContext(), "UTF-8");
74     }
75     
76     public void endDocument() throws SAXException JavaDoc
77     {
78         super.endDocument();
79         // serialize remaining 1st-level element
80
flushBuffer2Both();
81         try {
82             validOutput.close();
83             invalidOutput.close();
84         }
85         catch (IOException e) {
86             throw new SAXException JavaDoc("Exception when closing streams", e);
87         }
88     }
89     
90     private void flushBuffer2Both() throws SAXException JavaDoc
91     {
92         serializer.flush();
93         try {
94             buffer.writeTo(validOutput);
95             buffer.writeTo(invalidOutput);
96         }
97         catch (IOException e) {
98             throw new SAXException JavaDoc("Exception when serializing XML prolog", e);
99         }
100         buffer.reset();
101     }
102
103     public void startElement(String JavaDoc str, String JavaDoc str1, String JavaDoc str2, Attributes attributes)
104     throws SAXException JavaDoc
105     {
106         depth++;
107         switch (depth)
108         {
109             case 1:
110                 super.startElement(str, str1, str2, attributes);
111                 serializer.completeStartTag();
112                 // serialize xml prolog and 1st-level element start
113
flushBuffer2Both();
114                 break;
115                 
116             case 2:
117                 lastElementException = null;
118                 super.startElement(str, str1, str2, attributes);
119                 break;
120                 
121             default :
122                 super.startElement(str, str1, str2, attributes);
123         }
124     }
125     
126     public void endElement(String JavaDoc str, String JavaDoc str1, String JavaDoc str2)
127     throws SAXException JavaDoc
128     {
129         super.endElement(str, str1, str2);
130         if (depth== 2){
131             try {
132                 if (lastElementException == null) {
133                     serializer.flush();
134                     buffer.writeTo(validOutput);
135                 }
136                 else {
137                     StringBuffer JavaDoc errorMsg = new StringBuffer JavaDoc();
138                     errorMsg.append("\nOriginal location: line ");
139                     errorMsg.append(lastElementException.getLineNumber());
140                     errorMsg.append(", column ");
141                     errorMsg.append(lastElementException.getColumnNumber());
142                     errorMsg.append('\n');
143                     errorMsg.append(lastElementException.getMessage());
144                     errorMsg.append('\n');
145                     char[] comment = new char[errorMsg.length()];
146                     errorMsg.getChars(0, comment.length, comment, 0);
147                     super.comment(comment, 0, comment.length);
148                     serializer.flush();
149                     buffer.writeTo(invalidOutput);
150                 }
151                 buffer.reset();
152             }
153             catch (IOException e) {
154                 throw new SAXException JavaDoc("Exception when serializing XML prolog", e);
155             }
156         }
157         depth--;
158     }
159
160     private class MyErrorHandler implements ErrorHandler JavaDoc {
161
162         public void error(SAXParseException JavaDoc arg0) throws SAXException JavaDoc {
163             lastElementException = arg0;
164         }
165
166         public void fatalError(SAXParseException JavaDoc arg0) throws SAXException JavaDoc {}
167         public void warning(SAXParseException JavaDoc arg0) throws SAXException JavaDoc {}
168         
169     }
170 }
171
Popular Tags