54 import com.sun.org.apache.xerces.internal.util.NamespaceSupport;
55 import com.sun.org.apache.xerces.internal.util.SymbolTable;
56 import com.sun.org.apache.xerces.internal.utils.SecuritySupport;
57 import com.sun.org.apache.xerces.internal.xni.QName;
58
59 import com.sun.xml.internal.stream.util.ReadOnlyIterator;
60
61 /**
62 * This class implements a StAX XMLStreamWriter. It extends
63 * <code>AbstractMap</code> in order to support a getter for
64 * implementation-specific properties. For example, you can get
65 * the underlying <code>OutputStream</code> by casting an instance
66 * of this class to <code>Map</code> and calling
67 * <code>getProperty(OUTPUTSTREAM_PROPERTY)</code>.
68 *
69 * @author Neeraj Bajaj
70 * @author K.Venugopal
71 * @author Santiago.Pericas-Geertsen@sun.com
72 * @author Sunitha.Reddy@sun.com
73 */
74 public final class XMLStreamWriterImpl extends AbstractMap implements XMLStreamWriter {
75
76 public static final String START_COMMENT = "<!--";
77 public static final String END_COMMENT = "-->";
78 public static final String DEFAULT_ENCODING = " encoding=\"utf-8\"";
79 public static final String DEFAULT_XMLDECL = "<?xml version=\"1.0\" ?>";
80 public static final String DEFAULT_XML_VERSION = "1.0";
81 public static final char CLOSE_START_TAG = '>';
82 public static final char OPEN_START_TAG = '<';
83 public static final String OPEN_END_TAG = "</";
84 public static final char CLOSE_END_TAG = '>';
85 public static final String START_CDATA = "<![CDATA[";
86 public static final String END_CDATA = "]]>";
87 public static final String CLOSE_EMPTY_ELEMENT = "/>";
88 public static final String SPACE = " ";
89 public static final String UTF_8 = "UTF-8";
90
91 public static final String OUTPUTSTREAM_PROPERTY = "sjsxp-outputstream";
92
93 /**
94 * This flag can be used to turn escaping off for content. It does
1095 try {
1096 if (fStartTagOpened) {
1097 closeStartTag();
1098 }
1099
1100 if ((target == null) || (data == null)) {
1101 throw new XMLStreamException("PI target cannot be null");
1102 }
1103
1104 fWriter.write("<?");
1105 fWriter.write(target);
1106 fWriter.write(SPACE);
1107 fWriter.write(data);
1108 fWriter.write("?>");
1109 } catch (IOException e) {
1110 throw new XMLStreamException(e);
1111 }
1112 }
1113
1114 /**
1115 * @throws XMLStreamException
1116 */
1117 public void writeStartDocument() throws XMLStreamException {
1118 try {
1119 fWriter.write(DEFAULT_XMLDECL);
1120 } catch (IOException ex) {
1121 throw new XMLStreamException(ex);
1122 }
1123 }
1124
1125 /**
1126 * @param version
1127 * @throws XMLStreamException
1128 */
1129 public void writeStartDocument(String version) throws XMLStreamException {
1130 try {
1131 if ((version == null) || version.equals("")) {
1132 writeStartDocument();
1133
1134 return;
1135 }
1136
1137 fWriter.write("<?xml version=\"");
1138 fWriter.write(version);
1139 fWriter.write("\"");
1140
1141 //fWriter.write(DEFAULT_ENCODING);
1142 fWriter.write("?>");
1143 } catch (IOException ex) {
1144 throw new XMLStreamException(ex);
1145 }
1146 }
1147
1148 /**
1149 * @param encoding
1150 * @param version
1151 * @throws XMLStreamException
1152 */
1153 public void writeStartDocument(String encoding, String version)
1154 throws XMLStreamException {
1155 //Revisit : What about standalone ?
1156 try {
1157 if ((encoding == null) && (version == null)) {
1158 writeStartDocument();
1159
1160 return;
1161 }
1162
1163 if (encoding == null) {
1164 writeStartDocument(version);
1165
1166 return;
1167 }
1168
1169 String streamEncoding = null;
1170 if (fWriter instanceof OutputStreamWriter) {
1171 streamEncoding = ((OutputStreamWriter) fWriter).getEncoding();
1172 }
1173 else if (fWriter instanceof UTF8OutputStreamWriter) {
1174 streamEncoding = ((UTF8OutputStreamWriter) fWriter).getEncoding();
1175 }
1176 else if (fWriter instanceof XMLWriter) {
1177 streamEncoding = ((OutputStreamWriter) ((XMLWriter)fWriter).getWriter()).getEncoding();
1178 }
1179
1180 if (streamEncoding != null && !streamEncoding.equalsIgnoreCase(encoding)) {
1181 // If the equality check failed, check for charset encoding aliases
1182 boolean foundAlias = false;
1183 Set aliases = Charset.forName(encoding).aliases();
1184 for (Iterator it = aliases.iterator(); !foundAlias && it.hasNext(); ) {
1185 if (streamEncoding.equalsIgnoreCase((String) it.next())) {
1186 foundAlias = true;
1187 }
1188 }
1189 // If no alias matches the encoding name, then report error
1190 if (!foundAlias) {
1191 throw new XMLStreamException("Underlying stream encoding '"
1192 + streamEncoding
1193 + "' and input paramter for writeStartDocument() method '"
1194 + encoding + "' do not match.");
1195 }
1196 }
1197
1198
1199 fWriter.write("<?xml version=\"");
1200
1201 if ((version == null) || version.equals("")) {
1202 fWriter.write(DEFAULT_XML_VERSION);
1203 } else {
1204 fWriter.write(version);
1205 }
1206
1207 if (!encoding.equals("")) {
1208 fWriter.write("\" encoding=\"");
1209 fWriter.write(encoding);
1210 }
1211
1212 fWriter.write("\"?>");
1213 } catch (IOException ex) {
1214 throw new XMLStreamException(ex);
1215 }
1216 }
1217
1218 /**
1219 * @param localName
1220 * @throws XMLStreamException
1221 */
1222 public void writeStartElement(String localName) throws XMLStreamException {
1223 try {
1224 if (localName == null) {
1225 throw new XMLStreamException("Local Name cannot be null");
1226 }
1227
1228 if (fStartTagOpened) {
1229 closeStartTag();
1230 }
1231
1232 openStartTag();
1233 fElementStack.push(null, localName, null, null, false);
1234 fInternalNamespaceContext.pushContext();
|
54 import com.sun.org.apache.xerces.internal.util.NamespaceSupport;
55 import com.sun.org.apache.xerces.internal.util.SymbolTable;
56 import com.sun.org.apache.xerces.internal.utils.SecuritySupport;
57 import com.sun.org.apache.xerces.internal.xni.QName;
58
59 import com.sun.xml.internal.stream.util.ReadOnlyIterator;
60
61 /**
62 * This class implements a StAX XMLStreamWriter. It extends
63 * <code>AbstractMap</code> in order to support a getter for
64 * implementation-specific properties. For example, you can get
65 * the underlying <code>OutputStream</code> by casting an instance
66 * of this class to <code>Map</code> and calling
67 * <code>getProperty(OUTPUTSTREAM_PROPERTY)</code>.
68 *
69 * @author Neeraj Bajaj
70 * @author K.Venugopal
71 * @author Santiago.Pericas-Geertsen@sun.com
72 * @author Sunitha.Reddy@sun.com
73 */
74 public final class XMLStreamWriterImpl extends AbstractMap implements XMLStreamWriterBase {
75
76 public static final String START_COMMENT = "<!--";
77 public static final String END_COMMENT = "-->";
78 public static final String DEFAULT_ENCODING = " encoding=\"utf-8\"";
79 public static final String DEFAULT_XMLDECL = "<?xml version=\"1.0\" ?>";
80 public static final String DEFAULT_XML_VERSION = "1.0";
81 public static final char CLOSE_START_TAG = '>';
82 public static final char OPEN_START_TAG = '<';
83 public static final String OPEN_END_TAG = "</";
84 public static final char CLOSE_END_TAG = '>';
85 public static final String START_CDATA = "<![CDATA[";
86 public static final String END_CDATA = "]]>";
87 public static final String CLOSE_EMPTY_ELEMENT = "/>";
88 public static final String SPACE = " ";
89 public static final String UTF_8 = "UTF-8";
90
91 public static final String OUTPUTSTREAM_PROPERTY = "sjsxp-outputstream";
92
93 /**
94 * This flag can be used to turn escaping off for content. It does
1095 try {
1096 if (fStartTagOpened) {
1097 closeStartTag();
1098 }
1099
1100 if ((target == null) || (data == null)) {
1101 throw new XMLStreamException("PI target cannot be null");
1102 }
1103
1104 fWriter.write("<?");
1105 fWriter.write(target);
1106 fWriter.write(SPACE);
1107 fWriter.write(data);
1108 fWriter.write("?>");
1109 } catch (IOException e) {
1110 throw new XMLStreamException(e);
1111 }
1112 }
1113
1114 /**
1115 * Writes the XML declaration.
1116 *
1117 * @throws XMLStreamException in case of an IOException
1118 */
1119 public void writeStartDocument() throws XMLStreamException {
1120 writeStartDocument(null, null, false, false);
1121 }
1122
1123 /**
1124 * Writes the XML declaration.
1125 *
1126 * @param version the specified version
1127 * @throws XMLStreamException in case of an IOException
1128 */
1129 public void writeStartDocument(String version) throws XMLStreamException {
1130 writeStartDocument(null, version, false, false);
1131 }
1132
1133 /**
1134 * Writes the XML declaration.
1135 *
1136 * @param encoding the specified encoding
1137 * @param version the specified version
1138 * @throws XMLStreamException in case of an IOException
1139 */
1140 @Override
1141 public void writeStartDocument(String encoding, String version)
1142 throws XMLStreamException {
1143 writeStartDocument(encoding, version, false, false);
1144 }
1145
1146 @Override
1147 public void writeStartDocument(String encoding, String version,
1148 boolean standalone, boolean standaloneSet)
1149 throws XMLStreamException {
1150
1151 try {
1152 if ((encoding == null || encoding.length() == 0)
1153 && (version == null || version.length() == 0)
1154 && (!standaloneSet)) {
1155 fWriter.write(DEFAULT_XMLDECL);
1156 return;
1157 }
1158
1159 // Verify the encoding before writing anything
1160 if (encoding != null && !encoding.equals("")) {
1161 verifyEncoding(encoding);
1162 }
1163
1164 fWriter.write("<?xml version=\"");
1165
1166 if ((version == null) || version.equals("")) {
1167 fWriter.write(DEFAULT_XML_VERSION);
1168 } else {
1169 fWriter.write(version);
1170 }
1171
1172 if (encoding != null && !encoding.equals("")) {
1173 fWriter.write("\" encoding=\"");
1174 fWriter.write(encoding);
1175 }
1176
1177 if (standaloneSet) {
1178 fWriter.write("\" standalone=\"");
1179 if (standalone) {
1180 fWriter.write("yes");
1181 } else {
1182 fWriter.write("no");
1183 }
1184 }
1185
1186 fWriter.write("\"?>");
1187 } catch (IOException ex) {
1188 throw new XMLStreamException(ex);
1189 }
1190 }
1191
1192 /**
1193 * Verifies that the encoding is consistent between the underlying encoding
1194 * and that specified.
1195 *
1196 * @param encoding the specified encoding
1197 * @throws XMLStreamException if they do not match
1198 */
1199 private void verifyEncoding(String encoding) throws XMLStreamException {
1200
1201 String streamEncoding = null;
1202 if (fWriter instanceof OutputStreamWriter) {
1203 streamEncoding = ((OutputStreamWriter) fWriter).getEncoding();
1204 }
1205 else if (fWriter instanceof UTF8OutputStreamWriter) {
1206 streamEncoding = ((UTF8OutputStreamWriter) fWriter).getEncoding();
1207 }
1208 else if (fWriter instanceof XMLWriter) {
1209 streamEncoding = ((OutputStreamWriter) ((XMLWriter)fWriter).getWriter()).getEncoding();
1210 }
1211
1212 if (streamEncoding != null && !streamEncoding.equalsIgnoreCase(encoding)) {
1213 // If the equality check failed, check for charset encoding aliases
1214 boolean foundAlias = false;
1215 Set aliases = Charset.forName(encoding).aliases();
1216 for (Iterator it = aliases.iterator(); !foundAlias && it.hasNext(); ) {
1217 if (streamEncoding.equalsIgnoreCase((String) it.next())) {
1218 foundAlias = true;
1219 }
1220 }
1221 // If no alias matches the encoding name, then report error
1222 if (!foundAlias) {
1223 throw new XMLStreamException("Underlying stream encoding '"
1224 + streamEncoding
1225 + "' and input paramter for writeStartDocument() method '"
1226 + encoding + "' do not match.");
1227 }
1228 }
1229 }
1230
1231 /**
1232 * @param localName
1233 * @throws XMLStreamException
1234 */
1235 public void writeStartElement(String localName) throws XMLStreamException {
1236 try {
1237 if (localName == null) {
1238 throw new XMLStreamException("Local Name cannot be null");
1239 }
1240
1241 if (fStartTagOpened) {
1242 closeStartTag();
1243 }
1244
1245 openStartTag();
1246 fElementStack.push(null, localName, null, null, false);
1247 fInternalNamespaceContext.pushContext();
|