KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ui > DOMParserSaveEncoding


1 /*
2  * Copyright 1999-2002,2004,2005 The Apache Software Foundation.
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 ui;
17
18 import org.apache.xerces.parsers.DOMParser;
19 import org.apache.xerces.util.EncodingMap;
20 import org.apache.xerces.xni.Augmentations;
21 import org.apache.xerces.xni.XMLResourceIdentifier;
22 import org.apache.xerces.xni.XNIException;
23
24 /**
25  * The DOMParserSaveEncoding class extends DOMParser. It also provides
26  * the Java Encoding of the XML document by overriding the startDocument method
27  * and providing a way to capture the MIME encoding from the XML document which
28  * in turn is converted to the Java Encoding by the internal MIME2Java class.
29  *
30  */

31
32
33 public class DOMParserSaveEncoding extends DOMParser {
34     String JavaDoc _mimeEncoding = "UTF-8";//Default MIME so we check the file.encoding
35
private void setMimeEncoding( String JavaDoc encoding ) {
36         _mimeEncoding = encoding;
37     }
38     private String JavaDoc getMimeEncoding() {
39         return(_mimeEncoding);
40     }
41     public String JavaDoc getJavaEncoding() {
42         String JavaDoc javaEncoding = null;
43         String JavaDoc mimeEncoding = getMimeEncoding();
44
45         if (mimeEncoding != null) {
46             if (mimeEncoding.equals( "DEFAULT" ))
47                 javaEncoding = "UTF8";
48             else if (mimeEncoding.equalsIgnoreCase( "UTF-16" ))
49                 javaEncoding = "Unicode";
50             else
51                 javaEncoding = EncodingMap.getIANA2JavaMapping( mimeEncoding );
52         }
53         if(javaEncoding == null) // Should never return null
54
javaEncoding = "UTF8";
55         return(javaEncoding);
56     }
57     public void startGeneralEntity(String JavaDoc name,
58                             XMLResourceIdentifier identifier,
59                             String JavaDoc encoding, Augmentations augs) throws XNIException {
60         if( encoding != null){
61             setMimeEncoding( encoding);
62         }
63         super.startGeneralEntity(name, identifier, encoding, augs);
64     }
65
66 }
67
68
Popular Tags