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

📄 modelwriter.java

📁 该源代码为一些通用的程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            writeOpenComment(writer, comments);
            if (comments.getCloseTagComment() != null) {
                this.writerSupport.writeTag(writer, tagName, attributes, XMLWriterSupport.OPEN);
                writeCloseComment(writer, comments);
                this.writerSupport.writeCloseTag(writer, tagName);
            }
            else {
                this.writerSupport.writeTag(writer, tagName, attributes, XMLWriterSupport.CLOSE);
            }
        }
    }

    /**
     * Writes a closed (short) tag with eventually nested comments.
     *
     * @param writer  the writer.
     * @param tagName  the tag name.
     * @param attribute  the attribute name.
     * @param value  the attribute value.
     * @param comments  the comments.
     * 
     * @throws IOException if there is an I/O problem.
     */
    protected void writeTag(final Writer writer, 
                            final String tagName,
                            final String attribute, 
                            final String value,
                            final Comments comments) throws IOException {
        if (comments == null) {
            this.writerSupport.writeTag(writer, tagName, attribute, value , XMLWriterSupport.CLOSE);
        }
        else {
            writeOpenComment(writer, comments);
            if (comments.getCloseTagComment() != null) {
                this.writerSupport.writeTag(
                    writer, tagName, attribute, value, XMLWriterSupport.OPEN
                );
                writeCloseComment(writer, comments);
                this.writerSupport.writeCloseTag(writer, tagName);
            }
            else {
                this.writerSupport.writeTag(
                    writer, tagName, attribute, value, XMLWriterSupport.CLOSE
                );
            }
        }
    }

    /**
     * Writes a model to the specified writer.
     * 
     * @param writer  the writer.
     * 
     * @throws IOException if there is an I/O problem.
     */
    public void write(final Writer writer) throws IOException {
        
        writeStandardComment(writer, this.model.getModelComments());
        this.writerSupport.writeTag(writer, ClassModelTags.OBJECTS_TAG);
        final String[] sources = this.model.getSources();
        for (int i = 0; i < sources.length; i++) {
            final Comments comments = this.model.getIncludeComment(sources[i]);
            writeTag(
                writer, 
                ClassModelTags.INCLUDE_TAG, ClassModelTags.SOURCE_ATTR, sources[i], comments
            );
        }

        for (int i = 0; i < this.model.size(); i++) {
            final ClassDescription cd = this.model.get(i);
            writeClassDescription(writer, cd);
        }

        final ManualMappingInfo[] mappings = getModel().getMappingModel().getManualMapping();
        for (int i = 0; i < mappings.length; i++) {
            final ManualMappingInfo mi = mappings[i];
            writeManualMapping(writer, mi);
        }

        final MultiplexMappingInfo[] mmappings = getModel().getMappingModel().getMultiplexMapping();
        for (int i = 0; i < mmappings.length; i++) {
            final MultiplexMappingInfo mi = mmappings[i];
            writeMultiplexMapping(writer, mi);
        }

        writeCloseComment(writer, this.model.getModelComments());
        this.writerSupport.writeCloseTag(writer, ClassModelTags.OBJECTS_TAG);
        
    }

    /**
     * Writes a manual mapping to the XML output.
     * 
     * @param writer  the writer.
     * @param mi  the mapping info.
     * 
     * @throws IOException if there is an I/O problem.
     */
    protected void writeManualMapping(final Writer writer, final ManualMappingInfo mi) throws IOException {
        final AttributeList al = new AttributeList();
        al.setAttribute(ClassModelTags.CLASS_ATTR, mi.getBaseClass().getName());
        al.setAttribute(ClassModelTags.READ_HANDLER_ATTR, mi.getReadHandler().getName());
        al.setAttribute(ClassModelTags.WRITE_HANDLER_ATTR, mi.getWriteHandler().getName());
        writeTag(writer, ClassModelTags.MANUAL_TAG, al, mi.getComments());
    }

    /**
     * Writes a multiplex mapping to the XML output.
     * 
     * @param writer  the writer.
     * @param mi  the mapping info.
     * 
     * @throws IOException if there is an I/O problem.
     */
    protected void writeMultiplexMapping(final Writer writer, final MultiplexMappingInfo mi)
        throws IOException {
        
        final TypeInfo[] tis = mi.getChildClasses();

        final AttributeList al = new AttributeList();
        al.setAttribute(ClassModelTags.BASE_CLASS_ATTR, mi.getBaseClass().getName());
        al.setAttribute(ClassModelTags.TYPE_ATTR, mi.getTypeAttribute());
        getWriterSupport().writeTag(writer, ClassModelTags.MAPPING_TAG, al, XMLWriterSupport.OPEN);

        for (int j = 0; j < tis.length; j++) {
            final AttributeList tiAttr = new AttributeList();
            tiAttr.setAttribute(ClassModelTags.NAME_ATTR, tis[j].getName());
            tiAttr.setAttribute(ClassModelTags.CLASS_ATTR, tis[j].getType().getName());
            writeTag(writer, ClassModelTags.TYPE_TAG, tiAttr, tis[j].getComments());
        }

        getWriterSupport().writeCloseTag(writer, ClassModelTags.MAPPING_TAG);
    }

    /**
     * Writes a class description.
     * 
     * @param writer  the writer.
     * @param cd  the class description.
     * 
     * @throws IOException if there is an I/O problem.
     */
    protected void writeClassDescription(final Writer writer, final ClassDescription cd) throws IOException {

        if (cd.isUndefined()) {
            return;
        }

        final AttributeList al = new AttributeList();
        al.setAttribute(ClassModelTags.CLASS_ATTR, cd.getName());
        if (cd.getRegisterKey() != null) {
            al.setAttribute(ClassModelTags.REGISTER_NAMES_ATTR, cd.getRegisterKey());
        }
        if (cd.isPreserve()) {
            al.setAttribute(ClassModelTags.IGNORE_ATTR, "true");
        }
        this.writerSupport.writeTag(writer, ClassModelTags.OBJECT_TAG, al, XMLWriterSupport.OPEN);

        final TypeInfo[] constructorInfo = cd.getConstructorDescription();
        if (constructorInfo != null && constructorInfo.length != 0) {
            this.writerSupport.writeTag(writer, ClassModelTags.CONSTRUCTOR_TAG);
            for (int i = 0; i < constructorInfo.length; i++) {
                final AttributeList constructorList = new AttributeList();
                constructorList.setAttribute(
                    ClassModelTags.CLASS_ATTR, constructorInfo[i].getType().getName()
                );
                constructorList.setAttribute(
                    ClassModelTags.PROPERTY_ATTR, constructorInfo[i].getName()
                );
                writeTag(writer, ClassModelTags.PARAMETER_TAG, constructorList, 
                         constructorInfo[i].getComments());
            }
            this.writerSupport.writeCloseTag(writer, ClassModelTags.CONSTRUCTOR_TAG);
        }

        final PropertyInfo[] properties = cd.getProperties();
        for (int i = 0; i < properties.length; i++) {
            writePropertyInfo(writer, properties[i]);
        }

        this.writerSupport.writeCloseTag(writer, ClassModelTags.OBJECT_TAG);
    }

    /**
     * Writes a property info element.
     * 
     * @param writer  the writer.
     * @param ipi  the property info.
     * 
     * @throws IOException if there is an I/O problem.
     */
    private void writePropertyInfo(final Writer writer, final PropertyInfo ipi) throws IOException {
        final AttributeList props = new AttributeList();
        props.setAttribute(ClassModelTags.NAME_ATTR, ipi.getName());

        if (ipi instanceof IgnoredPropertyInfo) {
            writeTag(writer, ClassModelTags.IGNORED_PROPERTY_TAG, props, ipi.getComments());
            return;
        }

        if (ipi.getPropertyType().equals(PropertyType.ATTRIBUTE)) {
            props.setAttribute(ClassModelTags.ATTRIBUTE_ATTR, ipi.getXmlName());
            props.setAttribute(ClassModelTags.ATTRIBUTE_HANDLER_ATTR, ipi.getXmlHandler());
            writeTag(writer, ClassModelTags.ATTRIBUTE_PROPERTY_TAG, props, ipi.getComments());
        }
        else if (ipi.getPropertyType().equals(PropertyType.ELEMENT)) {
            if (ipi.getComments() == null || ipi.getComments().getOpenTagComment() == null)
            {
                this.writerSupport.indent(writer, XMLWriterSupport.INDENT_ONLY);
                writer.write("<!-- property type is " + ipi.getType() + " -->");
                writer.write(System.getProperty("line.separator", "\n"));
            }
            props.setAttribute(ClassModelTags.ELEMENT_ATTR, ipi.getXmlName());
            writeTag(writer, ClassModelTags.ELEMENT_PROPERTY_TAG, props, ipi.getComments());
        }
        else {
            props.setAttribute(ClassModelTags.LOOKUP_ATTR, ipi.getXmlName());
            writeTag(writer, ClassModelTags.LOOKUP_PROPERTY_TAG, props, ipi.getComments());
        }
    }

    /**
     * Returns the writer support object.
     * 
     * @return The writer support object.
     */
    public XMLWriterSupport getWriterSupport() {
        return this.writerSupport;
    }
}

⌨️ 快捷键说明

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