⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 saxhandler.java

📁 一个用于搜索本地文件内容的小型搜索引擎
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
     *
     * @param name entity name
     * @param publicID public id
     * @param systemID system id
     * @throws SAXException when things go wrong
     */
    public void externalEntityDecl(String name,
                                   String publicID, String systemID)
                                   throws SAXException {
        // Store the public and system ids for the name
        externalEntities.put(name, new String[]{publicID, systemID});

        if (!inInternalSubset) return;

        internalSubset.append("  <!ENTITY ")
              .append(name);
        appendExternalId(publicID, systemID);
        internalSubset.append(">\n");
    }

    /**
     * This handles an attribute declaration in the internal subset.
     *
     * @param eName <code>String</code> element name of attribute
     * @param aName <code>String</code> attribute name
     * @param type <code>String</code> attribute type
     * @param valueDefault <code>String</code> default value of attribute
     * @param value <code>String</code> value of attribute
     * @throws SAXException
     */
    public void attributeDecl(String eName, String aName, String type,
                              String valueDefault, String value)
        throws SAXException {

        if (!inInternalSubset) return;

        internalSubset.append("  <!ATTLIST ")
              .append(eName)
              .append(' ')
              .append(aName)
              .append(' ')
              .append(type)
              .append(' ');
        if (valueDefault != null) {
              internalSubset.append(valueDefault);
        } else {
            internalSubset.append('\"')
                  .append(value)
                  .append('\"');
        }
        if ((valueDefault != null) && (valueDefault.equals("#FIXED"))) {
            internalSubset.append(" \"")
                  .append(value)
                  .append('\"');
        }
        internalSubset.append(">\n");
    }

    /**
     * Handle an element declaration in a DTD.
     *
     * @param name <code>String</code> name of element
     * @param model <code>String</code> model of the element in DTD syntax
     * @throws SAXException
     */
    public void elementDecl(String name, String model) throws SAXException {
        // Skip elements that come from the external subset
        if (!inInternalSubset) return;

        internalSubset.append("  <!ELEMENT ")
              .append(name)
              .append(' ')
              .append(model)
              .append(">\n");
    }

    /**
     * Handle an internal entity declaration in a DTD.
     *
     * @param name <code>String</code> name of entity
     * @param value <code>String</code> value of the entity
     * @throws SAXException
     */
    public void internalEntityDecl(String name, String value)
        throws SAXException {

        // Skip entities that come from the external subset
        if (!inInternalSubset) return;

        internalSubset.append("  <!ENTITY ");
        if (name.startsWith("%")) {
           internalSubset.append("% ").append(name.substring(1));
        } else {
           internalSubset.append(name);
        }
        internalSubset.append(" \"")
              .append(value)
              .append("\">\n");
    }

    /**
     * This will indicate that a processing instruction has been encountered.
     * (The XML declaration is not a processing instruction and will not
     * be reported.)
     *
     * @param target <code>String</code> target of PI
     * @param data <code>String</code> containing all data sent to the PI.
     *             This typically looks like one or more attribute value
     *             pairs.
     * @throws SAXException when things go wrong
     */
    public void processingInstruction(String target, String data)
        throws SAXException {

        if (suppress) return;

        flushCharacters();

        if (atRoot) {
            factory.addContent(document, factory.processingInstruction(target, data));
        } else {
            factory.addContent(getCurrentElement(),
                factory.processingInstruction(target, data));
        }
    }

    /**
     * This indicates that an unresolvable entity reference has been
     * encountered, normally because the external DTD subset has not been
     * read.
     *
     * @param name <code>String</code> name of entity
     * @throws SAXException when things go wrong
     */
    public void skippedEntity(String name)
        throws SAXException {

        // We don't handle parameter entity references.
        if (name.startsWith("%")) return;

        flushCharacters();

        factory.addContent(getCurrentElement(), factory.entityRef(name));
    }

    /**
     * This will add the prefix mapping to the JDOM
     * <code>Document</code> object.
     *
     * @param prefix <code>String</code> namespace prefix.
     * @param uri <code>String</code> namespace URI.
     */
    public void startPrefixMapping(String prefix, String uri)
        throws SAXException {

        if (suppress) return;

        Namespace ns = Namespace.getNamespace(prefix, uri);
        declaredNamespaces.add(ns);
    }

    /**
     * This reports the occurrence of an actual element.  It will include
     * the element's attributes, with the exception of XML vocabulary
     * specific attributes, such as
     * <code>xmlns:[namespace prefix]</code> and
     * <code>xsi:schemaLocation</code>.
     *
     * @param namespaceURI <code>String</code> namespace URI this element
     *                     is associated with, or an empty
     *                     <code>String</code>
     * @param localName <code>String</code> name of element (with no
     *                  namespace prefix, if one is present)
     * @param qName <code>String</code> XML 1.0 version of element name:
     *                [namespace prefix]:[localName]
     * @param atts <code>Attributes</code> list for this element
     * @throws SAXException when things go wrong
     */
    public void startElement(String namespaceURI, String localName,
                             String qName, Attributes atts)
                             throws SAXException {
        if (suppress) return;

        Element element = null;

        if ((namespaceURI != null) && (!namespaceURI.equals(""))) {
            String prefix = "";

            // Determine any prefix on the Element
            if (!qName.equals(localName)) {
                int split = qName.indexOf(":");
                prefix = qName.substring(0, split);
            }
            Namespace elementNamespace =
                Namespace.getNamespace(prefix, namespaceURI);
            element = factory.element(localName, elementNamespace);
        } else {
            element = factory.element(localName);
        }

        // Take leftover declared namespaces and add them to this element's
        // map of namespaces
        if (declaredNamespaces.size() > 0) {
            transferNamespaces(element);
        }

        // Handle attributes
        for (int i=0, len=atts.getLength(); i<len; i++) {
            Attribute attribute = null;

            String attLocalName = atts.getLocalName(i);
            String attQName = atts.getQName(i);
            int attType = getAttributeType(atts.getType(i));

            // Bypass any xmlns attributes which might appear, as we got
            // them already in startPrefixMapping().
            // This is sometimes necessary when SAXHandler is used with
            // another source than SAXBuilder, as with JDOMResult.
            if (attQName.startsWith("xmlns:") || attQName.equals("xmlns")) {
                continue;
            }

            // First clause per http://markmail.org/message/2p245ggcjst27xe6
            // patch from Mattias Jiderhamn
            if ("".equals(attLocalName) && attQName.indexOf(":") == -1) {
                attribute = factory.attribute(attQName, atts.getValue(i), attType);
            } else if (!attQName.equals(attLocalName)) {
                String attPrefix = attQName.substring(0, attQName.indexOf(":"));
                Namespace attNs = Namespace.getNamespace(attPrefix,
                                                         atts.getURI(i));

                attribute = factory.attribute(attLocalName, atts.getValue(i),
                                              attType, attNs);
            } else {
                attribute = factory.attribute(attLocalName, atts.getValue(i),
                                              attType);
            }
            factory.setAttribute(element, attribute);
        }

        flushCharacters();

        if (atRoot) {
            document.setRootElement(element);  // XXX should we use a factory call?
            atRoot = false;
        } else {
            factory.addContent(getCurrentElement(), element);
        }
        currentElement = element;
    }

    /**
     * This will take the supplied <code>{@link Element}</code> and
     * transfer its namespaces to the global namespace storage.
     *
     * @param element <code>Element</code> to read namespaces from.
     */
    private void transferNamespaces(Element element) {
        Iterator i = declaredNamespaces.iterator();
        while (i.hasNext()) {
            Namespace ns = (Namespace)i.next();
            if (ns != element.getNamespace()) {
                element.addNamespaceDeclaration(ns);
            }
        }
        declaredNamespaces.clear();
    }

    /**
     * This will report character data (within an element).
     *
     * @param ch <code>char[]</code> character array with character data
     * @param start <code>int</code> index in array where data starts.
     * @param length <code>int</code> length of data.
     * @throws SAXException
     */
    public void characters(char[] ch, int start, int length)
                    throws SAXException {

        if (suppress || (length == 0))
            return;

        if (previousCDATA != inCDATA) {
            flushCharacters();
        }

        textBuffer.append(ch, start, length);
    }

    /**
     * Capture ignorable whitespace as text.  If
     * setIgnoringElementContentWhitespace(true) has been called then this
     * method does nothing.
     *
     * @param ch <code>[]</code> - char array of ignorable whitespace
     * @param start <code>int</code> - starting position within array
     * @param length <code>int</code> - length of whitespace after start
     * @throws SAXException when things go wrong
     */
    public void ignorableWhitespace(char[] ch, int start, int length)
                                                     throws SAXException {
        if (!ignoringWhite) {
            characters(ch, start, length);
        }
    }

    /**
     * This will flush any characters from SAX character calls we've
     * been buffering.
     *
     * @throws SAXException when things go wrong
     */
    protected void flushCharacters() throws SAXException {
        if (ignoringBoundaryWhite) {
            if (!textBuffer.isAllWhitespace()) {
                flushCharacters(textBuffer.toString());
            }
        }
        else {
            flushCharacters(textBuffer.toString());
        }
        textBuffer.clear();
    }

    /**
     * Flush the given string into the document.  This is a protected method
     * so subclassers can control text handling without knowledge of the
     * internals of this class.
     *
     * @param data string to flush
     */
    protected void flushCharacters(String data) throws SAXException {
        if (data.length() == 0) {
            previousCDATA = inCDATA;
            return;
        }

/**
 * This is commented out because of some problems with
 * the inline DTDs that Xerces seems to have.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -