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

📄 scxmlserializer.java

📁 java xml bean把xml解析成bean
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        if (t.getTarget() != null) {
            b.append(indent).append("<target>");
            // The inline transition target can only be a state
            serializeState(b, (State) t.getTarget(), indent + INDENT);
            b.append(indent).append("</target>");
        }
    }

    /**
     * Serialize this Datamodel object.
     *
     * @param b The buffer to append the serialization to
     * @param dm The Datamodel to be serialized
     * @param indent The indent for this XML element
     */
    public static void serializeDatamodel(final StringBuffer b,
            final Datamodel dm, final String indent) {
        List data = dm.getData();
        if (data != null && data.size() > 0) {
            b.append(indent).append("<datamodel>\n");
            if (XFORMER == null) {
                b.append(indent).append(INDENT).
                    append("<!-- Body content was not serialized -->\n");
                b.append(indent).append("</datamodel>\n");
                return;
            }
            for (Iterator iter = data.iterator(); iter.hasNext();) {
                Data datum = (Data) iter.next();
                Node dataNode = datum.getNode();
                if (dataNode != null) {
                    StringWriter out = new StringWriter();
                    try {
                        Source input = new DOMSource(dataNode);
                        Result output = new StreamResult(out);
                        XFORMER.transform(input, output);
                    } catch (TransformerException te) {
                        org.apache.commons.logging.Log log = LogFactory.
                            getLog(SCXMLSerializer.class);
                        log.error(te.getMessage(), te);
                        b.append(indent).append(INDENT).
                            append("<!-- Data content not serialized -->\n");
                    }
                    b.append(indent).append(INDENT).append(out.toString());
                } else {
                    b.append(indent).append(INDENT).append("<data id=\"").
                        append(datum.getId()).append("\" expr=\"").
                        append(SCXMLHelper.escapeXML(datum.getExpr())).
                        append("\" />\n");
                }
            }
            b.append(indent).append("</datamodel>\n");
        }
    }

    /**
     * Serialize this OnEntry object.
     *
     * @param b The buffer to append the serialization to
     * @param t The TransitionTarget whose OnEntry is to be serialized
     * @param indent The indent for this XML element
     */
    public static void serializeOnEntry(final StringBuffer b,
            final TransitionTarget t, final String indent) {
        OnEntry e = t.getOnEntry();
        if (e != null && e.getActions().size() > 0) {
            b.append(indent).append("<onentry>\n");
            serializeActions(b, e.getActions(), indent + INDENT);
            b.append(indent).append("</onentry>\n");
        }
    }

    /**
     * Serialize this OnExit object.
     *
     * @param b The buffer to append the serialization to
     * @param t The TransitionTarget whose OnExit is to be serialized
     * @param indent The indent for this XML element
     */
    public static void serializeOnExit(final StringBuffer b,
            final TransitionTarget t, final String indent) {
        OnExit x = t.getOnExit();
        if (x != null && x.getActions().size() > 0) {
            b.append(indent).append("<onexit>\n");
            serializeActions(b, x.getActions(), indent + INDENT);
            b.append(indent).append("</onexit>\n");
        }
    }

    /**
     * Serialize this List of actions.
     *
     * @param b The buffer to append the serialization to
     * @param l The List of actions to serialize
     * @param indent The indent for this XML element
     * @return boolean true if the list of actions contains an &lt;exit/&gt;
     */
    public static boolean serializeActions(final StringBuffer b, final List l,
            final String indent) {
        if (l == null) {
            return false;
        }
        boolean exit = false;
        Iterator i = l.iterator();
        while (i.hasNext()) {
            Action a = (Action) i.next();
            if (a instanceof Var) {
                Var v = (Var) a;
                b.append(indent).append("<cs:var name=\"").append(v.getName())
                    .append("\" expr=\"")
                    .append(SCXMLHelper.escapeXML(v.getExpr()))
                    .append("\"/>\n");
            } else if (a instanceof Assign) {
                Assign asn = (Assign) a;
                b.append(indent).append("<assign");
                if (!SCXMLHelper.isStringEmpty(asn.getLocation())) {
                    b.append(" location=\"").append(asn.getLocation());
                    if (!SCXMLHelper.isStringEmpty(asn.getSrc())) {
                        b.append("\" src=\"").append(asn.getSrc());
                    } else {
                        b.append("\" expr=\"").
                            append(SCXMLHelper.escapeXML(asn.getExpr()));
                    }
                } else {
                    b.append(" name=\"").append(asn.getName()).
                        append("\" expr=\"").
                        append(SCXMLHelper.escapeXML(asn.getExpr()));
                }
                b.append("\"/>\n");
            } else if (a instanceof Send) {
                serializeSend(b, (Send) a, indent);
            } else if (a instanceof Cancel) {
                Cancel c = (Cancel) a;
                b.append(indent).append("<cancel sendid=\"")
                    .append(c.getSendid()).append("\"/>\n");
            } else if (a instanceof Log) {
                Log lg = (Log) a;
                b.append(indent).append("<log expr=\"").
                    append(SCXMLHelper.escapeXML(lg.getExpr())).
                    append("\"/>\n");
            } else if (a instanceof Exit) {
                Exit e = (Exit) a;
                b.append(indent).append("<cs:exit");
                String expr = SCXMLHelper.escapeXML(e.getExpr());
                String nl = e.getNamelist();
                if (expr != null) {
                    b.append(" expr=\"" + expr + "\"");
                }
                if (nl != null) {
                    b.append(" namelist=\"" + nl + "\"");
                }
                b.append("/>\n");
                exit = true;
            } else if (a instanceof If) {
                If iff = (If) a;
                serializeIf(b, iff, indent);
            } else if (a instanceof Else) {
                b.append(indent).append("<else/>\n");
            } else if (a instanceof ElseIf) {
                ElseIf eif = (ElseIf) a;
                b.append(indent).append("<elseif cond=\"")
                    .append(SCXMLHelper.escapeXML(eif.getCond()))
                    .append("\" />\n");
            }
        }
        return exit;
    }

    /**
     * Serialize this Send object.
     *
     * @param b The buffer to append the serialization to
     * @param send The Send object to serialize
     * @param indent The indent for this XML element
     */
    public static void serializeSend(final StringBuffer b,
            final Send send, final String indent) {
        b.append(indent).append("<send");
        if (send.getSendid() != null) {
            b.append(" sendid=\"").append(send.getSendid()).append("\"");
        }
        if (send.getTarget() != null) {
            b.append(" target=\"").append(send.getTarget()).append("\"");
        }
        if (send.getTargettype() != null) {
            b.append(" targetType=\"").append(send.getTargettype()).append("\"");
        }
        if (send.getNamelist() != null) {
            b.append(" namelist=\"").append(send.getNamelist()).append("\"");
        }
        if (send.getDelay() != null) {
            b.append(" delay=\"").append(send.getDelay()).append("\"");
        }
        if (send.getEvent() != null) {
            b.append(" event=\"").append(send.getEvent()).append("\"");
        }
        if (send.getHints() != null) {
            b.append(" hints=\"").append(send.getHints()).append("\"");
        }
        b.append(">\n");
        b.append(getBodyContent(send));
        b.append(indent).append("</send>\n");
    }

    /**
     * Return serialized body of <code>ExternalContent</code>.
     *
     * @param externalContent The model element containing the body content
     * @return String The serialized body content
     */
    public static final String getBodyContent(
            final ExternalContent externalContent) {
        StringBuffer buf = new StringBuffer();
        List externalNodes = externalContent.getExternalNodes();
        if (externalNodes.size() > 0 && XFORMER == null) {
            buf.append("<!-- Body content was not serialized -->\n");
            return buf.toString();
        }
        for (int i = 0; i < externalNodes.size(); i++) {
            Source input = new DOMSource((Node) externalNodes.get(i));
            StringWriter out = new StringWriter();
            Result output = new StreamResult(out);
            try {
                XFORMER.transform(input, output);
            } catch (TransformerException te) {
                org.apache.commons.logging.Log log = LogFactory.
                    getLog(SCXMLSerializer.class);
                log.error(te.getMessage(), te);
                buf.append("<!-- Not all body content was serialized -->");
            }
            buf.append(out.toString()).append("\n");
        }
        return buf.toString();
    }

    /**
     * Serialize this If object.
     *
     * @param b The buffer to append the serialization to
     * @param iff The If object to serialize
     * @param indent The indent for this XML element
     */
    public static void serializeIf(final StringBuffer b,
            final If iff, final String indent) {
        b.append(indent).append("<if cond=\"").append(SCXMLHelper.
            escapeXML(iff.getCond())).append("\">\n");
        serializeActions(b, iff.getActions(), indent + INDENT);
        b.append(indent).append("</if>\n");
    }

    /**
     * Serialize properties of TransitionTarget which are element attributes.
     *
     * @param b The buffer to append the serialization to
     * @param t The TransitionTarget
     */
    private static void serializeTransitionTargetAttributes(
            final StringBuffer b, final TransitionTarget t) {
        String id = t.getId();
        if (id != null) {
            b.append(" id=\"").append(id).append("\"");
        }
    }

    /**
     * Serialize namespace declarations for the root SCXML element.
     *
     * @param holder The {@link NamespacePrefixesHolder} object
     * @return The serialized namespace declarations
     */
    private static String serializeNamespaceDeclarations(
            final NamespacePrefixesHolder holder) {
        Map ns = holder.getNamespaces();
        StringBuffer b = new StringBuffer();
        if (ns != null) {
            Iterator iter = ns.entrySet().iterator();
            while (iter.hasNext()) {
                Map.Entry entry = (Map.Entry) iter.next();
                String prefix = (String) entry.getKey();
                String nsURI = (String) entry.getValue();
                if (prefix.length() == 0 && !nsURI.equals(NAMESPACE_SCXML)) {
                    org.apache.commons.logging.Log log = LogFactory.
                        getLog(SCXMLSerializer.class);
                    log.warn("When using the SCXMLSerializer, the default "
                        + "namespace must be the SCXML namespace:"
                        + NAMESPACE_SCXML);
                } if (prefix.equals("cs")
                        && !nsURI.equals(NAMESPACE_COMMONS_SCXML)) {
                    org.apache.commons.logging.Log log = LogFactory.
                        getLog(SCXMLSerializer.class);
                    log.warn("When using the SCXMLSerializer, the namespace"
                        + "prefix \"cs\" must bind to the Commons SCXML "
                        + "namespace:" + NAMESPACE_COMMONS_SCXML);
                } else if (prefix.length() > 0) {
                    b.append(" xmlns:").append(prefix).append("=\"").
                        append(nsURI).append("\"");
                }
            }
        }
        return b.toString();
    }

    /**
     * Get a <code>Transformer</code> instance.
     *
     * @return Transformer The <code>Transformer</code> instance.
     */
    private static Transformer getTransformer() {
        Transformer transformer = null;
        Properties outputProps = new Properties();
        outputProps.put(OutputKeys.OMIT_XML_DECLARATION, "yes");
        outputProps.put(OutputKeys.STANDALONE, "no");
        outputProps.put(OutputKeys.INDENT, "yes");
        try {
            TransformerFactory tfFactory = TransformerFactory.newInstance();
            transformer = tfFactory.newTransformer();
            transformer.setOutputProperties(outputProps);
        } catch (Throwable t) {
            return null;
        }
        return transformer;
    }

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

}

⌨️ 快捷键说明

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