KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xml > internal > serialize > DOMSerializerImpl


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 1999-2004 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Xerces" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation and was
52  * originally based on software copyright (c) 2002, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57
58
59
60 package com.sun.org.apache.xml.internal.serialize;
61
62 import java.io.File JavaDoc;
63 import java.io.FileOutputStream JavaDoc;
64 import java.io.IOException JavaDoc;
65 import java.io.OutputStream JavaDoc;
66 import java.io.OutputStreamWriter JavaDoc;
67 import java.io.StringWriter JavaDoc;
68 import java.io.UnsupportedEncodingException JavaDoc;
69 import java.io.Writer JavaDoc;
70 import java.lang.reflect.Method JavaDoc;
71 import java.util.Enumeration JavaDoc;
72 import java.util.Hashtable JavaDoc;
73 import java.net.URI JavaDoc;
74
75 import java.net.HttpURLConnection JavaDoc;
76 import java.net.URL JavaDoc;
77 import java.net.URLConnection JavaDoc;
78 import java.util.Vector JavaDoc;
79
80 import com.sun.org.apache.xerces.internal.dom.AttrImpl;
81 import com.sun.org.apache.xerces.internal.dom.AttributeMap;
82 import com.sun.org.apache.xerces.internal.dom.CoreDocumentImpl;
83 import com.sun.org.apache.xerces.internal.dom.DOMErrorImpl;
84 import com.sun.org.apache.xerces.internal.dom.DOMLocatorImpl;
85 import com.sun.org.apache.xerces.internal.dom.DOMMessageFormatter;
86 import com.sun.org.apache.xerces.internal.dom.DOMNormalizer;
87 import com.sun.org.apache.xerces.internal.dom.DOMStringListImpl;
88 import com.sun.org.apache.xerces.internal.impl.Constants;
89 import com.sun.org.apache.xerces.internal.impl.XMLEntityManager;
90 import com.sun.org.apache.xerces.internal.util.NamespaceSupport;
91 import com.sun.org.apache.xerces.internal.util.SymbolTable;
92 import org.w3c.dom.DOMConfiguration JavaDoc;
93 import org.w3c.dom.DOMError JavaDoc;
94 import org.w3c.dom.DOMErrorHandler JavaDoc;
95 import com.sun.org.apache.xerces.internal.util.XML11Char;
96 import com.sun.org.apache.xerces.internal.util.XMLChar;
97 import org.w3c.dom.Attr JavaDoc;
98 import org.w3c.dom.Comment JavaDoc;
99 import org.w3c.dom.DOMException JavaDoc;
100 import org.w3c.dom.DOMStringList JavaDoc;
101 import org.w3c.dom.Document JavaDoc;
102 import org.w3c.dom.DocumentFragment JavaDoc;
103 import org.w3c.dom.Element JavaDoc;
104 import org.w3c.dom.Node JavaDoc;
105 import org.w3c.dom.ProcessingInstruction JavaDoc;
106 import org.w3c.dom.ls.LSException JavaDoc;
107 import org.w3c.dom.ls.LSOutput JavaDoc;
108 import org.w3c.dom.ls.LSSerializer JavaDoc;
109 import org.w3c.dom.ls.LSSerializerFilter JavaDoc;
110
111
112 /**
113  * EXPERIMENTAL: Implemenatation of DOM Level 3 org.w3c.ls.LSSerializer by delegating serialization
114  * calls to <CODE>XMLSerializer</CODE>.
115  * LSSerializer provides an API for serializing (writing) a DOM document out in an
116  * XML document. The XML data is written to an output stream.
117  * During serialization of XML data, namespace fixup is done when possible as
118  * defined in DOM Level 3 Core, Appendix B.
119  *
120  * @author Elena Litani, IBM
121  * @author Gopal Sharma, Sun Microsystems
122  * @author Arun Yadav, Sun Microsystems
123  * @version $Id: DOMSerializerImpl.java,v 1.21 2004/04/22 20:39:04 mrglavas Exp $
124  */

125 public class DOMSerializerImpl implements LSSerializer JavaDoc, DOMConfiguration JavaDoc {
126
127     // TODO: When DOM Level 3 goes to REC replace method calls using
128
// reflection for: getXmlEncoding, getInputEncoding and getXmlEncoding
129
// with regular static calls on the Document object.
130

131     // data
132
// serializer
133
private XMLSerializer serializer;
134
135     // XML 1.1 serializer
136
private XML11Serializer xml11Serializer;
137     
138     //Recognized parameters
139
private DOMStringList JavaDoc fRecognizedParameters;
140     
141     /** REVISIT: Currently we handle 3 different configurations, would be nice just have one configuration
142      * that has different recognized parameters depending if it is used in Core/LS.
143      */

144     protected short features = 0;
145
146     protected final static short NAMESPACES = 0x1<<0;
147     protected final static short WELLFORMED = 0x1<<1;
148     protected final static short ENTITIES = 0x1<<2;
149     protected final static short CDATA = 0x1<<3;
150     protected final static short SPLITCDATA = 0x1<<4;
151     protected final static short COMMENTS = 0x1<<5;
152     protected final static short DISCARDDEFAULT = 0x1<<6;
153     protected final static short INFOSET = 0x1<<7;
154     protected final static short XMLDECL = 0x1<<8;
155     protected final static short NSDECL = 0x1<<9;
156     protected final static short DOM_ELEMENT_CONTENT_WHITESPACE = 0x1<<10;
157     
158     // well-formness checking
159
private DOMErrorHandler JavaDoc fErrorHandler = null;
160     private final DOMErrorImpl fError = new DOMErrorImpl();
161     private final DOMLocatorImpl fLocator = new DOMLocatorImpl();
162     private static final RuntimeException JavaDoc abort = new RuntimeException JavaDoc();
163
164     /**
165      * Constructs a new LSSerializer.
166      * The constructor turns on the namespace support in <code>XMLSerializer</code> and
167      * initializes the following fields: fNSBinder, fLocalNSBinder, fSymbolTable,
168      * fEmptySymbol, fXmlSymbol, fXmlnsSymbol, fNamespaceCounter, fFeatures.
169      */

170     public DOMSerializerImpl() {
171         // set default features
172
features |= NAMESPACES;
173         features |= ENTITIES;
174         features |= COMMENTS;
175         features |= CDATA;
176         features |= SPLITCDATA;
177         features |= WELLFORMED;
178         features |= NSDECL;
179         features |= DOM_ELEMENT_CONTENT_WHITESPACE;
180         features |= DISCARDDEFAULT;
181         features |= XMLDECL;
182              
183         serializer = new XMLSerializer();
184         initSerializer(serializer);
185     }
186
187
188
189     //
190
// LSSerializer methods
191
//
192

193     public DOMConfiguration JavaDoc getDomConfig(){
194         return this;
195     }
196
197     /** DOM L3-EXPERIMENTAL:
198      * Setter for boolean and object parameters
199      */

200     public void setParameter(String JavaDoc name, Object JavaDoc value) throws DOMException JavaDoc {
201         if (value instanceof Boolean JavaDoc) {
202             boolean state = ((Boolean JavaDoc) value).booleanValue();
203             if (name.equalsIgnoreCase(Constants.DOM_INFOSET)){
204                 if (state){
205                     features &=~ENTITIES;
206                     features &=~CDATA;
207                     features |=NAMESPACES;
208                     features |=WELLFORMED;
209                     features |=COMMENTS;
210                 }
211                 // false does not have any effect
212
} else if (name.equalsIgnoreCase(Constants.DOM_XMLDECL)) {
213                 features =
214                     (short) (state ? features | XMLDECL : features & ~XMLDECL);
215             } else if (name.equalsIgnoreCase(Constants.DOM_NAMESPACES)) {
216                 features =
217                     (short) (state
218                         ? features | NAMESPACES
219                         : features & ~NAMESPACES);
220                 serializer.fNamespaces = state;
221             } else if (name.equalsIgnoreCase(Constants.DOM_SPLIT_CDATA)) {
222                 features =
223                     (short) (state
224                         ? features | SPLITCDATA
225                         : features & ~SPLITCDATA);
226             } else if (name.equalsIgnoreCase(Constants.DOM_DISCARD_DEFAULT_CONTENT)) {
227                 features =
228                     (short) (state
229                         ? features | DISCARDDEFAULT
230                         : features & ~DISCARDDEFAULT);
231             } else if (name.equalsIgnoreCase(Constants.DOM_WELLFORMED)) {
232                 features =
233                     (short) (state
234                         ? features | WELLFORMED
235                         : features & ~WELLFORMED);
236             } else if (name.equalsIgnoreCase(Constants.DOM_ENTITIES)){
237                 features =
238                     (short) (state
239                         ? features | ENTITIES
240                         : features & ~ENTITIES);
241             }
242             else if (name.equalsIgnoreCase(Constants.DOM_CDATA_SECTIONS)){
243                 features =
244                     (short) (state
245                         ? features | CDATA
246                         : features & ~CDATA);
247                         }
248             else if (name.equalsIgnoreCase(Constants.DOM_COMMENTS)){
249                 features =
250                      (short) (state
251                          ? features | COMMENTS
252                          : features & ~COMMENTS);
253             }
254                 else if (name.equalsIgnoreCase(Constants.DOM_CANONICAL_FORM)
255                     || name.equalsIgnoreCase(Constants.DOM_VALIDATE_IF_SCHEMA)
256                     || name.equalsIgnoreCase(Constants.DOM_VALIDATE)
257                     || name.equalsIgnoreCase(Constants.DOM_CHECK_CHAR_NORMALIZATION)
258                     || name.equalsIgnoreCase(Constants.DOM_DATATYPE_NORMALIZATION)
259                     || name.equalsIgnoreCase(Constants.DOM_FORMAT_PRETTY_PRINT)){
260                 // true is not supported
261
if (state) {
262                     String JavaDoc msg =
263                         DOMMessageFormatter.formatMessage(
264                             DOMMessageFormatter.DOM_DOMAIN,
265                             "FEATURE_NOT_SUPPORTED",
266                             new Object JavaDoc[] { name });
267                     throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR, msg);
268                 }
269             } else if (
270                 name.equalsIgnoreCase(Constants.DOM_NAMESPACE_DECLARATIONS)
271                     || name.equalsIgnoreCase(Constants.DOM_ELEMENT_CONTENT_WHITESPACE)
272                     || name.equalsIgnoreCase(Constants.DOM_IGNORE_UNKNOWN_CHARACTER_DENORMALIZATIONS)) {
273                 // false is not supported
274
if (!state) {
275                     String JavaDoc msg =
276                         DOMMessageFormatter.formatMessage(
277                             DOMMessageFormatter.DOM_DOMAIN,
278                             "FEATURE_NOT_SUPPORTED",
279                             new Object JavaDoc[] { name });
280                     throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR, msg);
281                 }
282             } else {
283                 String JavaDoc msg =
284                     DOMMessageFormatter.formatMessage(
285                         DOMMessageFormatter.DOM_DOMAIN,
286                         "FEATURE_NOT_FOUND",
287                         new Object JavaDoc[] { name });
288                 throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR, msg);
289             }
290         } else if (name.equalsIgnoreCase(Constants.DOM_ERROR_HANDLER)) {
291             if (value == null || value instanceof DOMErrorHandler JavaDoc) {
292                 fErrorHandler = (DOMErrorHandler JavaDoc)value;
293             } else {
294                 // REVISIT: modify error exception to TYPE_MISMATCH
295
String JavaDoc msg =
296                     DOMMessageFormatter.formatMessage(
297                         DOMMessageFormatter.DOM_DOMAIN,
298                         "TYPE_MISMATCH_ERR",
299                         new Object JavaDoc[] { name });
300                 throw new DOMException JavaDoc(DOMException.NOT_FOUND_ERR, msg);
301             }
302         } else if (
303             name.equalsIgnoreCase(Constants.DOM_RESOURCE_RESOLVER)
304                 || name.equalsIgnoreCase(Constants.DOM_SCHEMA_LOCATION)
305                 || name.equalsIgnoreCase(Constants.DOM_SCHEMA_TYPE)
306                 && value != null) {
307             String JavaDoc msg =
308                 DOMMessageFormatter.formatMessage(
309                     DOMMessageFormatter.DOM_DOMAIN,
310                     "FEATURE_NOT_SUPPORTED",
311                     new Object JavaDoc[] { name });
312             throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR, msg);
313         } else {
314             String JavaDoc msg =
315                 DOMMessageFormatter.formatMessage(
316                     DOMMessageFormatter.DOM_DOMAIN,
317                     "FEATURE_NOT_FOUND",
318                     new Object JavaDoc[] { name });
319             throw new DOMException JavaDoc(DOMException.NOT_FOUND_ERR, msg);
320         }
321     }
322
323     /** DOM L3-EXPERIMENTAL:
324      * Check if parameter can be set
325      */

326     public boolean canSetParameter(String JavaDoc name, Object JavaDoc state) {
327         if (state instanceof Boolean JavaDoc){
328             boolean value = ((Boolean JavaDoc)state).booleanValue();
329             if (name.equalsIgnoreCase(Constants.DOM_NAMESPACES)
330             || name.equalsIgnoreCase(Constants.DOM_SPLIT_CDATA)
331             || name.equalsIgnoreCase(Constants.DOM_DISCARD_DEFAULT_CONTENT)
332             || name.equalsIgnoreCase(Constants.DOM_XMLDECL)
333             || name.equalsIgnoreCase(Constants.DOM_WELLFORMED)
334             || name.equalsIgnoreCase(Constants.DOM_INFOSET)
335             || name.equalsIgnoreCase(Constants.DOM_ENTITIES)
336             || name.equalsIgnoreCase(Constants.DOM_CDATA_SECTIONS)
337             || name.equalsIgnoreCase(Constants.DOM_COMMENTS)){
338                 // both values supported
339
return true;
340             }
341             else if (name.equalsIgnoreCase(Constants.DOM_CANONICAL_FORM)
342                 || name.equalsIgnoreCase(Constants.DOM_VALIDATE_IF_SCHEMA)
343                 || name.equalsIgnoreCase(Constants.DOM_VALIDATE)
344                 || name.equalsIgnoreCase(Constants.DOM_CHECK_CHAR_NORMALIZATION)
345                 || name.equalsIgnoreCase(Constants.DOM_DATATYPE_NORMALIZATION)
346                 || name.equalsIgnoreCase(Constants.DOM_FORMAT_PRETTY_PRINT)){
347                 // true is not supported
348
return !value;
349             }
350             else if(name.equalsIgnoreCase(Constants.DOM_NORMALIZE_CHARACTERS)){
351                 return false;
352             }
353             else if (name.equalsIgnoreCase(Constants.DOM_NAMESPACE_DECLARATIONS)
354                     || name.equalsIgnoreCase(Constants.DOM_ELEMENT_CONTENT_WHITESPACE)
355                     || name.equalsIgnoreCase(Constants.DOM_IGNORE_UNKNOWN_CHARACTER_DENORMALIZATIONS)) {
356                 // false is not supported
357
return value;
358                     }
359         }
360         else if (name.equalsIgnoreCase(Constants.DOM_ERROR_HANDLER) &&
361                 state == null || state instanceof DOMErrorHandler JavaDoc){
362             return true;
363         }
364         return false;
365     }
366
367     /**
368      * DOM Level 3 Core CR - Experimental.
369      *
370      * The list of the parameters supported by this
371      * <code>DOMConfiguration</code> object and for which at least one value
372      * can be set by the application. Note that this list can also contain
373      * parameter names defined outside this specification.
374      */

375     public DOMStringList JavaDoc getParameterNames() {
376         
377         if (fRecognizedParameters == null){
378             Vector JavaDoc parameters = new Vector JavaDoc();
379
380             //Add DOM recognized parameters
381
//REVISIT: Would have been nice to have a list of
382
//recognized parameters.
383
parameters.add(Constants.DOM_NAMESPACES);
384             parameters.add(Constants.DOM_SPLIT_CDATA);
385             parameters.add(Constants.DOM_DISCARD_DEFAULT_CONTENT);
386             parameters.add(Constants.DOM_XMLDECL);
387             parameters.add(Constants.DOM_CANONICAL_FORM);
388             parameters.add(Constants.DOM_VALIDATE_IF_SCHEMA);
389             parameters.add(Constants.DOM_VALIDATE);
390             parameters.add(Constants.DOM_CHECK_CHAR_NORMALIZATION);
391             parameters.add(Constants.DOM_DATATYPE_NORMALIZATION);
392             parameters.add(Constants.DOM_FORMAT_PRETTY_PRINT);
393             //parameters.add(Constants.DOM_NORMALIZE_CHARACTERS);
394
parameters.add(Constants.DOM_WELLFORMED);
395             parameters.add(Constants.DOM_INFOSET);
396             parameters.add(Constants.DOM_NAMESPACE_DECLARATIONS);
397             parameters.add(Constants.DOM_ELEMENT_CONTENT_WHITESPACE);
398             parameters.add(Constants.DOM_ENTITIES);
399             parameters.add(Constants.DOM_CDATA_SECTIONS);
400             parameters.add(Constants.DOM_COMMENTS);
401             parameters.add(Constants.DOM_IGNORE_UNKNOWN_CHARACTER_DENORMALIZATIONS);
402             parameters.add(Constants.DOM_ERROR_HANDLER);
403             //parameters.add(Constants.DOM_SCHEMA_LOCATION);
404
//parameters.add(Constants.DOM_SCHEMA_TYPE);
405

406             //Add recognized xerces features and properties
407

408             fRecognizedParameters = new DOMStringListImpl(parameters);
409             
410         }
411
412         return fRecognizedParameters;
413     }
414     
415     /** DOM L3-EXPERIMENTAL:
416      * Getter for boolean and object parameters
417      */

418     public Object JavaDoc getParameter(String JavaDoc name) throws DOMException JavaDoc {
419         if (name.equalsIgnoreCase(Constants.DOM_COMMENTS)) {
420             return ((features & COMMENTS) != 0) ? Boolean.TRUE : Boolean.FALSE;
421         } else if(name.equalsIgnoreCase(Constants.DOM_NORMALIZE_CHARACTERS)){
422             return null;
423         } else if (name.equalsIgnoreCase(Constants.DOM_NAMESPACES)) {
424             return (features & NAMESPACES) != 0 ? Boolean.TRUE : Boolean.FALSE;
425         } else if (name.equalsIgnoreCase(Constants.DOM_XMLDECL)) {
426             return (features & XMLDECL) != 0 ? Boolean.TRUE : Boolean.FALSE;
427         } else if (name.equalsIgnoreCase(Constants.DOM_CDATA_SECTIONS)) {
428             return (features & CDATA) != 0 ? Boolean.TRUE : Boolean.FALSE;
429         } else if (name.equalsIgnoreCase(Constants.DOM_ENTITIES)) {
430             return (features & ENTITIES) != 0 ? Boolean.TRUE : Boolean.FALSE;
431         } else if (name.equalsIgnoreCase(Constants.DOM_SPLIT_CDATA)) {
432             return (features & SPLITCDATA) != 0 ? Boolean.TRUE : Boolean.FALSE;
433         } else if (name.equalsIgnoreCase(Constants.DOM_WELLFORMED)) {
434             return (features & WELLFORMED) != 0 ? Boolean.TRUE : Boolean.FALSE;
435         } else if (name.equalsIgnoreCase(Constants.DOM_ELEMENT_CONTENT_WHITESPACE) ||
436                    name.equalsIgnoreCase(Constants.DOM_IGNORE_UNKNOWN_CHARACTER_DENORMALIZATIONS)
437                     || name.equalsIgnoreCase(Constants.DOM_NAMESPACE_DECLARATIONS)) {
438             return Boolean.TRUE;
439         }else if (name.equalsIgnoreCase(Constants.DOM_DISCARD_DEFAULT_CONTENT)){
440             return ((features & DISCARDDEFAULT)!=0)?Boolean.TRUE:Boolean.FALSE;
441         }else if (name.equalsIgnoreCase(Constants.DOM_INFOSET)){
442             if ((features & ENTITIES) == 0 &&
443                  (features & CDATA) ==0 &&
444                  (features & NAMESPACES) !=0 &&
445                  (features & WELLFORMED) !=0 &&
446                  (features & COMMENTS) !=0){
447                      return Boolean.TRUE;
448                  }
449                  return Boolean.FALSE;
450         } else if (name.equalsIgnoreCase (Constants.DOM_FORMAT_PRETTY_PRINT)
451                 || name.equalsIgnoreCase(Constants.DOM_CANONICAL_FORM)
452                 || name.equalsIgnoreCase(Constants.DOM_VALIDATE_IF_SCHEMA)
453                 || name.equalsIgnoreCase(Constants.DOM_CHECK_CHAR_NORMALIZATION)
454                 || name.equalsIgnoreCase(Constants.DOM_VALIDATE)
455                 || name.equalsIgnoreCase(Constants.DOM_VALIDATE_IF_SCHEMA)
456                 || name.equalsIgnoreCase(Constants.DOM_DATATYPE_NORMALIZATION)) {
457             return Boolean.FALSE;
458         } else if (name.equalsIgnoreCase(Constants.DOM_ERROR_HANDLER)) {
459             return fErrorHandler;
460         } else if (
461             name.equalsIgnoreCase(Constants.DOM_RESOURCE_RESOLVER)
462                 || name.equalsIgnoreCase(Constants.DOM_SCHEMA_LOCATION)
463                 || name.equalsIgnoreCase(Constants.DOM_SCHEMA_TYPE)) {
464             String JavaDoc msg =
465                 DOMMessageFormatter.formatMessage(
466                     DOMMessageFormatter.DOM_DOMAIN,
467                     "FEATURE_NOT_SUPPORTED",
468                     new Object JavaDoc[] { name });
469             throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR, msg);
470         } else {
471             String JavaDoc msg =
472                 DOMMessageFormatter.formatMessage(
473                     DOMMessageFormatter.DOM_DOMAIN,
474                     "FEATURE_NOT_FOUND",
475                     new Object JavaDoc[] { name });
476             throw new DOMException JavaDoc(DOMException.NOT_FOUND_ERR, msg);
477         }
478     }
479
480
481     /**
482      * DOM L3 EXPERIMENTAL:
483      * Serialize the specified node as described above in the description of
484      * <code>LSSerializer</code>. The result of serializing the node is
485      * returned as a string. Writing a Document or Entity node produces a
486      * serialized form that is well formed XML. Writing other node types
487      * produces a fragment of text in a form that is not fully defined by
488      * this document, but that should be useful to a human for debugging or
489      * diagnostic purposes.
490      * @param wnode The node to be written.
491      * @return Returns the serialized data, or <code>null</code> in case a
492      * failure occured and the failure wasn't canceled by the error
493      * handler.
494      * @exception DOMException
495      * DOMSTRING_SIZE_ERR: The resulting string is too long to fit in a
496      * <code>DOMString</code>.
497      */

498     public String JavaDoc writeToString(Node JavaDoc wnode) throws DOMException JavaDoc, LSException JavaDoc {
499         // determine which serializer to use:
500
Document JavaDoc doc = (wnode.getNodeType() == Node.DOCUMENT_NODE)?(Document JavaDoc)wnode:wnode.getOwnerDocument();
501         Method getVersion = null;
502         XMLSerializer ser = null;
503         String JavaDoc ver = null;
504         // this should run under JDK 1.1.8...
505
try {
506             getVersion = doc.getClass().getMethod("getXmlVersion", new Class JavaDoc[]{});
507             if(getVersion != null ) {
508                 ver = (String JavaDoc)getVersion.invoke(doc, null);
509             }
510         } catch (Exception JavaDoc e) {
511             // no way to test the version...
512
// ignore the exception
513
}
514         if(ver != null && ver.equals("1.1")) {
515             if(xml11Serializer == null) {
516                 xml11Serializer = new XML11Serializer();
517                 initSerializer(xml11Serializer);
518             }
519             // copy setting from "main" serializer to XML 1.1 serializer
520
copySettings(serializer, xml11Serializer);
521             ser = xml11Serializer;
522         } else {
523             ser = serializer;
524         }
525
526         StringWriter JavaDoc destination = new StringWriter JavaDoc();
527         try {
528             prepareForSerialization(ser, wnode);
529             ser._format.setEncoding("UTF-16");
530             ser.setOutputCharStream(destination);
531             if (wnode == null)
532                 return null;
533             else if (wnode.getNodeType() == Node.DOCUMENT_NODE)
534                 ser.serialize((Document JavaDoc)wnode);
535             else if (wnode.getNodeType() == Node.DOCUMENT_FRAGMENT_NODE)
536                 ser.serialize((DocumentFragment JavaDoc)wnode);
537             else if (wnode.getNodeType() == Node.ELEMENT_NODE)
538                 ser.serialize((Element JavaDoc)wnode);
539             else
540                 return null;
541         } catch (RuntimeException JavaDoc e) {
542             if (e == DOMNormalizer.abort){
543                 // stopped at user request
544
return null;
545             }
546             throw new LSException JavaDoc(LSException.SERIALIZE_ERR, e.toString());
547         } catch (IOException JavaDoc ioe) {
548             String JavaDoc msg = DOMMessageFormatter.formatMessage(
549                 DOMMessageFormatter.DOM_DOMAIN,
550                 "STRING_TOO_LONG",
551                 new Object JavaDoc[] { ioe.getMessage()});
552             throw new DOMException JavaDoc(DOMException.DOMSTRING_SIZE_ERR,msg);
553         }
554         return destination.toString();
555     }
556
557     /**
558      * DOM L3 EXPERIMENTAL:
559      * The end-of-line sequence of characters to be used in the XML being
560      * written out. The only permitted values are these:
561      * <dl>
562      * <dt><code>null</code></dt>
563      * <dd>
564      * Use a default end-of-line sequence. DOM implementations should choose
565      * the default to match the usual convention for text files in the
566      * environment being used. Implementations must choose a default
567      * sequence that matches one of those allowed by 2.11 "End-of-Line
568      * Handling". </dd>
569      * <dt>CR</dt>
570      * <dd>The carriage-return character (#xD).</dd>
571      * <dt>CR-LF</dt>
572      * <dd> The
573      * carriage-return and line-feed characters (#xD #xA). </dd>
574      * <dt>LF</dt>
575      * <dd> The line-feed
576      * character (#xA). </dd>
577      * </dl>
578      * <br>The default value for this attribute is <code>null</code>.
579      */

580     public void setNewLine(String JavaDoc newLine) {
581         serializer._format.setLineSeparator(newLine);
582     }
583
584
585     /**
586      * DOM L3 EXPERIMENTAL:
587      * The end-of-line sequence of characters to be used in the XML being
588      * written out. The only permitted values are these:
589      * <dl>
590      * <dt><code>null</code></dt>
591      * <dd>
592      * Use a default end-of-line sequence. DOM implementations should choose
593      * the default to match the usual convention for text files in the
594      * environment being used. Implementations must choose a default
595      * sequence that matches one of those allowed by 2.11 "End-of-Line
596      * Handling". </dd>
597      * <dt>CR</dt>
598      * <dd>The carriage-return character (#xD).</dd>
599      * <dt>CR-LF</dt>
600      * <dd> The
601      * carriage-return and line-feed characters (#xD #xA). </dd>
602      * <dt>LF</dt>
603      * <dd> The line-feed
604      * character (#xA). </dd>
605      * </dl>
606      * <br>The default value for this attribute is <code>null</code>.
607      */

608     public String JavaDoc getNewLine() {
609         return serializer._format.getLineSeparator();
610     }
611
612
613     /**
614      * When the application provides a filter, the serializer will call out
615      * to the filter before serializing each Node. Attribute nodes are never
616      * passed to the filter. The filter implementation can choose to remove
617      * the node from the stream or to terminate the serialization early.
618      */

619     public LSSerializerFilter JavaDoc getFilter(){
620         return serializer.fDOMFilter;
621     }
622     /**
623      * When the application provides a filter, the serializer will call out
624      * to the filter before serializing each Node. Attribute nodes are never
625      * passed to the filter. The filter implementation can choose to remove
626      * the node from the stream or to terminate the serialization early.
627      */

628     public void setFilter(LSSerializerFilter JavaDoc filter){
629         serializer.fDOMFilter = filter;
630     }
631
632     // this initializes a newly-created serializer
633
private void initSerializer(XMLSerializer ser) {
634         ser.fNSBinder = new NamespaceSupport();
635         ser.fLocalNSBinder = new NamespaceSupport();
636         ser.fSymbolTable = new SymbolTable();
637     }
638
639     // copies all settings that could have been modified
640
// by calls to LSSerializer methods from one serializer to another.
641
// IMPORTANT: if new methods are implemented or more settings of
642
// the serializer are made alterable, this must be
643
// reflected in this method!
644
private void copySettings(XMLSerializer src, XMLSerializer dest) {
645         dest.fDOMErrorHandler = fErrorHandler;
646         dest._format.setEncoding(src._format.getEncoding());
647         dest._format.setLineSeparator(src._format.getLineSeparator());
648         dest.fDOMFilter = src.fDOMFilter;
649     }//copysettings
650

651     /**
652       * Serialize the specified node as described above in the general
653       * description of the <code>LSSerializer</code> interface. The output
654       * is written to the supplied <code>LSOutput</code>.
655       * <br> When writing to a <code>LSOutput</code>, the encoding is found by
656       * looking at the encoding information that is reachable through the
657       * <code>LSOutput</code> and the item to be written (or its owner
658       * document) in this order:
659       * <ol>
660       * <li> <code>LSOutput.encoding</code>,
661       * </li>
662       * <li>
663       * <code>Document.actualEncoding</code>,
664       * </li>
665       * <li>
666       * <code>Document.xmlEncoding</code>.
667       * </li>
668       * </ol>
669       * <br> If no encoding is reachable through the above properties, a
670       * default encoding of "UTF-8" will be used.
671       * <br> If the specified encoding is not supported an
672       * "unsupported-encoding" error is raised.
673       * <br> If no output is specified in the <code>LSOutput</code>, a
674       * "no-output-specified" error is raised.
675       * @param node The node to serialize.
676       * @param destination The destination for the serialized DOM.
677       * @return Returns <code>true</code> if <code>node</code> was
678       * successfully serialized and <code>false</code> in case the node
679       * couldn't be serialized.
680       */

681     public boolean write(Node JavaDoc node, LSOutput JavaDoc destination) throws LSException JavaDoc{
682
683         if (node == null)
684             return false;
685             
686         Method getVersion = null;
687         XMLSerializer ser = null;
688         String JavaDoc ver = null;
689         Document JavaDoc fDocument =(node.getNodeType() == Node.DOCUMENT_NODE)
690                 ? (Document JavaDoc) node
691                 : node.getOwnerDocument();
692         // this should run under JDK 1.1.8...
693
try {
694             getVersion = fDocument.getClass().getMethod("getXmlVersion", new Class JavaDoc[] {});
695             if (getVersion != null) {
696                 ver = (String JavaDoc) getVersion.invoke(fDocument, null);
697             }
698         } catch (Exception JavaDoc e) {
699             //no way to test the version...
700
//ignore the exception
701
}
702         //determine which serializer to use:
703
if (ver != null && ver.equals("1.1")) {
704             if (xml11Serializer == null) {
705                 xml11Serializer = new XML11Serializer();
706                 initSerializer(xml11Serializer);
707             }
708             //copy setting from "main" serializer to XML 1.1 serializer
709
copySettings(serializer, xml11Serializer);
710             ser = xml11Serializer;
711         } else {
712             ser = serializer;
713         }
714
715         String JavaDoc encoding = null;
716         if ((encoding = destination.getEncoding()) == null) {
717             try {
718                 Method getEncoding =
719                     fDocument.getClass().getMethod("getInputEncoding", new Class JavaDoc[] {});
720                 if (getEncoding != null) {
721                     encoding = (String JavaDoc) getEncoding.invoke(fDocument, null);
722                 }
723             } catch (Exception JavaDoc e) {
724                 // ignore the exception
725
}
726             if (encoding == null) {
727                 try {
728                     Method getEncoding =
729                         fDocument.getClass().getMethod("getXmlEncoding", new Class JavaDoc[] {});
730                     if (getEncoding != null) {
731                         encoding = (String JavaDoc) getEncoding.invoke(fDocument, null);
732                     }
733                 } catch (Exception JavaDoc e) {
734                     // ignore the exception
735
}
736                 if (encoding == null) {
737                     encoding = "UTF-8";
738                 }
739             }
740         }
741         try {
742             prepareForSerialization(ser, node);
743             ser._format.setEncoding(encoding);
744             OutputStream JavaDoc outputStream = destination.getByteStream();
745             Writer JavaDoc writer = destination.getCharacterStream();
746             String JavaDoc uri = destination.getSystemId();
747             if (writer == null) {
748                 if (outputStream == null) {
749                     if (uri == null) {
750                         if (ser.fDOMErrorHandler != null) {
751                             DOMErrorImpl error = new DOMErrorImpl();
752                             error.fType = "no-output-specified";
753                             error.fMessage = "no-output-specified";
754                             error.fSeverity = DOMError.SEVERITY_FATAL_ERROR;
755                             ser.fDOMErrorHandler.handleError(error);
756                         }
757                         throw new LSException JavaDoc(LSException.SERIALIZE_ERR,
758                             DOMMessageFormatter.formatMessage(
759                                 DOMMessageFormatter.SERIALIZER_DOMAIN,
760                                 "no-output-specified", null));
761                     }
762                     else {
763                         // URI was specified. Handle relative URIs.
764
String JavaDoc expanded = XMLEntityManager.expandSystemId(uri, null, true);
765                         URL JavaDoc url = new URL JavaDoc(expanded != null ? expanded : uri);
766                         OutputStream JavaDoc out = null;
767                         String JavaDoc protocol = url.getProtocol();
768                         String JavaDoc host = url.getHost();
769                         // Use FileOutputStream if this URI is for a local file.
770
if (protocol.equals("file")
771                             && (host == null || host.length() == 0 || host.equals("localhost"))) {
772                             // REVISIT: We have to decode %nn sequences. For
773
// now files containing spaces and other characters
774
// which were escaped in the URI will fail. -- mrglavas
775
out = new FileOutputStream JavaDoc(new File JavaDoc(url.getPath()));
776                         }
777                         // Try to write to some other kind of URI. Some protocols
778
// won't support this, though HTTP should work.
779
else {
780                             URLConnection JavaDoc urlCon = url.openConnection();
781                             urlCon.setDoInput(false);
782                             urlCon.setDoOutput(true);
783                             urlCon.setUseCaches(false); // Enable tunneling.
784
if (urlCon instanceof HttpURLConnection JavaDoc) {
785                                 // The DOM L3 LS CR says if we are writing to an HTTP URI
786
// it is to be done with an HTTP PUT.
787
HttpURLConnection JavaDoc httpCon = (HttpURLConnection JavaDoc) urlCon;
788                                 httpCon.setRequestMethod("PUT");
789                             }
790                             out = urlCon.getOutputStream();
791                         }
792                         ser.setOutputByteStream(out);
793                     }
794                 }
795                 else {
796                     // byte stream was specified
797
ser.setOutputByteStream(outputStream);
798                 }
799             }
800             else {
801                 // character stream is specified
802
ser.setOutputCharStream(writer);
803             }
804
805             if (node.getNodeType() == Node.DOCUMENT_NODE)
806                 ser.serialize((Document JavaDoc) node);
807             else if (node.getNodeType() == Node.DOCUMENT_FRAGMENT_NODE)
808                 ser.serialize((DocumentFragment JavaDoc) node);
809             else if (node.getNodeType() == Node.ELEMENT_NODE)
810                 ser.serialize((Element JavaDoc) node);
811             else
812                 return false;
813         } catch( UnsupportedEncodingException JavaDoc ue) {
814             if (ser.fDOMErrorHandler != null) {
815                 DOMErrorImpl error = new DOMErrorImpl();
816                 error.fException = ue;
817                 error.fType = "unsupported-encoding";
818                 error.fMessage = ue.getMessage();
819                 error.fSeverity = DOMError.SEVERITY_FATAL_ERROR;
820                 ser.fDOMErrorHandler.handleError(error);
821             }
822             throw new LSException JavaDoc(LSException.SERIALIZE_ERR,
823                 DOMMessageFormatter.formatMessage(
824                     DOMMessageFormatter.SERIALIZER_DOMAIN,
825                     "unsupported-encoding", null));
826             //return false;
827
} catch (RuntimeException JavaDoc e) {
828             if (e == DOMNormalizer.abort){
829                 // stopped at user request
830
return false;
831             }
832             throw new LSException JavaDoc(LSException.SERIALIZE_ERR, e.toString());
833         } catch (Exception JavaDoc e) {
834             if (ser.fDOMErrorHandler != null) {
835                 DOMErrorImpl error = new DOMErrorImpl();
836                 error.fException = e;
837                 error.fMessage = e.getMessage();
838                 error.fSeverity = DOMError.SEVERITY_ERROR;
839                 ser.fDOMErrorHandler.handleError(error);
840
841             }
842             e.printStackTrace();
843             throw new LSException JavaDoc(LSException.SERIALIZE_ERR, e.toString());
844         }
845         return true;
846
847     } //write
848

849     /**
850       * Serialize the specified node as described above in the general
851       * description of the <code>LSSerializer</code> interface. The output
852       * is written to the supplied URI.
853       * <br> When writing to a URI, the encoding is found by looking at the
854       * encoding information that is reachable through the item to be written
855       * (or its owner document) in this order:
856       * <ol>
857       * <li>
858       * <code>Document.inputEncoding</code>,
859       * </li>
860       * <li>
861       * <code>Document.xmlEncoding</code>.
862       * </li>
863       * </ol>
864       * <br> If no encoding is reachable through the above properties, a
865       * default encoding of "UTF-8" will be used.
866       * <br> If the specified encoding is not supported an
867       * "unsupported-encoding" error is raised.
868       * @param node The node to serialize.
869       * @param URI The URI to write to.
870       * @return Returns <code>true</code> if <code>node</code> was
871       * successfully serialized and <code>false</code> in case the node
872       * couldn't be serialized.
873       */

874     public boolean writeToURI(Node JavaDoc node, String JavaDoc URI) throws LSException JavaDoc{
875         if (node == null){
876             return false;
877         }
878
879         Method getXmlVersion = null;
880         XMLSerializer ser = null;
881         String JavaDoc ver = null;
882         String JavaDoc encoding = null;
883
884         Document JavaDoc fDocument =(node.getNodeType() == Node.DOCUMENT_NODE)
885                 ? (Document JavaDoc) node
886                 : node.getOwnerDocument();
887         // this should run under JDK 1.1.8...
888
try {
889             getXmlVersion =
890                 fDocument.getClass().getMethod("getXmlVersion", new Class JavaDoc[] {});
891             if (getXmlVersion != null) {
892                 ver = (String JavaDoc) getXmlVersion.invoke(fDocument, null);
893             }
894         } catch (Exception JavaDoc e) {
895             // no way to test the version...
896
// ignore the exception
897
}
898         if (ver != null && ver.equals("1.1")) {
899             if (xml11Serializer == null) {
900                 xml11Serializer = new XML11Serializer();
901                 initSerializer(xml11Serializer);
902             }
903             // copy setting from "main" serializer to XML 1.1 serializer
904
copySettings(serializer, xml11Serializer);
905             ser = xml11Serializer;
906         } else {
907             ser = serializer;
908         }
909
910         try {
911             Method getEncoding =
912                 fDocument.getClass().getMethod("getInputEncoding", new Class JavaDoc[] {});
913             if (getEncoding != null) {
914                 encoding = (String JavaDoc) getEncoding.invoke(fDocument, null);
915             }
916         } catch (Exception JavaDoc e) {
917             // ignore the exception
918
}
919         if (encoding == null) {
920             try {
921                 Method getEncoding =
922                     fDocument.getClass().getMethod("getXmlEncoding", new Class JavaDoc[] {});
923                 if (getEncoding != null) {
924                     encoding = (String JavaDoc) getEncoding.invoke(fDocument, null);
925                 }
926             } catch (Exception JavaDoc e) {
927                 // ignore the exception
928
}
929             if (encoding == null) {
930                 encoding = "UTF-8";
931             }
932         }
933
934         try {
935             prepareForSerialization(ser, node);
936             ser._format.setEncoding(encoding);
937             
938             // URI was specified. Handle relative URIs.
939
String JavaDoc expanded = XMLEntityManager.expandSystemId(URI, null, true);
940             URL JavaDoc url = new URL JavaDoc(expanded != null ? expanded : URI);
941             OutputStream JavaDoc out = null;
942             String JavaDoc protocol = url.getProtocol();
943             String JavaDoc host = url.getHost();
944             // Use FileOutputStream if this URI is for a local file.
945
if (protocol.equals("file")
946                 && (host == null || host.length() == 0 || host.equals("localhost"))) {
947                 // REVISIT: We have to decode %nn sequences. For
948
// now files containing spaces and other characters
949
// which were escaped in the URI will fail. -- mrglavas
950
out = new FileOutputStream JavaDoc(new File JavaDoc(url.getPath()));
951             }
952             // Try to write to some other kind of URI. Some protocols
953
// won't support this, though HTTP should work.
954
else {
955                 URLConnection JavaDoc urlCon = url.openConnection();
956                 urlCon.setDoInput(false);
957                 urlCon.setDoOutput(true);
958                 urlCon.setUseCaches(false); // Enable tunneling.
959
if (urlCon instanceof HttpURLConnection JavaDoc) {
960                     // The DOM L3 LS CR says if we are writing to an HTTP URI
961
// it is to be done with an HTTP PUT.
962
HttpURLConnection JavaDoc httpCon = (HttpURLConnection JavaDoc) urlCon;
963                     httpCon.setRequestMethod("PUT");
964                 }
965                 out = urlCon.getOutputStream();
966             }
967             ser.setOutputByteStream(out);
968
969             if (node.getNodeType() == Node.DOCUMENT_NODE)
970                 ser.serialize((Document JavaDoc) node);
971             else if (node.getNodeType() == Node.DOCUMENT_FRAGMENT_NODE)
972                 ser.serialize((DocumentFragment JavaDoc) node);
973             else if (node.getNodeType() == Node.ELEMENT_NODE)
974                 ser.serialize((Element JavaDoc) node);
975             else
976                 return false;
977         } catch (RuntimeException JavaDoc e) {
978             if (e == DOMNormalizer.abort){
979                 // stopped at user request
980
return false;
981             }
982             throw new LSException JavaDoc(LSException.SERIALIZE_ERR, e.toString());
983         } catch (Exception JavaDoc e) {
984             if (ser.fDOMErrorHandler != null) {
985                 DOMErrorImpl error = new DOMErrorImpl();
986                 error.fException = e;
987                 error.fMessage = e.getMessage();
988                 error.fSeverity = DOMError.SEVERITY_ERROR;
989                 ser.fDOMErrorHandler.handleError(error);
990             }
991             throw new LSException JavaDoc(LSException.SERIALIZE_ERR, e.toString());
992         }
993         return true;
994     } //writeURI
995

996     
997     //
998
// Private methods
999
//
1000

1001    private void prepareForSerialization(XMLSerializer ser, Node JavaDoc node) {
1002        ser.reset();
1003        ser.features = features;
1004        ser.fDOMErrorHandler = fErrorHandler;
1005        ser.fNamespaces = (features & NAMESPACES) !=0;
1006        ser._format.setOmitComments((features & COMMENTS)==0);
1007        ser._format.setOmitXMLDeclaration((features & XMLDECL) == 0);
1008 
1009        if ((features & WELLFORMED) != 0) {
1010            // REVISIT: this is inefficient implementation of well-formness. Instead, we should check
1011
// well-formness as we serialize the tree
1012
Node JavaDoc next, root;
1013            root = node;
1014            Method versionChanged;
1015            boolean verifyNames = true;
1016            Document JavaDoc document =(node.getNodeType() == Node.DOCUMENT_NODE)
1017                    ? (Document JavaDoc) node
1018                    : node.getOwnerDocument();
1019            try {
1020                versionChanged = document.getClass().getMethod("isXMLVersionChanged()", new Class JavaDoc[] {});
1021                if (versionChanged != null) {
1022                    verifyNames = ((Boolean JavaDoc)versionChanged.invoke(document, null)).booleanValue();
1023                }
1024            } catch (Exception JavaDoc e) {
1025                //no way to test the version...
1026
//ignore the exception
1027
}
1028            while (node != null) {
1029              verify(node, verifyNames, false);
1030              // Move down to first child
1031
next = node.getFirstChild();
1032              // No child nodes, so walk tree
1033
while (next == null) {
1034                // Move to sibling if possible.
1035
next = node.getNextSibling();
1036                if (next == null){
1037                    node = node.getParentNode();
1038                    if (root == node){
1039                        next = null;
1040                        break;
1041                    }
1042                    next = node.getNextSibling();
1043                }
1044              }
1045              node = next;
1046          }
1047 
1048
1049        }
1050    }
1051    
1052    
1053    private void verify (Node JavaDoc node, boolean verifyNames, boolean xml11Version){
1054
1055        int type = node.getNodeType();
1056        fLocator.fRelatedNode = node;
1057        boolean wellformed;
1058        switch (type) {
1059            case Node.DOCUMENT_NODE:{
1060                break;
1061            }
1062            case Node.DOCUMENT_TYPE_NODE:{
1063                break;
1064            }
1065            case Node.ELEMENT_NODE:{
1066                if (verifyNames){
1067                    if((features & NAMESPACES) != 0){
1068                        wellformed = CoreDocumentImpl.isValidQName(node.getPrefix() , node.getLocalName(), xml11Version) ;
1069                    }
1070                    else{
1071                        wellformed = CoreDocumentImpl.isXMLName(node.getNodeName() , xml11Version);
1072                    }
1073                    if (!wellformed){
1074                            if (!wellformed){
1075                                if (fErrorHandler != null) {
1076                                    String JavaDoc msg = DOMMessageFormatter.formatMessage(
1077                                        DOMMessageFormatter.DOM_DOMAIN,
1078                                        "wf-invalid-character-in-node-name",
1079                                        new Object JavaDoc[]{"Element", node.getNodeName()});
1080                                        DOMNormalizer.reportDOMError(fErrorHandler, fError, fLocator, msg, DOMError.SEVERITY_FATAL_ERROR,
1081                                        "wf-invalid-character-in-node-name");
1082                                }
1083                        
1084                            }
1085                    }
1086                }
1087                
1088                AttributeMap attributes = (node.hasAttributes()) ? (AttributeMap) node.getAttributes() : null;
1089                if (attributes != null) {
1090                    for (int i = 0; i < attributes.getLength(); ++i) {
1091                        Attr JavaDoc attr = (Attr JavaDoc) attributes.item(i);
1092                        fLocator.fRelatedNode = attr;
1093                        DOMNormalizer.isAttrValueWF( fErrorHandler, fError, fLocator,
1094                                      attributes,(AttrImpl) attr, attr.getValue(), xml11Version);
1095                        if (verifyNames) {
1096                            wellformed = CoreDocumentImpl.isXMLName( attr.getNodeName(), xml11Version);
1097                            if (!wellformed) {
1098                                    String JavaDoc msg =
1099                                        DOMMessageFormatter.formatMessage(
1100                                            DOMMessageFormatter.DOM_DOMAIN,
1101                                            "wf-invalid-character-in-node-name",
1102                                            new Object JavaDoc[] { "Attr", node.getNodeName()});
1103                                    DOMNormalizer.reportDOMError( fErrorHandler, fError, fLocator, msg, DOMError.SEVERITY_FATAL_ERROR,
1104                                        "wf-invalid-character-in-node-name");
1105                            }
1106                        }
1107                    }
1108
1109                }
1110                
1111                break;
1112            }
1113        
1114        case Node.COMMENT_NODE: {
1115            // only verify well-formness if comments included in the tree
1116
if ((features & COMMENTS) != 0)
1117                DOMNormalizer.isCommentWF(fErrorHandler, fError, fLocator, ((Comment JavaDoc)node).getData(), xml11Version);
1118            break;
1119        }
1120        case Node.ENTITY_REFERENCE_NODE: {
1121            // only if entity is preserved in the tree
1122
if (verifyNames && (features & ENTITIES) != 0){
1123                CoreDocumentImpl.isXMLName(node.getNodeName() , xml11Version);
1124            }
1125            break;
1126            
1127        }
1128        case Node.CDATA_SECTION_NODE: {
1129            // verify content
1130
DOMNormalizer.isXMLCharWF(fErrorHandler, fError, fLocator, node.getNodeValue(), xml11Version);
1131            // the ]]> string will be checked during serialization
1132
break;
1133        }
1134        case Node.TEXT_NODE:{
1135            DOMNormalizer.isXMLCharWF(fErrorHandler, fError, fLocator, node.getNodeValue(), xml11Version);
1136            break;
1137        }
1138        case Node.PROCESSING_INSTRUCTION_NODE:{
1139            ProcessingInstruction JavaDoc pinode = (ProcessingInstruction JavaDoc)node ;
1140            String JavaDoc target = pinode.getTarget();
1141            if (verifyNames) {
1142                if (xml11Version) {
1143                    wellformed = XML11Char.isXML11ValidName(target);
1144                } else {
1145                    wellformed = XMLChar.isValidName(target);
1146                }
1147
1148                if (!wellformed) {
1149                    String JavaDoc msg =
1150                        DOMMessageFormatter.formatMessage(
1151                            DOMMessageFormatter.DOM_DOMAIN,
1152                            "wf-invalid-character-in-node-name",
1153                            new Object JavaDoc[] { "Element", node.getNodeName()});
1154                    DOMNormalizer.reportDOMError(
1155                        fErrorHandler,
1156                        fError,
1157                        fLocator,
1158                        msg,
1159                        DOMError.SEVERITY_FATAL_ERROR,
1160                        "wf-invalid-character-in-node-name");
1161                }
1162            }
1163            DOMNormalizer.isXMLCharWF(fErrorHandler, fError, fLocator, pinode.getData(), xml11Version);
1164            break;
1165        }
1166        }
1167               
1168    }
1169
1170}//DOMSerializerImpl
1171

1172
1173
1174
1175
Popular Tags