001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 2005-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.cnd.api.xml;
043:
044: import java.io.IOException;
045: import java.io.OutputStream;
046:
047: /**
048: * Drive the writing of an XML document.
049: * <p>
050: * While one can implement the {@link XMLEncoder} interface directly,
051: * the recommended practice
052: * is to define one or more specialized <code>XMLEncoder</code>s for the
053: * expected top-level elements and delegate to their {@link XMLEncoder#encode}.
054: */
055:
056: abstract public class XMLDocWriter implements XMLEncoder {
057:
058: private int indentChars = 2;
059: private XMLEncoderStream encoderStream;
060:
061: public XMLDocWriter() {
062: }
063:
064: /**
065: * Set number of spaces to be used for each indent level.
066: */
067:
068: public void setIndentChars(int indentChars) {
069: this .indentChars = indentChars;
070: }
071:
072: /**
073: * Return the XML encoding string.
074: * <p>
075: * The typical value is "UTF-8".
076: * <br>
077: * The default implementation handles US/Chinese/Japanese.
078: */
079:
080: protected String encoding() {
081: String lang = System.getenv("LANG"); // NOI18N
082: String encoding = "UTF-8"; // NOI18N
083: if (lang != null) {
084: if (lang.equals("zh") || // NOI18N
085: lang.equals("zh.GBK") || // NOI18N
086: lang.equals("zh_CN.EUC") || // NOI18N
087: lang.equals("zh_CN.GB18030") || // NOI18N
088: lang.equals("zh_CN") || // NOI18N
089: lang.equals("zh_CN.GBK")) { // NOI18N
090:
091: encoding = "EUC-JP"; // NOI18N
092:
093: } else if (lang.equals("ja") || // NOI18N
094: lang.equals("ja_JP.eucJP")) { // NOI18N
095:
096: encoding = "EUC-JP"; // NOI18N
097: } else {
098: encoding = "UTF-8"; // NOI18N
099: }
100: }
101: return encoding;
102: }
103:
104: /**
105: * Put out
106: * <?xml version="1.0" encoding="UTF-8"?>
107: * (Or the correct encoding)
108: */
109:
110: private void writeHeader() {
111: String version = "1.0"; // NOI18N
112: encoderStream.println("<?xml version=\"" + version
113: + "\" encoding=\"" + encoding() + "\"?>"); // NOI18N
114: }
115:
116: /**
117: * Put out
118: * <!DOCTYPE ... >
119: * LATER though ...
120: */
121:
122: private void writeDoctype() {
123: }
124:
125: private void writeTail() {
126: }
127:
128: /**
129: * Drive the writing of an XML document.
130: * <p>
131: * Will put the following to the stream:
132: * <pre>
133: <?xml version="1.0" encoding="<the-appropriate-encoding>"?>
134: call {@link #encode}
135: * </pre>
136: * <p>
137: * <i>the-appropriate-encoding</i> is whatever is returned by
138: * {@link #encoding}.
139: */
140: public void write(OutputStream os) throws IOException {
141: try {
142: encoderStream = new XMLEncoderStream(os, indentChars,
143: encoding());
144: writeHeader();
145: writeDoctype();
146: encode(encoderStream);
147: } finally {
148: writeTail();
149: encoderStream.close();
150: }
151: }
152: }
|