Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
252 views
in Technique[技术] by (71.8m points)

xml - How do I use square brackets in ATTLIST enumeration?

I have the following DTD and XML file. When I try to validate the file, I get the following error. The error is occurring on the square bracket in 'byte[]'. How can I include this 'byte[]' in my enumeration? I have tried escaping them with [$#93;, but I get the same error. I tried using quotes around the '"byte[]"' like so, but that didn't work either.

org.xml.sax.SAXParseExceptionpublicId: -//Genera//DTD XML EJB 1.0//EN; systemId: file:///C:/tdc-to-xml/ejb.v2.dtd; lineNumber: 11; columnNumber: 52; The enumerated type list must end with ')' in the "java-type" attribute declaration.

DTD

<!ELEMENT kds-ejb (entities, methods)>
<!ELEMENT entities (entity+)>
<!ELEMENT entity (field+)>
<!ATTLIST entity name CDATA #REQUIRED>
<!ATTLIST entity db-name CDATA #REQUIRED>
<!ATTLIST entity sequence-name CDATA #IMPLIED>
<!ATTLIST entity is-audited (TRUE|FALSE) "FALSE">
<!ELEMENT field (EMPTY)>
<!ATTLIST field name CDATA #REQUIRED>
<!ATTLIST field db-name CDATA #REQUIRED>
<!-- the following line is line 11 -->
<!ATTLIST field java-type (BigDecimal|Boolean|byte[]|Character|Date|Integer|Long|String|Timestamp) #REQUIRED>
<!ATTLIST field is-auto-generated (TRUE|FALSE) "FALSE">
<!ATTLIST field is-audited (TRUE|FALSE) "FALSE">
<!ATTLIST field is-primary-key (TRUE|FALSE) "FALSE">
<!ATTLIST field primary-key-sequence CDATA #IMPLIED>
<!ELEMENT methods (method+)>
<!ELEMENT method (sql, parameters, code?)>
<!ATTLIST method name CDATA #REQUIRED>
<!ATTLIST method entity-name CDATA #IMPLIED>
<!ATTLIST method returns-collection (TRUE|FALSE) "FALSE">
<!ATTLIST method special-return-type CDATA #IMPLIED>
<!ELEMENT sql (#PCDATA)>
<!ELEMENT parameters (parameter)>
<!ELEMENT parameter (EMPTY)>
<!ATTLIST parameter name CDATA #REQUIRED>
<!ATTLIST parameter java-type CDATA #REQUIRED>
<!ATTLIST parameter special-return-type CDATA #IMPLIED>
<!ELEMENT code (line+)>
<!ELEMENT line (text+)>
<!ATTLIST line sequence CDATA #REQUIRED>
<!ELEMENT text (#PCDATA)>

XML

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE kds-ejb PUBLIC "-//Genera//DTD XML EJB 1.0//EN" "ejb.v1.dtd">
<kds-ejb>
    <entities>
        <entity db-name="tblValidate" name="Validate">
            <field db-name="VALIDATE_ID" is-auto-generated="TRUE" is-primary-key="TRUE" java-type="Integer" name="id" primary-key-sequence="0"/>
            <field db-name="VALIDATE_TYPE" java-type="String" name="type"/>
            <field db-name="VALIDATE_DESCRIPTION" java-type="String" name="description"/>
            <field db-name="VALIDATE_ACTIVEYN" java-type="Boolean" name="activeYN"/>
            <field db-name="VALIDATE_OLDCODE" java-type="String" name="oldCode"/>
            <field db-name="VALIDATE_KEYCODE" java-type="String" name="keyCode"/>
        </entity>
    </entities>
    <methods>
        <method entity-name="Validate" name="getValidateByType" returns-collection="TRUE">
            <sql>SELECT * FROM TBLVALIDATE WHERE VALIDATE_TYPE = :type ORDER BY VALIDATE_DESCRIPTION</sql>
            <parameters>
                <parameter java-type="String" name="oldCode" sequence="0"/>
            </parameters>
        </method>
        <method entity-name="Validate" name="getValidateByOldCode" returns-collection="FALSE">
            <sql>SELECT * FROM TBLVALIDATE WHERE VALIDATE_OLDCODE = :oldCode</sql>
            <parameters>
                <parameter java-type="String" name="type" sequence="0"/>
            </parameters>
        </method>
    </methods>
</kds-ejb>

Code to create xml and validate.

public class TdcToXml {
private String fileName = null;

public TdcToXml(String fileName) {
    this.fileName = fileName;
}

private void process() {
    try {

        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

        Document doc = docBuilder.newDocument();
        Element rootElement = doc.createElement("kds-ejb");
        doc.appendChild(rootElement);

        Element entities = doc.createElement("entities");
        rootElement.appendChild(entities);

        Element entity = doc.createElement("entity");
        entity.setAttribute("name", "Validate");
        entity.setAttribute("db-name", "tblValidate");
        entities.appendChild(entity);

        String[][] fields = { { "id", "VALIDATE_ID", "Integer", "TRUE", null, "TRUE", "0" },
                { "type", "VALIDATE_TYPE", "String", null, null, null, null },
                { "description", "VALIDATE_DESCRIPTION", "String", null, null, null, null },
                { "activeYN", "VALIDATE_ACTIVEYN", "Boolean", null, null, null, null },
                { "oldCode", "VALIDATE_OLDCODE", "String", null, null, null, null },
                { "keyCode", "VALIDATE_KEYCODE", "String", null, null, null, null } };
        for (String[] singleField : fields) {
            Element field = doc.createElement("field");
            field.setAttribute("name", singleField[0]);
            field.setAttribute("db-name", singleField[1]);
            field.setAttribute("java-type", singleField[2]);
            if (singleField[3] != null)
                field.setAttribute("is-auto-generated", singleField[3]);
            if (singleField[4] != null)
                field.setAttribute("is-audited", singleField[4]);
            if (singleField[5] != null)
                field.setAttribute("is-primary-key", singleField[5]);
            if (singleField[6] != null)
                field.setAttribute("primary-key-sequence", singleField[6]);
            entity.appendChild(field);
        }

        Element methods = doc.createElement("methods");
        rootElement.appendChild(methods);

        Element method = doc.createElement("method");
        methods.appendChild(method);
        method.setAttribute("name", "getValidateByType");
        method.setAttribute("entity-name", "Validate");
        method.setAttribute("returns-collection", "TRUE");
        methods.appendChild(method);

        Element sql = doc.createElement("sql");
        sql.appendChild(doc.createTextNode(
                "SELECT * FROM TBLVALIDATE WHERE VALIDATE_TYPE = :type ORDER BY VALIDATE_DESCRIPTION"));
        method.appendChild(sql);

        Element parameters = doc.createElement("parameters");
        method.appendChild(parameters);

        Element parameter = doc.createElement("parameter");
        parameter.setAttribute("name", "oldCode");
        parameter.setAttribute("java-type", "String");
        parameter.setAttribute("sequence", "0");
        parameters.appendChild(parameter);

        method = doc.createElement("method");
        methods.appendChild(method);
        method.setAttribute("name", "getValidateByOldCode");
        method.setAttribute("entity-name", "Validate");
        method.setAttribute("returns-collection", "FALSE");
        methods.appendChild(method);

        sql = doc.createElement("sql");
        sql.appendChild(doc.createTextNode("SELECT * FROM TBLVALIDATE WHERE VALIDATE_OLDCODE = :oldCode"));
        method.appendChild(sql);

        parameters = doc.createElement("parameters");
        method.appendChild(parameters);

        parameter = doc.createElement("parameter");
        parameter.setAttribute("name", "type");
        parameter.setAttribute("java-type", "String");
        parameter.setAttribute("sequence", "0");
        parameters.appendChild(parameter);

        // write the content into xml file
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(new File(fileName));

        DOMImplementation domImpl = doc.getImplementation();
        DocumentType doctype = domImpl.createDocumentType("doctype", "-//Genera//DTD XML EJB 1.0//EN",
                "ejb.v1.dtd");
        transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, doctype.getPublicId());
        transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, doctype.getSystemId());
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
        transformer.transform(source, result);

        System.out.println("File saved!");

    } catch (ParserConfigurationException pce) {
        pce.printStackTrace();
    } catch (TransformerException tfe) {
        tfe.printStackTrace();
    }
}

private void validate() {
    try {
        DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
        domFactory.setValidating(true);
        DocumentBuilder builder = domFactory.newDocumentBuilder();
        builder.setErrorHandler(new ErrorHandler() {
            @Override
            public void error(SAXParseException exception) throws SAXException {
                // do something more useful in each of these handlers
                exception.printStackTrace();
            }

            @Override
            public void fatalError(SAXParseException exception) throws SAXException {
                exception.printStackTrace();
            }

            @Override
            public void warning(SAXParseException exception) throws SAXException {
                exception.printStackTrace();
            }
        });
        builder.parse(fileName);
    } catch (ParserConfigurationException pce) {
        pce.printStackTrace();
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public static void main(String[] args) {
    TdcToXml tdcToXml = new TdcToXml("ejb.xml");
    tdcToXml.process();
    tdcToXml.validate();
}

}
question from:https://stackoverflow.com/questions/65927211/how-do-i-use-square-brackets-in-attlist-enumeration

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can't use square brackets in an enumeration; you can only use nmtokens (name tokens) which are comprised of name characters. See here: https://www.w3.org/TR/REC-xml/#d0e3200


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...