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

📄 scxmlhelper.java

📁 java xml bean把xml解析成bean
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                }
            }
        }
        return allStates;
    }

    /**
     * According to the UML definition, two transitions
     * are conflicting if the sets of states they exit overlap.
     *
     * @param t1 a transition to check against t2
     * @param t2 a transition to check against t1
     * @param currentStates the set of current states (simple states only)
     * @return true if the t1 and t2 are conflicting transitions
     * @see #getStatesExited(Transition, Set)
     */
    public static boolean inConflict(final Transition t1,
            final Transition t2, final Set currentStates) {
        Set ts1 = getStatesExited(t1, currentStates);
        Set ts2 = getStatesExited(t2, currentStates);
        ts1.retainAll(ts2);
        if (ts1.isEmpty()) {
            return false;
        }
        return true;
    }

    /**
     * Whether the first argument is a subtype of the second.
     *
     * @param child The candidate subtype
     * @param parent The supertype
     * @return true if child is subtype of parent, otherwise false
     */
    public static boolean subtypeOf(final Class child, final Class parent) {
        if (child == null || parent == null) {
            return false;
        }
        for (Class current = child; current != Object.class;
                current = current.getSuperclass()) {
            if (current == parent) {
                return true;
            }
        }
        return false;
    }

    /**
     * Whether the class implements the interface.
     *
     * @param clas The candidate class
     * @param interfayce The interface
     * @return true if clas implements interfayce, otherwise false
     */
    public static boolean implementationOf(final Class clas,
            final Class interfayce) {
        if (clas == null || interfayce == null || !interfayce.isInterface()) {
            return false;
        }
        for (Class current = clas; current != Object.class;
                current = current.getSuperclass()) {
            Class[] implementedInterfaces = current.getInterfaces();
            for (int i = 0; i < implementedInterfaces.length; i++) {
                if (implementedInterfaces[i] == interfayce) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * Set node value, depending on its type, from a String.
     *
     * @param node A Node whose value is to be set
     * @param value The new value
     */
    public static void setNodeValue(final Node node, final String value) {
        switch(node.getNodeType()) {
            case Node.ATTRIBUTE_NODE:
                node.setNodeValue(value);
                break;
            case Node.ELEMENT_NODE:
                //remove all text children
                if (node.hasChildNodes()) {
                    Node child = node.getFirstChild();
                    while (child != null) {
                        if (child.getNodeType() == Node.TEXT_NODE) {
                            node.removeChild(child);
                        }
                        child = child.getNextSibling();
                    }
                }
                //create a new text node and append
                Text txt = node.getOwnerDocument().createTextNode(value);
                node.appendChild(txt);
                break;
            case Node.TEXT_NODE:
            case Node.CDATA_SECTION_NODE:
                ((CharacterData) node).setData(value);
                break;
            default:
                String err = "Trying to set value of a strange Node type: "
                    + node.getNodeType();
                //Logger.logln(Logger.E, err);
                throw new IllegalArgumentException(err);
        }
    }

    /**
     * Retrieve a DOM node value as a string depending on its type.
     *
     * @param node A node to be retreived
     * @return The value as a string
     */
    public static String getNodeValue(final Node node) {
        String result = "";
        if (node == null) {
            return result;
        }
        switch(node.getNodeType()) {
            case Node.ATTRIBUTE_NODE:
                result = node.getNodeValue();
                break;
            case Node.ELEMENT_NODE:
                if (node.hasChildNodes()) {
                    Node child = node.getFirstChild();
                    StringBuffer buf = new StringBuffer();
                    while (child != null) {
                        if (child.getNodeType() == Node.TEXT_NODE) {
                            buf.append(((CharacterData) child).getData());
                        }
                        child = child.getNextSibling();
                    }
                    result = buf.toString();
                }
                break;
            case Node.TEXT_NODE:
            case Node.CDATA_SECTION_NODE:
                result = ((CharacterData) node).getData();
                break;
            default:
                String err = "Trying to get value of a strange Node type: "
                    + node.getNodeType();
                //Logger.logln(Logger.W, err );
                throw new IllegalArgumentException(err);
        }
        return result.trim();
    }

    /**
     * Clone data model.
     *
     * @param ctx The context to clone to.
     * @param datamodel The datamodel to clone.
     * @param evaluator The expression evaluator.
     * @param log The error log.
     */
    public static void cloneDatamodel(final Datamodel datamodel,
            final Context ctx, final Evaluator evaluator,
            final Log log) {
        if (datamodel == null) {
            return;
        }
        List data = datamodel.getData();
        if (data == null) {
            return;
        }
        for (Iterator iter = data.iterator(); iter.hasNext();) {
            Data datum = (Data) iter.next();
            Node datumNode = datum.getNode();
            Node valueNode = null;
            if (datumNode != null) {
                valueNode = datumNode.cloneNode(true);
            }
            // prefer "src" over "expr" over "inline"
            if (!SCXMLHelper.isStringEmpty(datum.getSrc())) {
                ctx.setLocal(datum.getId(), valueNode);
            } else if (!SCXMLHelper.isStringEmpty(datum.
                    getExpr())) {
                Object value = null;
                try {
                    ctx.setLocal(NAMESPACES_KEY, datum.getNamespaces());
                    value = evaluator.eval(ctx, datum.getExpr());
                    ctx.setLocal(NAMESPACES_KEY, null);
                } catch (SCXMLExpressionException see) {
                    if (log != null) {
                        log.error(see.getMessage(), see);
                    } else {
                        Log defaultLog = LogFactory.getLog(SCXMLHelper.class);
                        defaultLog.error(see.getMessage(), see);
                    }
                }
                ctx.setLocal(datum.getId(), value);
            } else {
                ctx.setLocal(datum.getId(), valueNode);
            }
        }
    }

    /**
     * Escape XML strings for serialization.
     * The basic algorithm is taken from Commons Lang (see oacl.Entities.java)
     *
     * @param str A string to be escaped
     * @return The escaped string
     */
    public static String escapeXML(final String str) {
        if (str == null) {
            return null;
        }

        // Make the writer an arbitrary bit larger than the source string
        int len = str.length();
        StringWriter stringWriter = new StringWriter(len + 8);

        for (int i = 0; i < len; i++) {
            char c = str.charAt(i);
            String entityName = null; // Look for XML 1.0 predefined entities
            switch (c) {
                case '"':
                    entityName = "quot";
                    break;
                case '&':
                    entityName = "amp";
                    break;
                case '\'':
                    entityName = "apos";
                    break;
                case '<':
                    entityName = "lt";
                    break;
                case '>':
                    entityName = "gt";
                    break;
                default:
            }
            if (entityName == null) {
                if (c > 0x7F) {
                    stringWriter.write("&#");
                    stringWriter.write(Integer.toString(c));
                    stringWriter.write(';');
                } else {
                    stringWriter.write(c);
                }
            } else {
                stringWriter.write('&');
                stringWriter.write(entityName);
                stringWriter.write(';');
            }
        }

        return stringWriter.toString();
    }

    /**
     * Discourage instantiation since this is a utility class.
     */
    private SCXMLHelper() {
        super();
    }

    /**
     * Current document namespaces are saved under this key in the parent
     * state's context.
     */
    private static final String NAMESPACES_KEY = "_ALL_NAMESPACES";

}

⌨️ 快捷键说明

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