【问题标题】:How create DatabaseChangeLog from xom XML document?如何从 xom XML 文档创建 DatabaseChangeLog?
【发布时间】:2015-03-10 22:37:16
【问题描述】:

在进行了一些程序内操作之后,我最终得到了一个 xom Document 对象,其中包含我要执行的 ChangeLog。据我了解,我必须在 ChangeLogParser 的帮助下将其转换为 liquibase XML 格式 ParsedNode。但是该接口在 parse 方法中假定了一个外部表示。使用 ResourceAccessor 将 Document 对象注入解析器也是不可能的,因为 getResourcesAsStream 方法返回一组 InputStream。 因此,我能想到的使用 liquibase 基础架构的唯一方法是将 Document 打印到 String 并通过 ByteArrayInputStream 将其反馈回来。 还是我需要编写一个独立的 Document -> ParsedNode 转换器?

【问题讨论】:

    标签: liquibase


    【解决方案1】:

    事实证明这是一个明智的选择。在快速浏览了 SAX 处理程序之后,这很有效:

    import liquibase.changelog.DatabaseChangeLog;
    import liquibase.exception.SetupException;
    import liquibase.parser.core.ParsedNode;
    import liquibase.parser.core.ParsedNodeException;
    import liquibase.resource.ClassLoaderResourceAccessor;
    import nu.xom.Document;
    import nu.xom.Element;
    import nu.xom.Elements;
    import org.apache.commons.lang3.StringUtils;
    
    /**
     * Convert a xom Document containing a liquibase changelog
     * to a DatabaseChangeLog.
     */
    public class XOMDocumentToDatabaseChangeLogConverter {
        /**
         * Convert the Document ot a DatabaseChangeLog.
         * @param changeLogXML to convert.
         * @return contained changelog.
         * @throws SetupException on error.
         * @throws ParsedNodeException on error.
         */
        public static DatabaseChangeLog convert(Document changeLogXML) throws SetupException, ParsedNodeException {
            DatabaseChangeLog result = new DatabaseChangeLog();
            result.setPhysicalFilePath("");
            Element rootElement = changeLogXML.getRootElement();
            result.load(convert(rootElement), new ClassLoaderResourceAccessor());
            return result;
        }
    
        /**
         * Internal recursive method doing the node rollup.
         * @param element to convert.
         * @return converted node.
         * @throws ParsedNodeException on error.
         */
        protected static ParsedNode convert(Element element) throws ParsedNodeException {
            ParsedNode node = new ParsedNode(null, element.getLocalName());
            for (int i = 0; i < element.getAttributeCount(); ++i) {
                node.addChild(null, element.getAttribute(i).getLocalName(), element.getAttribute(i).getValue());
            }
            String seenText = element.getValue();
            if (!StringUtils.trimToEmpty(seenText).equals("")) {
                node.setValue(seenText.trim());
            }
    
            Elements children = element.getChildElements();
            for (int i = 0; i < children.size(); ++i) {
                node.addChild(convert(children.get(i)));
            }
    
            return node;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-11-09
      • 1970-01-01
      • 1970-01-01
      • 2019-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多