cmsxmlcontenteditor.java

来自「找了很久才找到到源代码」· Java 代码 · 共 1,609 行 · 第 1/5 页

JAVA
1,609
字号
            if (addElement && removeElement) {
                btIcon = "xmledit_del_add.png";
            } else if (addElement) {
                btIcon = "xmledit_add.png";
                // create button action to add element on button click
                StringBuffer action = new StringBuffer(128);
                action.append("addElement('");
                action.append(elementName);
                action.append("', ");
                action.append(index);
                action.append(");");
                btAction = action.toString();
            } else if (removeElement) {
                btIcon = "xmledit_del.png";
                // create button action to remove element on button click
                StringBuffer action = new StringBuffer(128);
                action.append("removeElement('");
                action.append(elementName);
                action.append("', ");
                action.append(index);
                action.append(");");
                btAction = action.toString();
            }
            StringBuffer href = new StringBuffer(512);
            href.append("javascript:");
            href.append(btAction);
            href.append("\" onmouseover=\"");
            href.append(jsCall);
            href.append("checkElementButtons(true);\" onmouseout=\"checkElementButtons(false);\" id=\"btimg.");
            href.append(elementName).append(".").append(index);
            result = button(href.toString(), null, btIcon, Messages.GUI_EDITOR_XMLCONTENT_ELEMENT_BUTTONS_0, 0);
        } else {
            // no active button, create a spacer
            result = buttonBarSpacer(1);
        }

        return result;
    }

    /**
     * Corrects the XML structure of the edited content according to the XSD.<p>
     * 
     * @throws CmsException if the correction fails
     */
    private void correctXmlStructure() throws CmsException {

        m_content.setAutoCorrectionEnabled(true);
        m_content.correctXmlStructure(getCms());
        // write the corrected temporary file
        writeContent();
    }

    /**
     * Returns a list of value names containing the complete xpath of each value as String.<p>
     * 
     * @param contentDefinition the content definition to use
     * @param pathPrefix the xpath prefix
     * @param locale the locale to use
     * @return list of value names (containing the xpath of each value)
     */
    private List getSimpleValueNames(CmsXmlContentDefinition contentDefinition, String pathPrefix, Locale locale) {

        List valueNames = new ArrayList();
        Iterator i = contentDefinition.getTypeSequence().iterator();
        while (i.hasNext()) {

            I_CmsXmlSchemaType type = (I_CmsXmlSchemaType)i.next();

            String name = pathPrefix + type.getName();

            // get the element sequence of the type
            CmsXmlContentValueSequence elementSequence = m_content.getValueSequence(name, locale);
            int elementCount = elementSequence.getElementCount();
            // loop through elements
            for (int j = 0; j < elementCount; j++) {
                I_CmsXmlContentValue value = elementSequence.getValue(j);

                StringBuffer xPath = new StringBuffer(pathPrefix.length() + 16);
                xPath.append(pathPrefix);
                xPath.append(CmsXmlUtils.createXpathElement(type.getName(), value.getIndex() + 1));

                if (!type.isSimpleType()) {
                    // recurse into nested type sequence
                    CmsXmlNestedContentDefinition nestedSchema = (CmsXmlNestedContentDefinition)type;
                    xPath.append("/");
                    valueNames.addAll(getSimpleValueNames(
                        nestedSchema.getNestedContentDefinition(),
                        xPath.toString(),
                        locale));
                } else {
                    // this is a simple type, get widget to display
                    valueNames.add(xPath.toString());
                }
            }
        }
        return valueNames;
    }

    /**
     * Returns the error handler for error handling of the edited xml content.<p>
     * 
     * @return the error handler
     */
    private CmsXmlContentErrorHandler getValidationHandler() {

        if (m_validationHandler == null) {
            // errors were not yet checked, do this now and store result in member
            m_validationHandler = m_content.validate(getCms());
        }
        return m_validationHandler;
    }

    /**
     * Generates the HTML form for the XML content editor.<p>
     * 
     * This is a recursive method because nested schemas are possible,
     * do not call this method directly.<p>
     * 
     * @param contentDefinition the content definition to start with
     * @param pathPrefix for nested xml content
     * @param showHelpBubble if the code for a help bubble should be generated
     * 
     * @return the HTML that generates the form for the XML editor
     */
    private StringBuffer getXmlEditorForm(
        CmsXmlContentDefinition contentDefinition,
        String pathPrefix,
        boolean showHelpBubble) {

        StringBuffer result = new StringBuffer(1024);
        // only show errors if editor is not opened initially
        boolean showErrors = (getAction() != ACTION_NEW) && (getAction() != ACTION_DEFAULT);

        try {
            // check if we are in a nested content definition
            boolean nested = CmsStringUtil.isNotEmpty(pathPrefix);

            // create table
            result.append("<table class=\"xmlTable");
            if (nested) {
                // use other style for nested content definition table
                result.append("Nested");
            }
            result.append("\">\n");

            // show error header once if there were validation errors
            if (!nested && showErrors && (getValidationHandler().hasErrors())) {
                result.append("<tr><td colspan=\"4\">&nbsp;</td></tr>\n");
                result.append("<tr><td colspan=\"2\">&nbsp;</td>");
                result.append("<td class=\"xmlTdErrorHeader\">");
                result.append(key(Messages.ERR_EDITOR_XMLCONTENT_VALIDATION_ERROR_TITLE_0));
                result.append("</td><td>&nbsp;");
                result.append("</td></tr>\n");

                // show errors in different locales
                if ((getValidationHandler().getErrors(getElementLocale()) == null)
                    || (getValidationHandler().getErrors().size() > getValidationHandler().getErrors(getElementLocale()).size())) {

                    // iterate through all found errors
                    Map locErrors = getValidationHandler().getErrors();
                    Iterator locErrorsIter = locErrors.entrySet().iterator();
                    while (locErrorsIter.hasNext()) {
                        Map.Entry locEntry = (Map.Entry)locErrorsIter.next();
                        Locale locale = (Locale)locEntry.getKey();

                        // skip errors in the actual locale
                        if (getElementLocale().equals(locale)) {
                            continue;
                        }

                        result.append("<tr><td colspan=\"2\">&nbsp;</td>");
                        result.append("<td class=\"xmlTdError\">");
                        result.append(key(
                            Messages.ERR_EDITOR_XMLCONTENT_VALIDATION_ERROR_LANG_1,
                            new Object[] {locale.getLanguage()}));
                        result.append("</td><td>&nbsp;");
                        result.append("</td></tr>\n");

                        result.append("<tr><td colspan=\"2\">&nbsp;</td>");
                        result.append("<td class=\"xmlTdError\">");
                        result.append("<ul>");

                        // iterate through the found errors in a different locale
                        Map elErrors = (Map)locEntry.getValue();
                        Iterator elErrorsIter = elErrors.entrySet().iterator();
                        while (elErrorsIter.hasNext()) {
                            Map.Entry elEntry = (Map.Entry)elErrorsIter.next();

                            String nodeName = (String)elEntry.getKey();
                            String errorMsg = (String)elEntry.getValue();

                            // output the error message
                            result.append("<li>");
                            result.append(nodeName);
                            result.append(": ");
                            result.append(errorMsg);
                            result.append("</li>\n");
                        }

                        result.append("</ul>");
                        result.append("</td><td>&nbsp;");
                        result.append("</td></tr>\n");
                    }
                }
            }

            // iterate the type sequence        
            Iterator i = contentDefinition.getTypeSequence().iterator();
            while (i.hasNext()) {
                // get the type
                I_CmsXmlSchemaType type = (I_CmsXmlSchemaType)i.next();
                CmsXmlContentDefinition nestedContentDefinition = contentDefinition;
                if (!type.isSimpleType()) {
                    // get nested content definition for nested types
                    CmsXmlNestedContentDefinition nestedSchema = (CmsXmlNestedContentDefinition)type;
                    nestedContentDefinition = nestedSchema.getNestedContentDefinition();
                }
                // create xpath to the current element
                String name = pathPrefix + type.getName();

                // get the element sequence of the current type
                CmsXmlContentValueSequence elementSequence = m_content.getValueSequence(name, getElementLocale());
                int elementCount = elementSequence.getElementCount();

                // check if value is optional or multiple
                boolean addValue = false;
                if (elementCount < type.getMaxOccurs()) {
                    addValue = true;
                }
                boolean removeValue = false;
                if (elementCount > type.getMinOccurs()) {
                    removeValue = true;
                }

                // assure that at least one element is present in sequence
                boolean disabledElement = false;
                if (elementCount < 1) {
                    // current element is disabled, create dummy element
                    elementCount = 1;
                    elementSequence.addValue(getCms(), 0);
                    disabledElement = true;
                    m_optionalElementPresent = true;
                }

                // loop through multiple elements
                for (int j = 0; j < elementCount; j++) {
                    // get value and corresponding widget
                    I_CmsXmlContentValue value = elementSequence.getValue(j);
                    I_CmsWidget widget = null;
                    if (type.isSimpleType()) {
                        widget = contentDefinition.getContentHandler().getWidget(value);
                    }

                    // show errors and/or warnings               
                    String key = value.getPath();
                    if (showErrors
                        && getValidationHandler().hasErrors(getElementLocale())
                        && getValidationHandler().getErrors(getElementLocale()).containsKey(key)) {
                        // show error message
                        result.append("<tr><td></td><td><img src=\"");
                        result.append(getEditorResourceUri());
                        result.append("error.png");
                        result.append("\" border=\"0\" alt=\"\"></td><td class=\"xmlTdError\">");
                        result.append(resolveMacros((String)getValidationHandler().getErrors(getElementLocale()).get(
                            key)));
                        result.append("</td><td></td></tr>\n");
                    }
                    // warnings can be additional to errors
                    if (showErrors
                        && getValidationHandler().hasWarnings(getElementLocale())
                        && getValidationHandler().getWarnings(getElementLocale()).containsKey(key)) {
                        // show warning message
                        result.append("<tr><td></td><td><img src=\"");
                        result.append(getEditorResourceUri());
                        result.append("warning.png");
                        result.append("\" border=\"0\" alt=\"\"></td><td class=\"xmlTdWarning\">");
                        result.append(resolveMacros((String)getValidationHandler().getWarnings(getElementLocale()).get(
                            key)));
                        result.append("</td><td></td></tr>\n");
                    }
                    // create label and help bubble cells
                    result.append("<tr>");
                    result.append("<td class=\"xmlLabel");
                    if (disabledElement) {
                        // element is disabled, mark it with css
                        result.append("Disabled");
                    }
                    result.append("\">");
                    result.append(keyDefault(A_CmsWidget.getLabelKey((I_CmsWidgetParameter)value), value.getName()));
                    if (elementCount > 1) {
                        result.append(" [").append(value.getIndex() + 1).append("]");
                    }
                    result.append(": </td>");
                    if (showHelpBubble && (widget != null) && (value.getIndex() == 0)) {
                        // show help bubble only on first element of each content definition 
                        result.append(widget.getHelpBubble(getCms(), this, (I_CmsWidgetParameter)value));
                    } else {
                        // create empty cell for all following elements 
                        result.append(buttonBarSpacer(16));
                    }

                    // append individual widget html cell if element is enabled
                    if (!disabledElement) {
                        if (widget == null) {
                            // recurse into nested type sequence
                            String newPath = CmsXmlUtils.createXpathElement(value.getName(), value.getIndex() + 1);
                            result.append("<td class=\"maxwidth\">");
                            boolean showHelp = (j == 0);
                            result.append(getXmlEditorForm(
                                nestedContentDefinition,
                                pathPrefix + newPath + "/",
                                showHelp));
                            result.append("</td>");
                        } else {
                            // this is a simple type, display widget
                            result.append(widget.getDialogWidget(getCms(), this, (I_CmsWidgetParameter)value));
                        }
                    } else {
                        // disabled element, show message for optional element
                        result.append("<td class=\"xmlTdDisabled maxwidth\">");
 

⌨️ 快捷键说明

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