KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > multiview > EncodingHelper


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.multiview;
21
22 import org.netbeans.modules.xml.api.EncodingUtil;
23
24 import javax.swing.text.StyledDocument JavaDoc;
25 import java.io.UnsupportedEncodingException JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.io.ByteArrayInputStream JavaDoc;
29
30 /**
31  * @author pfiala
32  */

33 public class EncodingHelper {
34     public static final String JavaDoc DEFAULT_ENCODING = "UTF-8"; // NOI18N;
35

36     private String JavaDoc encoding = DEFAULT_ENCODING;
37
38     public boolean isValidEncoding(String JavaDoc encoding) {
39         //test encoding on dummy stream
40
try {
41             new java.io.OutputStreamWriter JavaDoc(new java.io.ByteArrayOutputStream JavaDoc(1), encoding);
42             return true;
43         } catch (UnsupportedEncodingException JavaDoc e) {
44             return false;
45         }
46     }
47
48     public String JavaDoc setDefaultEncoding(String JavaDoc s) {
49         // update prolog to new valid encoding
50
if (s.startsWith("<?xml")) {
51             int i = s.indexOf("?>");
52             if (i > 0) {
53                 s = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" + s.substring(i + 2);
54             }
55         }
56         return s;
57     }
58
59     public String JavaDoc getEncoding() {
60         return encoding;
61     }
62
63     public void resetEncoding() {
64         encoding = DEFAULT_ENCODING;
65     }
66
67     public String JavaDoc detectEncoding(StyledDocument JavaDoc document) throws IOException JavaDoc {
68         return setEncoding(EncodingUtil.detectEncoding(document));
69     }
70
71     public String JavaDoc detectEncoding(InputStream JavaDoc inputStream) throws IOException JavaDoc {
72         return setEncoding(EncodingUtil.detectEncoding(inputStream));
73     }
74
75     public String JavaDoc detectEncoding(byte[] data) throws IOException JavaDoc {
76         return detectEncoding(new ByteArrayInputStream JavaDoc(data));
77     }
78
79     public String JavaDoc setEncoding(String JavaDoc encoding) {
80         if (encoding == null) {
81             return this.encoding;
82         }
83         if (isValidEncoding(encoding)) {
84             this.encoding = encoding;
85         }
86         return encoding;
87     }
88 }
89
Popular Tags