1 57 58 59 60 package com.sun.org.apache.xml.internal.serialize; 61 62 import java.io.File ; 63 import java.io.FileOutputStream ; 64 import java.io.IOException ; 65 import java.io.OutputStream ; 66 import java.io.OutputStreamWriter ; 67 import java.io.StringWriter ; 68 import java.io.UnsupportedEncodingException ; 69 import java.io.Writer ; 70 import java.lang.reflect.Method ; 71 import java.util.Enumeration ; 72 import java.util.Hashtable ; 73 import java.net.URI ; 74 75 import java.net.HttpURLConnection ; 76 import java.net.URL ; 77 import java.net.URLConnection ; 78 import java.util.Vector ; 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 ; 93 import org.w3c.dom.DOMError ; 94 import org.w3c.dom.DOMErrorHandler ; 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 ; 98 import org.w3c.dom.Comment ; 99 import org.w3c.dom.DOMException ; 100 import org.w3c.dom.DOMStringList ; 101 import org.w3c.dom.Document ; 102 import org.w3c.dom.DocumentFragment ; 103 import org.w3c.dom.Element ; 104 import org.w3c.dom.Node ; 105 import org.w3c.dom.ProcessingInstruction ; 106 import org.w3c.dom.ls.LSException ; 107 import org.w3c.dom.ls.LSOutput ; 108 import org.w3c.dom.ls.LSSerializer ; 109 import org.w3c.dom.ls.LSSerializerFilter ; 110 111 112 125 public class DOMSerializerImpl implements LSSerializer , DOMConfiguration { 126 127 131 private XMLSerializer serializer; 134 135 private XML11Serializer xml11Serializer; 137 138 private DOMStringList fRecognizedParameters; 140 141 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 private DOMErrorHandler fErrorHandler = null; 160 private final DOMErrorImpl fError = new DOMErrorImpl(); 161 private final DOMLocatorImpl fLocator = new DOMLocatorImpl(); 162 private static final RuntimeException abort = new RuntimeException (); 163 164 170 public DOMSerializerImpl() { 171 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 193 public DOMConfiguration getDomConfig(){ 194 return this; 195 } 196 197 200 public void setParameter(String name, Object value) throws DOMException { 201 if (value instanceof Boolean ) { 202 boolean state = ((Boolean ) 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 } 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 if (state) { 262 String msg = 263 DOMMessageFormatter.formatMessage( 264 DOMMessageFormatter.DOM_DOMAIN, 265 "FEATURE_NOT_SUPPORTED", 266 new Object [] { name }); 267 throw new DOMException (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 if (!state) { 275 String msg = 276 DOMMessageFormatter.formatMessage( 277 DOMMessageFormatter.DOM_DOMAIN, 278 "FEATURE_NOT_SUPPORTED", 279 new Object [] { name }); 280 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, msg); 281 } 282 } else { 283 String msg = 284 DOMMessageFormatter.formatMessage( 285 DOMMessageFormatter.DOM_DOMAIN, 286 "FEATURE_NOT_FOUND", 287 new Object [] { name }); 288 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, msg); 289 } 290 } else if (name.equalsIgnoreCase(Constants.DOM_ERROR_HANDLER)) { 291 if (value == null || value instanceof DOMErrorHandler ) { 292 fErrorHandler = (DOMErrorHandler )value; 293 } else { 294 String msg = 296 DOMMessageFormatter.formatMessage( 297 DOMMessageFormatter.DOM_DOMAIN, 298 "TYPE_MISMATCH_ERR", 299 new Object [] { name }); 300 throw new DOMException (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 msg = 308 DOMMessageFormatter.formatMessage( 309 DOMMessageFormatter.DOM_DOMAIN, 310 "FEATURE_NOT_SUPPORTED", 311 new Object [] { name }); 312 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, msg); 313 } else { 314 String msg = 315 DOMMessageFormatter.formatMessage( 316 DOMMessageFormatter.DOM_DOMAIN, 317 "FEATURE_NOT_FOUND", 318 new Object [] { name }); 319 throw new DOMException (DOMException.NOT_FOUND_ERR, msg); 320 } 321 } 322 323 326 public boolean canSetParameter(String name, Object state) { 327 if (state instanceof Boolean ){ 328 boolean value = ((Boolean )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 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 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 return value; 358 } 359 } 360 else if (name.equalsIgnoreCase(Constants.DOM_ERROR_HANDLER) && 361 state == null || state instanceof DOMErrorHandler ){ 362 return true; 363 } 364 return false; 365 } 366 367 375 public DOMStringList getParameterNames() { 376 377 if (fRecognizedParameters == null){ 378 Vector parameters = new Vector (); 379 380 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_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 406 408 fRecognizedParameters = new DOMStringListImpl(parameters); 409 410 } 411 412 return fRecognizedParameters; 413 } 414 415 418 public Object getParameter(String name) throws DOMException { 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 msg = 465 DOMMessageFormatter.formatMessage( 466 DOMMessageFormatter.DOM_DOMAIN, 467 "FEATURE_NOT_SUPPORTED", 468 new Object [] { name }); 469 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, msg); 470 } else { 471 String msg = 472 DOMMessageFormatter.formatMessage( 473 DOMMessageFormatter.DOM_DOMAIN, 474 "FEATURE_NOT_FOUND", 475 new Object [] { name }); 476 throw new DOMException (DOMException.NOT_FOUND_ERR, msg); 477 } 478 } 479 480 481 498 public String writeToString(Node wnode) throws DOMException , LSException { 499 Document doc = (wnode.getNodeType() == Node.DOCUMENT_NODE)?(Document )wnode:wnode.getOwnerDocument(); 501 Method getVersion = null; 502 XMLSerializer ser = null; 503 String ver = null; 504 try { 506 getVersion = doc.getClass().getMethod("getXmlVersion", new Class []{}); 507 if(getVersion != null ) { 508 ver = (String )getVersion.invoke(doc, null); 509 } 510 } catch (Exception e) { 511 } 514 if(ver != null && ver.equals("1.1")) { 515 if(xml11Serializer == null) { 516 xml11Serializer = new XML11Serializer(); 517 initSerializer(xml11Serializer); 518 } 519 copySettings(serializer, xml11Serializer); 521 ser = xml11Serializer; 522 } else { 523 ser = serializer; 524 } 525 526 StringWriter destination = new StringWriter (); 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 )wnode); 535 else if (wnode.getNodeType() == Node.DOCUMENT_FRAGMENT_NODE) 536 ser.serialize((DocumentFragment )wnode); 537 else if (wnode.getNodeType() == Node.ELEMENT_NODE) 538 ser.serialize((Element )wnode); 539 else 540 return null; 541 } catch (RuntimeException e) { 542 if (e == DOMNormalizer.abort){ 543 return null; 545 } 546 throw new LSException (LSException.SERIALIZE_ERR, e.toString()); 547 } catch (IOException ioe) { 548 String msg = DOMMessageFormatter.formatMessage( 549 DOMMessageFormatter.DOM_DOMAIN, 550 "STRING_TOO_LONG", 551 new Object [] { ioe.getMessage()}); 552 throw new DOMException (DOMException.DOMSTRING_SIZE_ERR,msg); 553 } 554 return destination.toString(); 555 } 556 557 580 public void setNewLine(String newLine) { 581 serializer._format.setLineSeparator(newLine); 582 } 583 584 585 608 public String getNewLine() { 609 return serializer._format.getLineSeparator(); 610 } 611 612 613 619 public LSSerializerFilter getFilter(){ 620 return serializer.fDOMFilter; 621 } 622 628 public void setFilter(LSSerializerFilter filter){ 629 serializer.fDOMFilter = filter; 630 } 631 632 private void initSerializer(XMLSerializer ser) { 634 ser.fNSBinder = new NamespaceSupport(); 635 ser.fLocalNSBinder = new NamespaceSupport(); 636 ser.fSymbolTable = new SymbolTable(); 637 } 638 639 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 } 651 681 public boolean write(Node node, LSOutput destination) throws LSException { 682 683 if (node == null) 684 return false; 685 686 Method getVersion = null; 687 XMLSerializer ser = null; 688 String ver = null; 689 Document fDocument =(node.getNodeType() == Node.DOCUMENT_NODE) 690 ? (Document ) node 691 : node.getOwnerDocument(); 692 try { 694 getVersion = fDocument.getClass().getMethod("getXmlVersion", new Class [] {}); 695 if (getVersion != null) { 696 ver = (String ) getVersion.invoke(fDocument, null); 697 } 698 } catch (Exception e) { 699 } 702 if (ver != null && ver.equals("1.1")) { 704 if (xml11Serializer == null) { 705 xml11Serializer = new XML11Serializer(); 706 initSerializer(xml11Serializer); 707 } 708 copySettings(serializer, xml11Serializer); 710 ser = xml11Serializer; 711 } else { 712 ser = serializer; 713 } 714 715 String encoding = null; 716 if ((encoding = destination.getEncoding()) == null) { 717 try { 718 Method getEncoding = 719 fDocument.getClass().getMethod("getInputEncoding", new Class [] {}); 720 if (getEncoding != null) { 721 encoding = (String ) getEncoding.invoke(fDocument, null); 722 } 723 } catch (Exception e) { 724 } 726 if (encoding == null) { 727 try { 728 Method getEncoding = 729 fDocument.getClass().getMethod("getXmlEncoding", new Class [] {}); 730 if (getEncoding != null) { 731 encoding = (String ) getEncoding.invoke(fDocument, null); 732 } 733 } catch (Exception e) { 734 } 736 if (encoding == null) { 737 encoding = "UTF-8"; 738 } 739 } 740 } 741 try { 742 prepareForSerialization(ser, node); 743 ser._format.setEncoding(encoding); 744 OutputStream outputStream = destination.getByteStream(); 745 Writer writer = destination.getCharacterStream(); 746 String 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 (LSException.SERIALIZE_ERR, 758 DOMMessageFormatter.formatMessage( 759 DOMMessageFormatter.SERIALIZER_DOMAIN, 760 "no-output-specified", null)); 761 } 762 else { 763 String expanded = XMLEntityManager.expandSystemId(uri, null, true); 765 URL url = new URL (expanded != null ? expanded : uri); 766 OutputStream out = null; 767 String protocol = url.getProtocol(); 768 String host = url.getHost(); 769 if (protocol.equals("file") 771 && (host == null || host.length() == 0 || host.equals("localhost"))) { 772 out = new FileOutputStream (new File (url.getPath())); 776 } 777 else { 780 URLConnection urlCon = url.openConnection(); 781 urlCon.setDoInput(false); 782 urlCon.setDoOutput(true); 783 urlCon.setUseCaches(false); if (urlCon instanceof HttpURLConnection ) { 785 HttpURLConnection httpCon = (HttpURLConnection ) urlCon; 788 httpCon.setRequestMethod("PUT"); 789 } 790 out = urlCon.getOutputStream(); 791 } 792 ser.setOutputByteStream(out); 793 } 794 } 795 else { 796 ser.setOutputByteStream(outputStream); 798 } 799 } 800 else { 801 ser.setOutputCharStream(writer); 803 } 804 805 if (node.getNodeType() == Node.DOCUMENT_NODE) 806 ser.serialize((Document ) node); 807 else if (node.getNodeType() == Node.DOCUMENT_FRAGMENT_NODE) 808 ser.serialize((DocumentFragment ) node); 809 else if (node.getNodeType() == Node.ELEMENT_NODE) 810 ser.serialize((Element ) node); 811 else 812 return false; 813 } catch( UnsupportedEncodingException 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 (LSException.SERIALIZE_ERR, 823 DOMMessageFormatter.formatMessage( 824 DOMMessageFormatter.SERIALIZER_DOMAIN, 825 "unsupported-encoding", null)); 826 } catch (RuntimeException e) { 828 if (e == DOMNormalizer.abort){ 829 return false; 831 } 832 throw new LSException (LSException.SERIALIZE_ERR, e.toString()); 833 } catch (Exception 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 (LSException.SERIALIZE_ERR, e.toString()); 844 } 845 return true; 846 847 } 849 874 public boolean writeToURI(Node node, String URI) throws LSException { 875 if (node == null){ 876 return false; 877 } 878 879 Method getXmlVersion = null; 880 XMLSerializer ser = null; 881 String ver = null; 882 String encoding = null; 883 884 Document fDocument =(node.getNodeType() == Node.DOCUMENT_NODE) 885 ? (Document ) node 886 : node.getOwnerDocument(); 887 try { 889 getXmlVersion = 890 fDocument.getClass().getMethod("getXmlVersion", new Class [] {}); 891 if (getXmlVersion != null) { 892 ver = (String ) getXmlVersion.invoke(fDocument, null); 893 } 894 } catch (Exception e) { 895 } 898 if (ver != null && ver.equals("1.1")) { 899 if (xml11Serializer == null) { 900 xml11Serializer = new XML11Serializer(); 901 initSerializer(xml11Serializer); 902 } 903 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 [] {}); 913 if (getEncoding != null) { 914 encoding = (String ) getEncoding.invoke(fDocument, null); 915 } 916 } catch (Exception e) { 917 } 919 if (encoding == null) { 920 try { 921 Method getEncoding = 922 fDocument.getClass().getMethod("getXmlEncoding", new Class [] {}); 923 if (getEncoding != null) { 924 encoding = (String ) getEncoding.invoke(fDocument, null); 925 } 926 } catch (Exception e) { 927 } 929 if (encoding == null) { 930 encoding = "UTF-8"; 931 } 932 } 933 934 try { 935 prepareForSerialization(ser, node); 936 ser._format.setEncoding(encoding); 937 938 String expanded = XMLEntityManager.expandSystemId(URI, null, true); 940 URL url = new URL (expanded != null ? expanded : URI); 941 OutputStream out = null; 942 String protocol = url.getProtocol(); 943 String host = url.getHost(); 944 if (protocol.equals("file") 946 && (host == null || host.length() == 0 || host.equals("localhost"))) { 947 out = new FileOutputStream (new File (url.getPath())); 951 } 952 else { 955 URLConnection urlCon = url.openConnection(); 956 urlCon.setDoInput(false); 957 urlCon.setDoOutput(true); 958 urlCon.setUseCaches(false); if (urlCon instanceof HttpURLConnection ) { 960 HttpURLConnection httpCon = (HttpURLConnection ) 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 ) node); 971 else if (node.getNodeType() == Node.DOCUMENT_FRAGMENT_NODE) 972 ser.serialize((DocumentFragment ) node); 973 else if (node.getNodeType() == Node.ELEMENT_NODE) 974 ser.serialize((Element ) node); 975 else 976 return false; 977 } catch (RuntimeException e) { 978 if (e == DOMNormalizer.abort){ 979 return false; 981 } 982 throw new LSException (LSException.SERIALIZE_ERR, e.toString()); 983 } catch (Exception 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 (LSException.SERIALIZE_ERR, e.toString()); 992 } 993 return true; 994 } 996 997 1001 private void prepareForSerialization(XMLSerializer ser, Node 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 Node next, root; 1013 root = node; 1014 Method versionChanged; 1015 boolean verifyNames = true; 1016 Document document =(node.getNodeType() == Node.DOCUMENT_NODE) 1017 ? (Document ) node 1018 : node.getOwnerDocument(); 1019 try { 1020 versionChanged = document.getClass().getMethod("isXMLVersionChanged()", new Class [] {}); 1021 if (versionChanged != null) { 1022 verifyNames = ((Boolean )versionChanged.invoke(document, null)).booleanValue(); 1023 } 1024 } catch (Exception e) { 1025 } 1028 while (node != null) { 1029 verify(node, verifyNames, false); 1030 next = node.getFirstChild(); 1032 while (next == null) { 1034 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 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 msg = DOMMessageFormatter.formatMessage( 1077 DOMMessageFormatter.DOM_DOMAIN, 1078 "wf-invalid-character-in-node-name", 1079 new Object []{"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 attr = (Attr ) 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 msg = 1099 DOMMessageFormatter.formatMessage( 1100 DOMMessageFormatter.DOM_DOMAIN, 1101 "wf-invalid-character-in-node-name", 1102 new Object [] { "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 if ((features & COMMENTS) != 0) 1117 DOMNormalizer.isCommentWF(fErrorHandler, fError, fLocator, ((Comment )node).getData(), xml11Version); 1118 break; 1119 } 1120 case Node.ENTITY_REFERENCE_NODE: { 1121 if (verifyNames && (features & ENTITIES) != 0){ 1123 CoreDocumentImpl.isXMLName(node.getNodeName() , xml11Version); 1124 } 1125 break; 1126 1127 } 1128 case Node.CDATA_SECTION_NODE: { 1129 DOMNormalizer.isXMLCharWF(fErrorHandler, fError, fLocator, node.getNodeValue(), xml11Version); 1131 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 pinode = (ProcessingInstruction )node ; 1140 String 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 msg = 1150 DOMMessageFormatter.formatMessage( 1151 DOMMessageFormatter.DOM_DOMAIN, 1152 "wf-invalid-character-in-node-name", 1153 new Object [] { "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} 1172 1173 1174 1175 | Popular Tags |