KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > transcoder > svg2svg > SVGTranscoder


1 /*
2
3    Copyright 1999-2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17 */

18
19 package org.apache.batik.transcoder.svg2svg;
20
21 import java.io.IOException JavaDoc;
22 import java.io.Reader JavaDoc;
23 import java.io.StringReader JavaDoc;
24 import java.io.StringWriter JavaDoc;
25 import java.io.Writer JavaDoc;
26
27 import org.apache.batik.dom.util.DOMUtilities;
28 import org.apache.batik.transcoder.AbstractTranscoder;
29 import org.apache.batik.transcoder.ErrorHandler;
30 import org.apache.batik.transcoder.TranscoderException;
31 import org.apache.batik.transcoder.TranscoderInput;
32 import org.apache.batik.transcoder.TranscoderOutput;
33 import org.apache.batik.transcoder.TranscodingHints;
34 import org.apache.batik.transcoder.keys.BooleanKey;
35 import org.apache.batik.transcoder.keys.IntegerKey;
36 import org.apache.batik.transcoder.keys.StringKey;
37 import org.w3c.dom.Document JavaDoc;
38
39 /**
40  * This class is a trancoder from SVG to SVG.
41  *
42  * @author <a HREF="mailto:stephane@hillion.org">Stephane Hillion</a>
43  * @version $Id: SVGTranscoder.java,v 1.8 2004/10/30 18:38:06 deweese Exp $
44  */

45 public class SVGTranscoder extends AbstractTranscoder {
46
47     /**
48      * The default error handler.
49      */

50     public final static ErrorHandler DEFAULT_ERROR_HANDLER = new ErrorHandler() {
51         public void error(TranscoderException ex) throws TranscoderException {
52             throw ex;
53         }
54         public void fatalError(TranscoderException ex) throws TranscoderException {
55             throw ex;
56         }
57         public void warning(TranscoderException ex) throws TranscoderException {
58             // Do nothing
59
}
60     };
61
62     /**
63      * The key to specify the newline character sequence.
64      */

65     public final static TranscodingHints.Key KEY_NEWLINE = new NewlineKey();
66
67     /**
68      * The "\r" newline value.
69      */

70     public final static NewlineValue VALUE_NEWLINE_CR = new NewlineValue("\r");
71
72     /**
73      * The "\r\n" newline value.
74      */

75     public final static NewlineValue VALUE_NEWLINE_CR_LF = new NewlineValue("\r\n");
76
77     /**
78      * The "\n" newline value.
79      */

80     public final static NewlineValue VALUE_NEWLINE_LF = new NewlineValue("\n");
81
82     /**
83      * The key to specify whether to format the input.
84      */

85     public final static TranscodingHints.Key KEY_FORMAT = new BooleanKey();
86
87     /**
88      * The value to turn on formatting.
89      */

90     public final static Boolean JavaDoc VALUE_FORMAT_ON = Boolean.TRUE;
91
92     /**
93      * The value to turn off formatting.
94      */

95     public final static Boolean JavaDoc VALUE_FORMAT_OFF = Boolean.FALSE;
96
97     /**
98      * The key to specify the tabulation width.
99      */

100     public final static TranscodingHints.Key KEY_TABULATION_WIDTH
101         = new IntegerKey();
102
103     /**
104      * The key to specify the document width.
105      */

106     public final static TranscodingHints.Key KEY_DOCUMENT_WIDTH
107         = new IntegerKey();
108
109     /**
110      * The key to specify the doctype option.
111      */

112     public final static TranscodingHints.Key KEY_DOCTYPE
113         = new DoctypeKey();
114
115     /**
116      * The doctype value to change the declaration.
117      */

118     public final static DoctypeValue VALUE_DOCTYPE_CHANGE =
119         new DoctypeValue(PrettyPrinter.DOCTYPE_CHANGE);
120
121     /**
122      * The doctype value to remove the declaration.
123      */

124     public final static DoctypeValue VALUE_DOCTYPE_REMOVE =
125         new DoctypeValue(PrettyPrinter.DOCTYPE_REMOVE);
126
127     /**
128      * The doctype value to keep unchanged the declaration.
129      */

130     public final static DoctypeValue VALUE_DOCTYPE_KEEP_UNCHANGED =
131         new DoctypeValue(PrettyPrinter.DOCTYPE_KEEP_UNCHANGED);
132
133     /**
134      * The key to specify the public id.
135      */

136     public final static TranscodingHints.Key KEY_PUBLIC_ID
137         = new StringKey();
138
139     /**
140      * The key to specify the system id.
141      */

142     public final static TranscodingHints.Key KEY_SYSTEM_ID
143         = new StringKey();
144
145     /**
146      * The key to specify the XML declaration option.
147      */

148     public final static TranscodingHints.Key KEY_XML_DECLARATION
149         = new StringKey();
150
151     /**
152      * Creates a new SVGTranscoder.
153      */

154     public SVGTranscoder() {
155         setErrorHandler(DEFAULT_ERROR_HANDLER);
156     }
157
158     /**
159      * Transcodes the specified input in the specified output.
160      * @param input the input to transcode
161      * @param output the ouput where to transcode
162      * @exception TranscoderException if an error occured while transcoding
163      */

164     public void transcode(TranscoderInput input, TranscoderOutput output)
165         throws TranscoderException {
166         Reader JavaDoc r = input.getReader();
167         Writer JavaDoc w = output.getWriter();
168
169         if (r == null) {
170             Document JavaDoc d = input.getDocument();
171             if (d == null) {
172                 throw new Error JavaDoc("Reader or Document expected");
173             }
174             StringWriter JavaDoc sw = new StringWriter JavaDoc();
175             try {
176                 DOMUtilities.writeDocument(d, sw);
177             } catch (IOException JavaDoc e) {
178                 throw new Error JavaDoc("IO");
179             }
180             r = new StringReader JavaDoc(sw.toString());
181         }
182         if (w == null) {
183             throw new Error JavaDoc("Writer expected");
184         }
185         prettyPrint(r, w);
186     }
187
188
189     /**
190      * Pretty print the given reader.
191      */

192     protected void prettyPrint(Reader JavaDoc in, Writer JavaDoc out) throws TranscoderException {
193         try {
194             PrettyPrinter pp = new PrettyPrinter();
195             NewlineValue nlv = (NewlineValue)hints.get(KEY_NEWLINE);
196             if (nlv != null) {
197                 pp.setNewline(nlv.getValue());
198             }
199             Boolean JavaDoc b = (Boolean JavaDoc)hints.get(KEY_FORMAT);
200             if (b != null) {
201                 pp.setFormat(b.booleanValue());
202             }
203             Integer JavaDoc i = (Integer JavaDoc)hints.get(KEY_TABULATION_WIDTH);
204             if (i != null) {
205                 pp.setTabulationWidth(i.intValue());
206             }
207             i = (Integer JavaDoc)hints.get(KEY_DOCUMENT_WIDTH);
208             if (i != null) {
209                 pp.setDocumentWidth(i.intValue());
210             }
211             DoctypeValue dtv = (DoctypeValue)hints.get(KEY_DOCTYPE);
212             if (dtv != null) {
213                 pp.setDoctypeOption(dtv.getValue());
214             }
215             String JavaDoc s = (String JavaDoc)hints.get(KEY_PUBLIC_ID);
216             if (s != null) {
217                 pp.setPublicId(s);
218             }
219             s = (String JavaDoc)hints.get(KEY_SYSTEM_ID);
220             if (s != null) {
221                 pp.setSystemId(s);
222             }
223
224             s = (String JavaDoc)hints.get(KEY_XML_DECLARATION);
225             if (s != null) {
226                 pp.setXMLDeclaration(s);
227             }
228
229             pp.print(in, out);
230             out.flush();
231         } catch (IOException JavaDoc e) {
232             getErrorHandler().fatalError(new TranscoderException(e.getMessage()));
233         }
234     }
235
236     /**
237      * To represent a newline key.
238      */

239     protected static class NewlineKey extends TranscodingHints.Key {
240         public boolean isCompatibleValue(Object JavaDoc v) {
241             return v instanceof NewlineValue;
242         }
243     }
244
245     /**
246      * To represent a newline value.
247      */

248     protected static class NewlineValue {
249         protected String JavaDoc value;
250         public NewlineValue(String JavaDoc val) {
251             value = val;
252         }
253         public String JavaDoc getValue() {
254             return value;
255         }
256     }
257
258     /**
259      * To represent a doctype key.
260      */

261     protected static class DoctypeKey extends TranscodingHints.Key {
262         public boolean isCompatibleValue(Object JavaDoc v) {
263             return v instanceof DoctypeValue;
264         }
265     }
266
267     /**
268      * To represent a doctype value.
269      */

270     protected static class DoctypeValue {
271         int value;
272         public DoctypeValue(int value) {
273             this.value = value;
274         }
275         public int getValue() {
276             return value;
277         }
278     }
279 }
280
Popular Tags