cmsresourcewrapperxmlpage.java

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

JAVA
1,069
字号

            // if sub path is empty
            if (CmsStringUtil.isEmptyOrWhitespaceOnly(path)) {

                // delete the xml page itself
                cms.deleteResource(resourcename, siblingMode);

                // remove all virtual files for this resource
                Iterator iter = getVirtualFiles().iterator();
                while (iter.hasNext()) {
                    TMP_FILE_TABLE.remove(resourcename + "/" + (String)iter.next());
                }

                return true;
            }

            CmsFile file = cms.readFile(xmlPage);
            CmsXmlPage xml = CmsXmlPageFactory.unmarshal(cms, file);

            String[] tokens = path.split("/");
            if (tokens.length == 1) {

                // deleting a virtual file
                if (getVirtualFiles().contains(tokens[0])) {

                    // mark the virtual file in the temp file table as deleted
                    TMP_FILE_TABLE.put(resourcename, new Integer(0));
                } else {

                    // delete locale
                    xml.removeLocale(new Locale(tokens[0]));

                    // save the xml page
                    file.setContents(xml.marshal());
                    cms.writeFile(file);
                }

            } else if (tokens.length == 2) {

                String name = tokens[1];
                if (name.endsWith(EXTENSION_ELEMENT)) {
                    name = name.substring(0, name.length() - EXTENSION_ELEMENT.length() - 1);
                }

                // delete element
                xml.removeValue(name, new Locale(tokens[0]));

                // save the xml page
                file.setContents(xml.marshal());
                cms.writeFile(file);
            }

            return true;
        }

        return false;
    }

    /**
     * @see org.opencms.file.wrapper.A_CmsResourceWrapper#getLock(org.opencms.file.CmsObject, org.opencms.file.CmsResource)
     */
    public CmsLock getLock(CmsObject cms, CmsResource resource) throws CmsException {

        CmsResource xmlPage = cms.readResource(resource.getStructureId());
        //CmsResource xmlPage = findXmlPage(cms, resource.getRootPath());
        if (xmlPage != null) {

            I_CmsResourceType resType = OpenCms.getResourceManager().getResourceType(xmlPage.getTypeId());
            if (resType instanceof CmsResourceTypeXmlPage) {
                return cms.getLock(xmlPage);
            }
        }

        return null;
    }

    /**
     * @see org.opencms.file.wrapper.I_CmsResourceWrapper#isWrappedResource(org.opencms.file.CmsObject, org.opencms.file.CmsResource)
     */
    public boolean isWrappedResource(CmsObject cms, CmsResource res) {

        try {
            I_CmsResourceType resType = OpenCms.getResourceManager().getResourceType(res.getTypeId());
            if (resType instanceof CmsResourceTypeXmlPage) {
                return true;
            }
        } catch (CmsException ex) {
            // noop
        }

        return false;
    }

    /**
     * @see org.opencms.file.wrapper.A_CmsResourceWrapper#lockResource(org.opencms.file.CmsObject, java.lang.String)
     */
    public boolean lockResource(CmsObject cms, String resourcename) throws CmsException {

        CmsResource res = findXmlPage(cms, resourcename);
        if (res != null) {
            cms.lockResource(cms.getRequestContext().removeSiteRoot(res.getRootPath()));
            return true;
        }

        return false;
    }

    /**
     * @see org.opencms.file.wrapper.A_CmsResourceWrapper#moveResource(org.opencms.file.CmsObject, java.lang.String, java.lang.String)
     */
    public boolean moveResource(CmsObject cms, String source, String destination)
    throws CmsException, CmsIllegalArgumentException {

        // only allow copying of xml pages at whole or locales and elements inside the same xml page
        CmsResource srcXmlPage = findXmlPage(cms, source);

        if (srcXmlPage != null) {
            String srcPath = getSubPath(cms, srcXmlPage, source);

            // if the source is the xml page itself just copy the resource
            if (CmsStringUtil.isEmptyOrWhitespaceOnly(srcPath)) {
                cms.moveResource(source, destination);
                return true;
            } else {

                // only a locale or an element should be copied
                CmsResource destXmlPage = findXmlPage(cms, destination);
                if (srcXmlPage.equals(destXmlPage)) {

                    // copying inside the same xml page resource
                    String destPath = getSubPath(cms, destXmlPage, destination);

                    String[] srcTokens = srcPath.split("/");
                    String[] destTokens = destPath.split("/");

                    if (srcTokens.length == destTokens.length) {

                        CmsFile srcFile = cms.readFile(srcXmlPage);
                        CmsXmlPage srcXml = CmsXmlPageFactory.unmarshal(cms, srcFile);

                        if (srcTokens.length == 1) {

                            // copy locale
                            srcXml.moveLocale(new Locale(srcTokens[0]), new Locale(destTokens[0]));
                        } else if (srcTokens.length == 2) {

                            // TODO: move element
                        }

                        // write file
                        srcFile.setContents(srcXml.marshal());
                        cms.writeFile(srcFile);
                    } else {

                        // TODO: error: destination path is invalid
                    }
                } else {

                    // TODO: error: moving only allowed inside the same xml page
                }
            }

            return true;
        }

        return false;
    }

    /**
     * @see org.opencms.file.wrapper.A_CmsResourceWrapper#readFile(CmsObject, String, CmsResourceFilter)
     */
    public CmsFile readFile(CmsObject cms, String resourcename, CmsResourceFilter filter) throws CmsException {

        // find the xml page this is for
        CmsResource xmlPage = findXmlPage(cms, resourcename);
        if (xmlPage != null) {

            // cut off trailing slash
            if (resourcename.endsWith("/")) {
                resourcename = resourcename.substring(0, resourcename.length() - 1);
            }

            // get the path below the xml page
            String path = getSubPath(cms, xmlPage, resourcename);

            String[] tokens = path.split("/");
            if (tokens.length == 1) {

                CmsFile file = cms.readFile(xmlPage);

                // check temp file table to remove deleted virtual files
                if ((TMP_FILE_TABLE.containsKey(resourcename))
                    && (TMP_FILE_TABLE.get(resourcename).equals(new Integer(0)))) {
                    return null;
                }

                // read the control code resource
                if (tokens[0].equals(NAME_ELEMENT_CONTROLCODE)) {

                    CmsWrappedResource wrap = new CmsWrappedResource(xmlPage);
                    wrap.setRootPath(xmlPage.getRootPath() + "/" + NAME_ELEMENT_CONTROLCODE);

                    CmsFile ret = wrap.getFile();
                    ret.setContents(file.getContents());
                    return ret;
                }
            } else if (tokens.length == 2) {

                CmsFile file = cms.readFile(xmlPage);
                CmsXmlPage xml = CmsXmlPageFactory.unmarshal(cms, file);

                // cut off the html suffix
                String name = tokens[1];
                if (name.endsWith("." + EXTENSION_ELEMENT)) {
                    name = name.substring(0, name.length() - 5);
                }

                if (xml.hasValue(name, new Locale(tokens[0]))) {

                    String contentString = xml.getStringValue(cms, name, new Locale(tokens[0]));
                    String fullPath = xmlPage.getRootPath() + "/" + tokens[0] + "/" + name + "." + EXTENSION_ELEMENT;
                    contentString = prepareContent(contentString, cms, xmlPage, fullPath);

                    byte[] content;
                    try {
                        content = contentString.getBytes(CmsLocaleManager.getResourceEncoding(cms, xmlPage));
                    } catch (UnsupportedEncodingException e) {
                        // should never happen
                        content = contentString.getBytes();
                    }
                    CmsResource resElem = getResourceForElement(xmlPage, fullPath, content.length);
                    CmsFile fileElem = new CmsFile(resElem);

                    fileElem.setContents(content);
                    return fileElem;
                }
            }
        }

        return null;
    }

    /**
     * @see org.opencms.file.wrapper.A_CmsResourceWrapper#readResource(CmsObject, String, CmsResourceFilter)
     */
    public CmsResource readResource(CmsObject cms, String resourcename, CmsResourceFilter filter) throws CmsException {

        try {

            // try to read the resource for the resource name
            CmsResource res = cms.readResource(resourcename, filter);
            if (CmsResourceTypeXmlPage.isXmlPage(res)) {
                // return the xml page resource as a folder
                return wrapResource(cms, res);
            }

            return null;
        } catch (CmsVfsResourceNotFoundException ex) {

            // find the xml page this is for
            CmsResource xmlPage = findXmlPage(cms, resourcename);
            if (xmlPage != null) {

                // cut off trailing slash
                if (resourcename.endsWith("/")) {
                    resourcename = resourcename.substring(0, resourcename.length() - 1);
                }

                // get the path below the xml page
                String path = getSubPath(cms, xmlPage, resourcename);

                CmsFile file = cms.readFile(xmlPage);
                CmsXmlPage xml = CmsXmlPageFactory.unmarshal(cms, file);

                String[] tokens = path.split("/");
                if (tokens.length == 1) {

                    // check temp file table to remove deleted virtual files
                    if ((TMP_FILE_TABLE.containsKey(resourcename))
                        && (TMP_FILE_TABLE.get(resourcename).equals(new Integer(0)))) {
                        return null;
                    }

                    // read the control code resource
                    if (tokens[0].equals(NAME_ELEMENT_CONTROLCODE)) {

                        CmsWrappedResource wrap = new CmsWrappedResource(xmlPage);
                        wrap.setRootPath(xmlPage.getRootPath() + "/" + NAME_ELEMENT_CONTROLCODE);
                        return wrap.getResource();
                    } else {

                        Locale locale = new Locale(tokens[0]);
                        if (xml.hasLocale(locale) && (file.getLength() > 0)) {
                            return getResourceForLocale(xmlPage, locale);
                        }
                    }
                } else if (tokens.length == 2) {

                    // cut off the html suffix
                    String name = tokens[1];
                    if (name.endsWith("." + EXTENSION_ELEMENT)) {
                        name = name.substring(0, name.length() - 5);
                    }

                    Locale locale = new Locale(tokens[0]);
                    if (xml.hasValue(name, locale)) {
                        String content = xml.getStringValue(cms, name, locale);
                        String fullPath = xmlPage.getRootPath()
                            + "/"
                            + tokens[0]
                            + "/"
                            + name
                            + "."
                            + EXTENSION_ELEMENT;
                        content = prepareContent(content, cms, xmlPage, fullPath);

                        int length = content.length();
                        try {
                            length = content.getBytes(CmsLocaleManager.getResourceEncoding(cms, xmlPage)).length;
                        } catch (UnsupportedEncodingException e) {
                            // this will never happen since UTF-8 is always supported
                        }

                        return getResourceForElement(xmlPage, fullPath, length);
                    }
                }

            }

            return null;
        }
    }

    /**
     * @see org.opencms.file.wrapper.A_CmsResourceWrapper#restoreLink(org.opencms.file.CmsObject, java.lang.String)
     */
    public String restoreLink(CmsObject cms, String uri) {

        CmsResource res = findXmlPage(cms, uri);
        if (res != null) {
            return res.getRootPath();
        }

        return null;
    }

    /**
     * @see org.opencms.file.wrapper.A_CmsResourceWrapper#rewriteLink(CmsObject, CmsResource)
     */
    public String rewriteLink(CmsObject cms, CmsResource res) {

        if (isWrappedResource(cms, res)) {
            String path = res.getRootPath();
            if (!path.endsWith("/")) {
                path += "/";
            }

⌨️ 快捷键说明

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