DomConsumer.java 33.1 KB
Newer Older
1
/* DomConsumer.java --
Tom Tromey committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
   Copyright (C) 1999,2000,2001 Free Software Foundation, Inc.

This file is part of GNU Classpath.

GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version. */

package gnu.xml.pipeline;

import gnu.xml.util.DomParser;

import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.DTDHandler;
import org.xml.sax.ErrorHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXParseException;
import org.xml.sax.ext.DeclHandler;
import org.xml.sax.ext.LexicalHandler;
import org.xml.sax.helpers.AttributesImpl;
import org.w3c.dom.Attr;
import org.w3c.dom.CDATASection;
import org.w3c.dom.CharacterData;
import org.w3c.dom.Document;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Element;
import org.w3c.dom.EntityReference;
import org.w3c.dom.Node;
import org.w3c.dom.ProcessingInstruction;
import org.w3c.dom.Text;

/**
 * This consumer builds a DOM Document from its input, acting either as a
 * pipeline terminus or as an intermediate buffer.  When a document's worth
 * of events has been delivered to this consumer, that document is read with
 * a {@link DomParser} and sent to the next consumer.  It is also available
 * as a read-once property.
 *
 * <p>The DOM tree is constructed as faithfully as possible.  There are some
 * complications since a DOM should expose behaviors that can't be implemented
 * without API backdoors into that DOM, and because some SAX parsers don't
 * report all the information that DOM permits to be exposed.  The general
 * problem areas involve information from the Document Type Declaration (DTD).
 * DOM only represents a limited subset, but has some behaviors that depend
 * on much deeper knowledge of a document's DTD.  You shouldn't have much to
 * worry about unless you change handling of "noise" nodes from its default
 * setting (which ignores them all); note if you use JAXP to populate your
 * DOM trees, it wants to save "noise" nodes by default.  (Such nodes include
 * ignorable whitespace, comments, entity references and CDATA boundaries.)
 * Otherwise, your
 * main worry will be if you use a SAX parser that doesn't flag ignorable
 * whitespace unless it's validating (few don't).
 *
 * <p> The SAX2 events used as input must contain XML Names for elements
 * and attributes, with original prefixes.  In SAX2,
 * this is optional unless the "namespace-prefixes" parser feature is set.
 * Moreover, many application components won't provide completely correct
 * structures anyway.  <em>Before you convert a DOM to an output document,
 * you should plan to postprocess it to create or repair such namespace
 * information.</em> The {@link NSFilter} pipeline stage does such work.
 *
 * <p> <em>Note:  changes late in DOM L2 process made it impractical to
 * attempt to create the DocumentType node in any implementation-neutral way,
 * much less to populate it (L1 didn't support even creating such nodes).
 * To create and populate such a node, subclass the inner
 * {@link DomConsumer.Handler} class and teach it about the backdoors into
 * whatever DOM implementation you want.  It's possible that some revised
 * DOM API (L3?) will make this problem solvable again. </em>
 *
 * @see DomParser
 *
 * @author David Brownell
 */
public class DomConsumer implements EventConsumer
{
108
    private Class               domImpl;
Tom Tromey committed
109

110 111 112 113
    private boolean             hidingCDATA = true;
    private boolean             hidingComments = true;
    private boolean             hidingWhitespace = true;
    private boolean             hidingReferences = true;
Tom Tromey committed
114

115 116
    private Handler             handler;
    private ErrorHandler        errHandler;
Tom Tromey committed
117

118
    private EventConsumer       next;
Tom Tromey committed
119 120 121 122 123 124 125 126 127 128 129

    // FIXME:  this can't be a generic pipeline stage just now,
    // since its input became a Class not a String (to be turned
    // into a class, using the right class loader)


    /**
     * Configures this pipeline terminus to use the specified implementation
     * of DOM when constructing its result value.
     *
     * @param impl class implementing {@link org.w3c.dom.Document Document}
130
     *  which publicly exposes a default constructor
Tom Tromey committed
131 132
     *
     * @exception SAXException when there is a problem creating an
133
     *  empty DOM document using the specified implementation
Tom Tromey committed
134 135 136 137
     */
    public DomConsumer (Class impl)
    throws SAXException
    {
138 139
        domImpl = impl;
        handler = new Handler (this);
Tom Tromey committed
140 141 142 143 144 145 146 147 148 149 150
    }

    /**
     * This is the hook through which a subclass provides a handler
     * which knows how to access DOM extensions, specific to some
     * implementation, to record additional data in a DOM.
     * Treat this as part of construction; don't call it except
     * before (or between) parses.
     */
    protected void setHandler (Handler h)
    {
151
        handler = h;
Tom Tromey committed
152 153 154 155 156 157
    }


    private Document emptyDocument ()
    throws SAXException
    {
158 159 160 161 162 163 164 165 166
        try {
            return (Document) domImpl.newInstance ();
        } catch (IllegalAccessException e) {
            throw new SAXException ("can't access constructor: "
                    + e.getMessage ());
        } catch (InstantiationException e) {
            throw new SAXException ("can't instantiate Document: "
                    + e.getMessage ());
        }
Tom Tromey committed
167 168 169 170 171 172 173 174 175 176 177 178 179 180
    }


    /**
     * Configures this consumer as a buffer/filter, using the specified
     * DOM implementation when constructing its result value.
     *
     * <p> This event consumer acts as a buffer and filter, in that it
     * builds a DOM tree and then writes it out when <em>endDocument</em>
     * is invoked.  Because of the limitations of DOM, much information
     * will as a rule not be seen in that replay.  To get a full fidelity
     * copy of the input event stream, use a {@link TeeConsumer}.
     *
     * @param impl class implementing {@link org.w3c.dom.Document Document}
181
     *  which publicly exposes a default constructor
Tom Tromey committed
182
     * @param next receives a "replayed" sequence of parse events when
183
     *  the <em>endDocument</em> method is invoked.
Tom Tromey committed
184 185
     *
     * @exception SAXException when there is a problem creating an
186
     *  empty DOM document using the specified DOM implementation
Tom Tromey committed
187 188 189 190
     */
    public DomConsumer (Class impl, EventConsumer n)
    throws SAXException
    {
191 192
        this (impl);
        next = n;
Tom Tromey committed
193 194 195 196 197 198 199
    }


    /**
     * Returns the document constructed from the preceding
     * sequence of events.  This method should not be
     * used again until another sequence of events has been
200
     * given to this EventConsumer.
Tom Tromey committed
201 202 203
     */
    final public Document getDocument ()
    {
204
        return handler.clearDocument ();
Tom Tromey committed
205 206 207 208
    }

    public void setErrorHandler (ErrorHandler handler)
    {
209
        errHandler = handler;
Tom Tromey committed
210 211 212 213 214 215 216 217 218 219 220 221
    }


    /**
     * Returns true if the consumer is hiding entity references nodes
     * (the default), and false if EntityReference nodes should
     * instead be created.  Such EntityReference nodes will normally be
     * empty, unless an implementation arranges to populate them and then
     * turn them back into readonly objects.
     *
     * @see #setHidingReferences
     */
222 223
    final public boolean        isHidingReferences ()
        { return hidingReferences; }
Tom Tromey committed
224 225 226 227 228 229 230 231

    /**
     * Controls whether the consumer will hide entity expansions,
     * or will instead mark them with entity reference nodes.
     *
     * @see #isHidingReferences
     * @param flag False if entity reference nodes will appear
     */
232 233 234
    final public void           setHidingReferences (boolean flag)
        { hidingReferences = flag; }

Tom Tromey committed
235 236 237 238 239 240 241 242

    /**
     * Returns true if the consumer is hiding comments (the default),
     * and false if they should be placed into the output document.
     *
     * @see #setHidingComments
     */
    public final boolean isHidingComments ()
243
        { return hidingComments; }
Tom Tromey committed
244 245 246 247 248 249 250

    /**
     * Controls whether the consumer is hiding comments.
     *
     * @see #isHidingComments
     */
    public final void setHidingComments (boolean flag)
251
        { hidingComments = flag; }
Tom Tromey committed
252 253 254 255 256 257 258 259 260 261


    /**
     * Returns true if the consumer is hiding ignorable whitespace
     * (the default), and false if such whitespace should be placed
     * into the output document as children of element nodes.
     *
     * @see #setHidingWhitespace
     */
    public final boolean isHidingWhitespace ()
262
        { return hidingWhitespace; }
Tom Tromey committed
263 264 265 266 267 268 269

    /**
     * Controls whether the consumer hides ignorable whitespace
     *
     * @see #isHidingComments
     */
    public final void setHidingWhitespace (boolean flag)
270
        { hidingWhitespace = flag; }
Tom Tromey committed
271 272 273 274 275 276 277 278


    /**
     * Returns true if the consumer is saving CDATA boundaries, or
     * false (the default) otherwise.
     *
     * @see #setHidingCDATA
     */
279 280
    final public boolean        isHidingCDATA ()
        { return hidingCDATA; }
Tom Tromey committed
281 282 283 284 285 286

    /**
     * Controls whether the consumer will save CDATA boundaries.
     *
     * @see #isHidingCDATA
     * @param flag True to treat CDATA text differently from other
287
     *  text nodes
Tom Tromey committed
288
     */
289 290 291
    final public void           setHidingCDATA (boolean flag)
        { hidingCDATA = flag; }

Tom Tromey committed
292 293 294 295


    /** Returns the document handler being used. */
    final public ContentHandler getContentHandler ()
296
        { return handler; }
Tom Tromey committed
297 298 299

    /** Returns the DTD handler being used. */
    final public DTDHandler getDTDHandler ()
300
        { return handler; }
Tom Tromey committed
301 302 303 304 305 306 307 308

    /**
     * Returns the lexical handler being used.
     * (DOM construction can't really use declaration handlers.)
     */
    final public Object getProperty (String id)
    throws SAXNotRecognizedException
    {
309 310 311 312 313
        if ("http://xml.org/sax/properties/lexical-handler".equals (id))
            return handler;
        if ("http://xml.org/sax/properties/declaration-handler".equals (id))
            return handler;
        throw new SAXNotRecognizedException (id);
Tom Tromey committed
314 315 316 317 318 319 320 321 322
    }

    EventConsumer getNext () { return next; }

    ErrorHandler getErrorHandler () { return errHandler; }

    /**
     * Class used to intercept various parsing events and use them to
     * populate a DOM document.  Subclasses would typically know and use
323
     * backdoors into specific DOM implementations, used to implement
Tom Tromey committed
324 325 326 327 328 329 330 331
     * DTD-related functionality.
     *
     * <p> Note that if this ever throws a DOMException (runtime exception)
     * that will indicate a bug in the DOM (e.g. doesn't support something
     * per specification) or the parser (e.g. emitted an illegal name, or
     * accepted illegal input data). </p>
     */
    public static class Handler
332 333
        implements ContentHandler, LexicalHandler,
            DTDHandler, DeclHandler
Tom Tromey committed
334
    {
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965
        protected DomConsumer           consumer;

        private DOMImplementation       impl;
        private Document                document;
        private boolean         isL2;

        private Locator         locator;
        private Node            top;
        private boolean         inCDATA;
        private boolean         mergeCDATA;
        private boolean         inDTD;
        private String          currentEntity;

        private boolean         recreatedAttrs;
        private AttributesImpl  attributes = new AttributesImpl ();

        /**
         * Subclasses may use SAX2 events to provide additional
         * behaviors in the resulting DOM.
         */
        protected Handler (DomConsumer consumer)
        throws SAXException
        {
            this.consumer = consumer;
            document = consumer.emptyDocument ();
            impl = document.getImplementation ();
            isL2 = impl.hasFeature ("XML", "2.0");
        }

        private void fatal (String message, Exception x)
        throws SAXException
        {
            SAXParseException   e;
            ErrorHandler        errHandler = consumer.getErrorHandler ();

            if (locator == null)
                e = new SAXParseException (message, null, null, -1, -1, x);
            else
                e = new SAXParseException (message, locator, x);
            if (errHandler != null)
                errHandler.fatalError (e);
            throw e;
        }

        /**
         * Returns and forgets the document produced.  If the handler is
         * reused, a new document may be created.
         */
        Document clearDocument ()
        {
            Document retval = document;
            document = null;
            locator = null;
            return retval;
        }

        /**
         * Returns the document under construction.
         */
        protected Document getDocument ()
            { return document; }

        /**
         * Returns the current node being populated.  This is usually
         * an Element or Document, but it might be an EntityReference
         * node if some implementation-specific code knows how to put
         * those into the result tree and later mark them as readonly.
         */
        protected Node getTop ()
            { return top; }


        // SAX1
        public void setDocumentLocator (Locator locator)
        {
            this.locator = locator;
        }

        // SAX1
        public void startDocument ()
        throws SAXException
        {
            if (document == null)
                try {
                    if (isL2) {
                        // couple to original implementation
                        document = impl.createDocument (null, "foo", null);
                        document.removeChild (document.getFirstChild ());
                    } else {
                        document = consumer.emptyDocument ();
                    }
                } catch (Exception e) {
                    fatal ("DOM create document", e);
                }
            top = document;
        }

        // SAX1
        public void endDocument ()
        throws SAXException
        {
            try {
                if (consumer.getNext () != null && document != null) {
                    DomParser   parser = new DomParser (document);

                    EventFilter.bind (parser, consumer.getNext ());
                    parser.parse ("ignored");
                }
            } finally {
                top = null;
            }
        }

        // SAX1
        public void processingInstruction (String target, String data)
        throws SAXException
        {
            // we can't create populated entity ref nodes using
            // only public DOM APIs (they've got to be readonly)
            if (currentEntity != null)
                return;

            ProcessingInstruction       pi;

            if (isL2
                    // && consumer.isUsingNamespaces ()
                    && target.indexOf (':') != -1)
                namespaceError (
                    "PI target name is namespace nonconformant: "
                        + target);
            if (inDTD)
                return;
            pi = document.createProcessingInstruction (target, data);
            top.appendChild (pi);
        }

        /**
         * Subclasses may overrride this method to provide a more efficient
         * way to construct text nodes.
         * Typically, copying the text into a single character array will
         * be more efficient than doing that as well as allocating other
         * needed for a String, including an internal StringBuffer.
         * Those additional memory and CPU costs can be incurred later,
         * if ever needed.
         * Unfortunately the standard DOM factory APIs encourage those costs
         * to be incurred early.
         */
        protected Text createText (
            boolean     isCDATA,
            char        ch [],
            int         start,
            int         length
        ) {
            String      value = new String (ch, start, length);

            if (isCDATA)
                return document.createCDATASection (value);
            else
                return document.createTextNode (value);
        }

        // SAX1
        public void characters (char ch [], int start, int length)
        throws SAXException
        {
            // we can't create populated entity ref nodes using
            // only public DOM APIs (they've got to be readonly
            // at creation time)
            if (currentEntity != null)
                return;

            Node        lastChild = top.getLastChild ();

            // merge consecutive text or CDATA nodes if appropriate.
            if (lastChild instanceof Text) {
                if (consumer.isHidingCDATA ()
                        // consecutive Text content ... always merge
                        || (!inCDATA
                            && !(lastChild instanceof CDATASection))
                        // consecutive CDATASection content ... don't
                        // merge between sections, only within them
                        || (inCDATA && mergeCDATA
                            && lastChild instanceof CDATASection)
                            ) {
                    CharacterData       last = (CharacterData) lastChild;
                    String              value = new String (ch, start, length);

                    last.appendData (value);
                    return;
                }
            }
            if (inCDATA && !consumer.isHidingCDATA ()) {
                top.appendChild (createText (true, ch, start, length));
                mergeCDATA = true;
            } else
                top.appendChild (createText (false, ch, start, length));
        }

        // SAX2
        public void skippedEntity (String name)
        throws SAXException
        {
            // this callback is useless except to report errors, since
            // we can't know if the ref was in content, within an
            // attribute, within a declaration ... only one of those
            // cases supports more intelligent action than a panic.
            fatal ("skipped entity: " + name, null);
        }

        // SAX2
        public void startPrefixMapping (String prefix, String uri)
        throws SAXException
        {
            // reconstruct "xmlns" attributes deleted by all
            // SAX2 parsers without "namespace-prefixes" = true
            if ("".equals (prefix))
                attributes.addAttribute ("", "", "xmlns",
                        "CDATA", uri);
            else
                attributes.addAttribute ("", "", "xmlns:" + prefix,
                        "CDATA", uri);
            recreatedAttrs = true;
        }

        // SAX2
        public void endPrefixMapping (String prefix)
        throws SAXException
            { }

        // SAX2
        public void startElement (
            String uri,
            String localName,
            String qName,
            Attributes atts
        ) throws SAXException
        {
            // we can't create populated entity ref nodes using
            // only public DOM APIs (they've got to be readonly)
            if (currentEntity != null)
                return;

            // parser discarded basic information; DOM tree isn't writable
            // without massaging to assign prefixes to all nodes.
            // the "NSFilter" class does that massaging.
            if (qName.length () == 0)
                qName = localName;


            Element     element;
            int         length = atts.getLength ();

            if (!isL2) {
                element = document.createElement (qName);

                // first the explicit attributes ...
                length = atts.getLength ();
                for (int i = 0; i < length; i++)
                    element.setAttribute (atts.getQName (i),
                                            atts.getValue (i));
                // ... then any recreated ones (DOM deletes duplicates)
                if (recreatedAttrs) {
                    recreatedAttrs = false;
                    length = attributes.getLength ();
                    for (int i = 0; i < length; i++)
                        element.setAttribute (attributes.getQName (i),
                                                attributes.getValue (i));
                    attributes.clear ();
                }

                top.appendChild (element);
                top = element;
                return;
            }

            // For an L2 DOM when namespace use is enabled, use
            // createElementNS/createAttributeNS except when
            // (a) it's an element in the default namespace, or
            // (b) it's an attribute with no prefix
            String      namespace;

            if (localName.length () != 0)
                namespace = (uri.length () == 0) ? null : uri;
            else
                namespace = getNamespace (getPrefix (qName), atts);

            if (namespace == null)
                element = document.createElement (qName);
            else
                element = document.createElementNS (namespace, qName);

            populateAttributes (element, atts);
            if (recreatedAttrs) {
                recreatedAttrs = false;
                // ... DOM deletes any duplicates
                populateAttributes (element, attributes);
                attributes.clear ();
            }

            top.appendChild (element);
            top = element;
        }

        final static String     xmlnsURI = "http://www.w3.org/2000/xmlns/";

        private void populateAttributes (Element element, Attributes attrs)
        throws SAXParseException
        {
            int         length = attrs.getLength ();

            for (int i = 0; i < length; i++) {
                String  type = attrs.getType (i);
                String  value = attrs.getValue (i);
                String  name = attrs.getQName (i);
                String  local = attrs.getLocalName (i);
                String  uri = attrs.getURI (i);

                // parser discarded basic information, DOM tree isn't writable
                if (name.length () == 0)
                    name = local;

                // all attribute types other than these three may not
                // contain scoped names... enumerated attributes get
                // reported as NMTOKEN, except for NOTATION values
                if (!("CDATA".equals (type)
                        || "NMTOKEN".equals (type)
                        || "NMTOKENS".equals (type))) {
                    if (value.indexOf (':') != -1) {
                        namespaceError (
                                "namespace nonconformant attribute value: "
                                    + "<" + element.getNodeName ()
                                    + " " + name + "='" + value + "' ...>");
                    }
                }

                // xmlns="" is legal (undoes default NS)
                // xmlns:foo="" is illegal
                String prefix = getPrefix (name);
                String namespace;

                if ("xmlns".equals (prefix)) {
                    if ("".equals (value))
                        namespaceError ("illegal null namespace decl, " + name);
                    namespace = xmlnsURI;
                } else if ("xmlns".equals (name))
                    namespace = xmlnsURI;

                else if (prefix == null)
                    namespace = null;
                else if (!"".equals(uri) && uri.length () != 0)
                    namespace = uri;
                else
                    namespace = getNamespace (prefix, attrs);

                if (namespace == null)
                    element.setAttribute (name, value);
                else
                    element.setAttributeNS (namespace, name, value);
            }
        }

        private String getPrefix (String name)
        {
            int         temp;

            if ((temp = name.indexOf (':')) > 0)
                return name.substring (0, temp);
            return null;
        }

        // used with SAX1-level parser output
        private String getNamespace (String prefix, Attributes attrs)
        throws SAXParseException
        {
            String namespace;
            String decl;

            // defaulting
            if (prefix == null) {
                decl = "xmlns";
                namespace = attrs.getValue (decl);
                if ("".equals (namespace))
                    return null;
                else if (namespace != null)
                    return namespace;

            // "xmlns" is like a keyword
            // ... according to the Namespace REC, but DOM L2 CR2+
            // and Infoset violate that by assigning a namespace.
            // that conflict is resolved elsewhere.
            } else if ("xmlns".equals (prefix))
                return null;

            // "xml" prefix is fixed
            else if ("xml".equals (prefix))
                return "http://www.w3.org/XML/1998/namespace";

            // otherwise, expect a declaration
            else {
                decl = "xmlns:" + prefix;
                namespace = attrs.getValue (decl);
            }

            // if we found a local declaration, great
            if (namespace != null)
                return namespace;


            // ELSE ... search up the tree we've been building
            for (Node n = top;
                    n != null && n.getNodeType () != Node.DOCUMENT_NODE;
                    n = n.getParentNode ()) {
                if (n.getNodeType () == Node.ENTITY_REFERENCE_NODE)
                    continue;
                Element e = (Element) n;
                Attr attr = e.getAttributeNode (decl);
                if (attr != null)
                    return attr.getNodeValue ();
            }
            // see above re "xmlns" as keyword
            if ("xmlns".equals (decl))
                return null;

            namespaceError ("Undeclared namespace prefix: " + prefix);
            return null;
        }

        // SAX2
        public void endElement (String uri, String localName, String qName)
        throws SAXException
        {
            // we can't create populated entity ref nodes using
            // only public DOM APIs (they've got to be readonly)
            if (currentEntity != null)
                return;

            top = top.getParentNode ();
        }

        // SAX1 (mandatory reporting if validating)
        public void ignorableWhitespace (char ch [], int start, int length)
        throws SAXException
        {
            if (consumer.isHidingWhitespace ())
                return;
            characters (ch, start, length);
        }

        // SAX2 lexical event
        public void startCDATA ()
        throws SAXException
        {
            inCDATA = true;
            // true except for the first fragment of a cdata section
            mergeCDATA = false;
        }

        // SAX2 lexical event
        public void endCDATA ()
        throws SAXException
        {
            inCDATA = false;
        }

        // SAX2 lexical event
        //
        // this SAX2 callback merges two unrelated things:
        //      - Declaration of the root element type ... belongs with
        //    the other DTD declaration methods, NOT HERE.
        //      - IDs for the optional external subset ... belongs here
        //    with other lexical information.
        //
        // ...and it doesn't include the internal DTD subset, desired
        // both to support DOM L2 and to enable "pass through" processing
        //
        public void startDTD (String name, String publicId, String SystemId)
        throws SAXException
        {
            // need to filter out comments and PIs within the DTD
            inDTD = true;
        }

        // SAX2 lexical event
        public void endDTD ()
        throws SAXException
        {
            inDTD = false;
        }

        // SAX2 lexical event
        public void comment (char ch [], int start, int length)
        throws SAXException
        {
            Node        comment;

            // we can't create populated entity ref nodes using
            // only public DOM APIs (they've got to be readonly)
            if (consumer.isHidingComments ()
                    || inDTD
                    || currentEntity != null)
                return;
            comment = document.createComment (new String (ch, start, length));
            top.appendChild (comment);
        }

        /**
         * May be overridden by subclasses to return true, indicating
         * that entity reference nodes can be populated and then made
         * read-only.
         */
        public boolean canPopulateEntityRefs ()
            { return false; }

        // SAX2 lexical event
        public void startEntity (String name)
        throws SAXException
        {
            // are we ignoring what would be contents of an
            // entity ref, since we can't populate it?
            if (currentEntity != null)
                return;

            // Are we hiding all entity boundaries?
            if (consumer.isHidingReferences ())
                return;

            // SAX2 shows parameter entities; DOM hides them
            if (name.charAt (0) == '%' || "[dtd]".equals (name))
                return;

            // Since we can't create a populated entity ref node in any
            // standard way, we create an unpopulated one.
            EntityReference ref = document.createEntityReference (name);
            top.appendChild (ref);
            top = ref;

            // ... allowing subclasses to populate them
            if (!canPopulateEntityRefs ())
                currentEntity = name;
        }

        // SAX2 lexical event
        public void endEntity (String name)
        throws SAXException
        {
            if (name.charAt (0) == '%' || "[dtd]".equals (name))
                return;
            if (name.equals (currentEntity))
                currentEntity = null;
            if (!consumer.isHidingReferences ())
                top = top.getParentNode ();
        }


        // SAX1 DTD event
        public void notationDecl (
            String name,
            String publicId, String SystemId
        ) throws SAXException
        {
            /* IGNORE -- no public DOM API lets us store these
             * into the doctype node
             */
        }

        // SAX1 DTD event
        public void unparsedEntityDecl (
            String name,
            String publicId, String SystemId,
            String notationName
        ) throws SAXException
        {
            /* IGNORE -- no public DOM API lets us store these
             * into the doctype node
             */
        }

        // SAX2 declaration event
        public void elementDecl (String name, String model)
        throws SAXException
        {
            /* IGNORE -- no content model support in DOM L2 */
        }

        // SAX2 declaration event
        public void attributeDecl (
            String eName,
            String aName,
            String type,
            String mode,
            String value
        ) throws SAXException
        {
            /* IGNORE -- no attribute model support in DOM L2 */
        }

        // SAX2 declaration event
        public void internalEntityDecl (String name, String value)
        throws SAXException
        {
            /* IGNORE -- no public DOM API lets us store these
             * into the doctype node
             */
        }

        // SAX2 declaration event
        public void externalEntityDecl (
            String name,
            String publicId,
            String SystemId
        ) throws SAXException
        {
            /* IGNORE -- no public DOM API lets us store these
             * into the doctype node
             */
        }

        //
        // These really should offer the option of nonfatal handling,
        // like other validity errors, though that would cause major
        // chaos in the DOM data structures.  DOM is already spec'd
        // to treat many of these as fatal, so this is consistent.
        //
        private void namespaceError (String description)
        throws SAXParseException
        {
            SAXParseException err;

            err = new SAXParseException (description, locator);
            throw err;
        }
Tom Tromey committed
966 967
    }
}