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

📄 dtdtest.java

📁 解决如何把XML应用到JAVA里问题
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     * criteria.
     * 
     * @param txt
     *            DOCUMENT ME!
     * @param expected
     *            DOCUMENT ME!
     * @param actual
     *            DOCUMENT ME!
     * 
     * @throws AssertionError
     *             DOCUMENT ME!
     */
    protected void assertSameDTDSubset(String txt, List expected, List actual) {
        /*
         * Nothing expected?
         */
        if (expected == null) {
            if (actual == null) {
                return; // Nothing found.
            } else {
                fail("Not expecting " + txt + " DTD subset.");
            }
        } else {
            /*
             * Something expected.
             */
            if (actual == null) {
                fail("Expecting " + txt + " DTD subset.");
            }

            /*
             * Correct #of declarations found?
             */
            assertEquals(txt + " DTD subset has correct #of declarations"
                    + ": expected=[" + expected.toString() + "]" + ", actual=["
                    + actual.toString() + "]", expected.size(), actual.size());

            /*
             * Check order, type, and values of each declaration. Serialization
             * tests are done separately.
             */
            Iterator itr1 = expected.iterator();

            Iterator itr2 = actual.iterator();

            while (itr1.hasNext()) {
                Object obj1 = itr1.next();

                Object obj2 = itr2.next();

                assertEquals(txt + " DTD subset: Same type of declaration",
                        obj1.getClass().getName(), obj2.getClass().getName());

                if (obj1 instanceof AttributeDecl) {
                    assertSameDecl((AttributeDecl) obj1, (AttributeDecl) obj2);
                } else if (obj1 instanceof ElementDecl) {
                    assertSameDecl((ElementDecl) obj1, (ElementDecl) obj2);
                } else if (obj1 instanceof InternalEntityDecl) {
                    assertSameDecl((InternalEntityDecl) obj1,
                            (InternalEntityDecl) obj2);
                } else if (obj1 instanceof ExternalEntityDecl) {
                    assertSameDecl((ExternalEntityDecl) obj1,
                            (ExternalEntityDecl) obj2);
                } else {
                    throw new AssertionError("Unexpected declaration type: "
                            + obj1.getClass());
                }
            }
        }
    }

    /**
     * Test helper method compares an expected and an actual {@link
     * AttributeDecl}.
     * 
     * @param expected
     *            DOCUMENT ME!
     * @param actual
     *            DOCUMENT ME!
     */
    public void assertSameDecl(AttributeDecl expected, AttributeDecl actual) {
        assertEquals("attributeName is correct", expected.getAttributeName(),
                actual.getAttributeName());

        assertEquals("elementName is correct", expected.getElementName(),
                actual.getElementName());

        assertEquals("type is correct", expected.getType(), actual.getType());

        assertEquals("value is not correct", expected.getValue(), actual
                .getValue());

        assertEquals("valueDefault is correct", expected.getValueDefault(),
                actual.getValueDefault());

        assertEquals("toString() is correct", expected.toString(), actual
                .toString());
    }

    /**
     * Test helper method compares an expected and an actual {@link
     * ElementDecl}.
     * 
     * @param expected
     *            DOCUMENT ME!
     * @param actual
     *            DOCUMENT ME!
     */
    protected void assertSameDecl(ElementDecl expected, ElementDecl actual) {
        assertEquals("name is correct", expected.getName(), actual.getName());

        assertEquals("model is not correct", expected.getModel(), actual
                .getModel());

        assertEquals("toString() is correct", expected.toString(), actual
                .toString());
    }

    /**
     * Test helper method compares an expected and an actual {@link
     * InternalEntityDecl}.
     * 
     * @param expected
     *            DOCUMENT ME!
     * @param actual
     *            DOCUMENT ME!
     */
    protected void assertSameDecl(InternalEntityDecl expected,
            InternalEntityDecl actual) {
        assertEquals("name is correct", expected.getName(), actual.getName());

        assertEquals("value is not correct", expected.getValue(), actual
                .getValue());

        assertEquals("toString() is correct", expected.toString(), actual
                .toString());
    }

    /**
     * Test helper method compares an expected and an actual {@link
     * ExternalEntityDecl}.
     * 
     * @param expected
     *            DOCUMENT ME!
     * @param actual
     *            DOCUMENT ME!
     */
    protected void assertSameDecl(ExternalEntityDecl expected,
            ExternalEntityDecl actual) {
        assertEquals("name is correct", expected.getName(), actual.getName());

        assertEquals("publicID is correct", expected.getPublicID(), actual
                .getPublicID());

        assertEquals("systemID is correct", expected.getSystemID(), actual
                .getSystemID());

        assertEquals("toString() is correct", expected.toString(), actual
                .toString());
    }

    /**
     * Helper method reads a local resource and parses it as an XML document.
     * The internal and external DTD subsets are optionally retained by the
     * parser and exposed via the {@link DocumentType}object on the returned
     * {@link Document}. The parser is configured with an {@link
     * EntityResolver}that knows how to find the local resource identified by
     * {@link #DTD_FILE}whose SYSTEM identifier is given by {@link
     * #DTD_SYSTEM_ID}.
     * 
     * @param resourceName
     *            DOCUMENT ME!
     * @param includeInternal
     *            DOCUMENT ME!
     * @param includeExternal
     *            DOCUMENT ME!
     * 
     * @return DOCUMENT ME!
     * 
     * @throws Exception
     *             DOCUMENT ME!
     */
    protected Document readDocument(String resourceName,
            boolean includeInternal, boolean includeExternal) throws Exception {
        SAXReader reader = new SAXReader();

        reader.setIncludeInternalDTDDeclarations(includeInternal);

        reader.setIncludeExternalDTDDeclarations(includeExternal);

        reader.setEntityResolver(new MyEntityResolver(DTD_FILE,
                DTD_PUBLICID, DTD_SYSTEM_ID));

        return getDocument(resourceName, reader);
    }

    /**
     * Provides a resolver for the local test DTD resource.
     */
    protected static class MyEntityResolver implements EntityResolver {
        private String resourceName;

        private String pubId;

        private String sysId;

        public MyEntityResolver(String localResourceName, String publicId,
                String systemId) {
            resourceName = localResourceName;

            sysId = systemId;
        }

        public InputSource resolveEntity(String publicId, String systemId)
                throws SAXException, IOException {
            if (pubId != null) {
                if (pubId.equals(publicId)) {
                    return new InputSource(getInputStream(resourceName));
                }
            }

            if (sysId.equals(systemId)) {
                return new InputSource(getInputStream(resourceName));
            } else {
                return null;
            }
        }

        /**
         * Returns an {@link InputStream}that will read from the indicated
         * local resource.
         * 
         * @param localResourceName
         *            DOCUMENT ME!
         * 
         * @return DOCUMENT ME!
         * 
         * @throws IOException
         *             DOCUMENT ME!
         */
        protected InputStream getInputStream(String localResourceName)
                throws IOException {
            InputStream is = new FileInputStream(localResourceName);

            return is;
        }
    }
}

/*
 * Redistribution and use of this software and associated documentation
 * ("Software"), with or without modification, are permitted provided that the
 * following conditions are met:
 * 
 * 1. Redistributions of source code must retain copyright statements and
 * notices. Redistributions must also contain a copy of this document.
 * 
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 * 
 * 3. The name "DOM4J" must not be used to endorse or promote products derived
 * from this Software without prior written permission of MetaStuff, Ltd. For
 * written permission, please contact dom4j-info@metastuff.com.
 * 
 * 4. Products derived from this Software may not be called "DOM4J" nor may
 * "DOM4J" appear in their names without prior written permission of MetaStuff,
 * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
 * 
 * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
 * 
 * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 * 
 * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
 */

⌨️ 快捷键说明

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