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

📄 verifier.java

📁 一个用于搜索本地文件内容的小型搜索引擎
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
     */
    public static String checkNamespaceCollision(Namespace namespace,
                                                 Namespace other) {
        String p1,p2,u1,u2,reason;

        reason = null;
        p1 = namespace.getPrefix();
        u1 = namespace.getURI();
        p2 = other.getPrefix();
        u2 = other.getURI();
        if (p1.equals(p2) && !u1.equals(u2)) {
            reason = "The namespace prefix \"" + p1 + "\" collides";
        }
        return reason;
    }

    /**
     * Check if <code>{@link Attribute}</code>'s namespace collides with a 
     * <code>{@link Element}</code>'s namespace.
     *
     * @param attribute <code>Attribute</code> to check.
     * @param element <code>Element</code> to check against.
     * @return <code>String</code> reason for collision, or
     *         <code>null</code> if no collision.
     */
    public static String checkNamespaceCollision(Attribute attribute,
                                                 Element element) {
        Namespace namespace = attribute.getNamespace();
        String prefix = namespace.getPrefix();
        if ("".equals(prefix)) {
            return null;
        }

        return checkNamespaceCollision(namespace, element);
    }

    /**
     * Check if a <code>{@link Namespace}</code> collides with a
     * <code>{@link Element}</code>'s namespace.
     *
     * @param namespace <code>Namespace</code> to check.
     * @param element <code>Element</code> to check against.
     * @return <code>String</code> reason for collision, or
     *         <code>null</code> if no collision.
     */
    public static String checkNamespaceCollision(Namespace namespace,
                                                 Element element) {
        String reason = checkNamespaceCollision(namespace,
                                                element.getNamespace());
        if (reason != null) {
            return reason + " with the element namespace prefix";
        }

        reason = checkNamespaceCollision(namespace,
                                         element.getAdditionalNamespaces());
        if (reason != null) {
            return reason;
        }

        reason = checkNamespaceCollision(namespace, element.getAttributes());
        if (reason != null) {
            return reason;
        }

        return null;
    }

    /**
     * Check if a <code>{@link Namespace}</code> collides with a
     * <code>{@link Attribute}</code>'s namespace.
     *
     * @param namespace <code>Namespace</code> to check.
     * @param attribute <code>Attribute</code> to check against.
     * @return <code>String</code> reason for collision, or
     *         <code>null</code> if no collision.
     */
    public static String checkNamespaceCollision(Namespace namespace,
                                                 Attribute attribute) {
        String reason = checkNamespaceCollision(namespace,
                                                attribute.getNamespace());
        if (reason != null) {
            reason += " with an attribute namespace prefix on the element";
        }
        return reason;
    }

    /**
     * Check if a <code>{@link Namespace}</code> collides with any namespace
     * from a list of objects.
     *
     * @param namespace <code>Namespace</code> to check.
     * @param list <code>List</code> to check against.
     * @return <code>String</code> reason for collision, or
     *         <code>null</code> if no collision.
     */
    public static String checkNamespaceCollision(Namespace namespace,
                                                 List list) {
        if (list == null) {
            return null;
        }

        String reason = null;
        Iterator i = list.iterator();
        while ((reason == null) && i.hasNext()) {
            Object obj = i.next();
            if (obj instanceof Attribute) {
                reason = checkNamespaceCollision(namespace, (Attribute) obj);
            }
            else if (obj instanceof Element) {
                reason = checkNamespaceCollision(namespace, (Element) obj);
            }
            else if (obj instanceof Namespace) {
                reason = checkNamespaceCollision(namespace, (Namespace) obj);
                if (reason != null) {
                    reason += " with an additional namespace declared" +
                              " by the element";
                }
            }
        }
        return reason;
    }

    /**
     * This will check the supplied data to see if it is legal for use as
     * a JDOM <code>{@link ProcessingInstruction}</code> target.
     *
     * @param target <code>String</code> target to check.
     * @return <code>String</code> reason target is illegal, or
     *         <code>null</code> if target is OK.
     */
    public static String checkProcessingInstructionTarget(String target) {
        // Check basic XML name rules first
        String reason;
        if ((reason = checkXMLName(target)) != null) {
            return reason;
        }

        // No colons allowed, per Namespace Specification Section 6
        if (target.indexOf(":") != -1) {
            return "Processing instruction targets cannot contain colons";
        }

        // Cannot begin with 'xml' in any case
        if (target.equalsIgnoreCase("xml")) {
            return "Processing instructions cannot have a target of " +
                   "\"xml\" in any combination of case. (Note that the " +
                   "\"<?xml ... ?>\" declaration at the beginning of a " +
                   "document is not a processing instruction and should not " + 
                   "be added as one; it is written automatically during " +
                   "output, e.g. by XMLOutputter.)";
        }

        // If we got here, everything is OK
        return null;
    }

   /**
     * This will check the supplied data to see if it is legal for use as
     * <code>{@link ProcessingInstruction}</code> data. Besides checking that
     * all the characters are allowed in XML, this also checks
     * that the data does not contain the PI end-string "?&gt;".
     *
     * @param data <code>String</code> data to check.
     * @return <code>String</code> reason data is illegal, or
     *         <code>null</code> if data is OK.
     */
    public static String checkProcessingInstructionData(String data) {
        // Check basic XML name rules first
        String reason = checkCharacterData(data);

        if (reason == null) {
            if (data.indexOf("?>") >= 0) {
                return "Processing instructions cannot contain " +
                       "the string \"?>\"";
            }
        }

        return reason;
    }

    /**
     * This will check the supplied data to see if it is legal for use as
     * JDOM <code>{@link Comment}</code> data.
     *
     * @param data <code>String</code> data to check.
     * @return <code>String</code> reason data is illegal, or
     *         <code>null</code> if data is OK.
     */
    public static String checkCommentData(String data) {
        String reason = null;
        if ((reason = checkCharacterData(data)) != null) {
            return reason;
        }

        if (data.indexOf("--") != -1) {
            return "Comments cannot contain double hyphens (--)";
        }
        if (data.endsWith("-")) {
            return "Comment data cannot end with a hyphen.";
        }

        // If we got here, everything is OK
        return null;
    }

    // [13] PubidChar ::= #x20 | #xD | #xA | [a-zA-Z0-9] |
    // [-'()+,./:=?;*#@$_%]
    public static boolean isXMLPublicIDCharacter(char c) {

        if (c >= 'a' && c <= 'z') return true;
        if (c >= '?' && c <= 'Z') return true;
        if (c >= '\'' && c <= ';') return true;

        if (c == ' ') return true;
        if (c == '!') return true;
        if (c == '=') return true;
        if (c == '#') return true;
        if (c == '$') return true;
        if (c == '_') return true;
        if (c == '%') return true;
        if (c == '\n') return true;
        if (c == '\r') return true;
        if (c == '\t') return true;

        return false;
    }

    /**
     * This will ensure that the data for a public identifier
     * is legal.
     *
     * @param publicID <code>String</code> public ID to check.
     * @return <code>String</code> reason public ID is illegal, or
     *         <code>null</code> if public ID is OK.
     */
    public static String checkPublicID(String publicID) {
        String reason = null;

        if (publicID == null) return null;
        // This indicates there is no public ID

        for (int i = 0; i < publicID.length(); i++) {
          char c = publicID.charAt(i);
          if (!isXMLPublicIDCharacter(c)) {
            reason = c + " is not a legal character in public IDs";
            break;
          }
        }

        return reason;
    }


    /**
     * This will ensure that the data for a system literal
     * is legal.
     *
     * @param systemLiteral <code>String</code> system literal to check.
     * @return <code>String</code> reason system literal is illegal, or
     *         <code>null</code> if system literal is OK.
     */
    public static String checkSystemLiteral(String systemLiteral) {
        String reason = null;

        if (systemLiteral == null) return null;
        // This indicates there is no system ID

        if (systemLiteral.indexOf('\'') != -1
          && systemLiteral.indexOf('"') != -1) {
            reason =
             "System literals cannot simultaneously contain both single and double quotes.";
        }
        else {
          reason = checkCharacterData(systemLiteral);
        }

        return reason;
    }

    /**
     * This is a utility function for sharing the base process of checking
     * any XML name.
     *
     * @param name <code>String</code> to check for XML name compliance.
     * @return <code>String</code> reason the name is illegal, or
     *         <code>null</code> if OK.
     */
    public static String checkXMLName(String name) {
        // Cannot be empty or null
        if ((name == null) || (name.length() == 0) 
                           || (name.trim().equals(""))) {
            return "XML names cannot be null or empty";
        }

      
        // Cannot start with a number
        char first = name.charAt(0);
        if (!isXMLNameStartCharacter(first)) {
            return "XML names cannot begin with the character \"" + 
                   first + "\"";
        }
        // Ensure legal content for non-first chars
        for (int i=1, len = name.length(); i<len; i++) {
            char c = name.charAt(i);
            if (!isXMLNameCharacter(c)) {
                return "XML names cannot contain the character \"" + c + "\"";
            }
        }

        // We got here, so everything is OK

⌨️ 快捷键说明

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