Search in sources :

Example 1 with CmsFile

use of org.opencms.file.CmsFile in project opencms-core by alkacon.

the class CmsCloneModuleThread method replaceModuleName.

/**
 * Initializes a thread to find and replace all occurrence of the module's path.<p>
 *
 * @throws CmsException in case writing the file fails
 * @throws UnsupportedEncodingException in case of the wrong encoding
 */
private void replaceModuleName() throws CmsException, UnsupportedEncodingException {
    CmsResourceFilter filter = CmsResourceFilter.ALL.addRequireFile().addExcludeState(CmsResource.STATE_DELETED).addRequireTimerange().addRequireVisible();
    List<CmsResource> resources = getCms().readResources(CmsWorkplace.VFS_PATH_MODULES + m_cloneInfo.getName() + "/", filter);
    for (CmsResource resource : resources) {
        CmsFile file = getCms().readFile(resource);
        if (CmsResourceTypeXmlContent.isXmlContent(file)) {
            CmsXmlContent xmlContent = CmsXmlContentFactory.unmarshal(getCms(), file);
            xmlContent.setAutoCorrectionEnabled(true);
            file = xmlContent.correctXmlStructure(getCms());
        }
        byte[] contents = file.getContents();
        String encoding = CmsLocaleManager.getResourceEncoding(getCms(), file);
        String content = new String(contents, encoding);
        Matcher matcher = Pattern.compile(m_cloneInfo.getSourceModuleName()).matcher(content);
        if (matcher.find()) {
            contents = matcher.replaceAll(m_cloneInfo.getName()).getBytes(encoding);
            if (lockResource(getCms(), file)) {
                file.setContents(contents);
                getCms().writeFile(file);
            }
        }
    }
}
Also used : CmsXmlContent(org.opencms.xml.content.CmsXmlContent) CmsFile(org.opencms.file.CmsFile) CmsResource(org.opencms.file.CmsResource) Matcher(java.util.regex.Matcher) CmsResourceFilter(org.opencms.file.CmsResourceFilter)

Example 2 with CmsFile

use of org.opencms.file.CmsFile in project opencms-core by alkacon.

the class CmsCloneModuleThread method transformResource.

/**
 * Reads a file into a string, applies a transformation to the string, and writes the string back to the file.<p>
 *
 * @param resource the resource to transform
 * @param transformation the transformation to apply
 * @throws CmsException if something goes wrong
 * @throws UnsupportedEncodingException in case the encoding is not supported
 */
private void transformResource(CmsResource resource, Function<String, String> transformation) throws CmsException, UnsupportedEncodingException {
    CmsFile file = getCms().readFile(resource);
    if (CmsResourceTypeXmlContent.isXmlContent(file)) {
        CmsXmlContent xmlContent = CmsXmlContentFactory.unmarshal(getCms(), file);
        xmlContent.setAutoCorrectionEnabled(true);
        file = xmlContent.correctXmlStructure(getCms());
    }
    String encoding = CmsLocaleManager.getResourceEncoding(getCms(), file);
    String content = new String(file.getContents(), encoding);
    content = transformation.apply(content);
    file.setContents(content.getBytes(encoding));
    lockResource(getCms(), file);
    getCms().writeFile(file);
}
Also used : CmsXmlContent(org.opencms.xml.content.CmsXmlContent) CmsFile(org.opencms.file.CmsFile)

Example 3 with CmsFile

use of org.opencms.file.CmsFile in project opencms-core by alkacon.

the class CmsCloneModuleThread method replaceFormatterPaths.

/**
 * Replaces the referenced formatters within the new XSD files with the new formatter paths.<p>
 *
 * @param targetModule the target module
 *
 * @throws CmsException if something goes wrong
 * @throws UnsupportedEncodingException if the file content could not be read with the determined encoding
 */
private void replaceFormatterPaths(CmsModule targetModule) throws CmsException, UnsupportedEncodingException {
    CmsResource formatterSourceFolder = getCms().readResource("/system/modules/" + m_cloneInfo.getFormatterSourceModule() + "/");
    CmsResource formatterTargetFolder = getCms().readResource("/system/modules/" + m_cloneInfo.getFormatterTargetModule() + "/");
    for (I_CmsResourceType type : targetModule.getResourceTypes()) {
        String schemaPath = type.getConfiguration().get("schema");
        CmsResource res = getCms().readResource(schemaPath);
        CmsFile file = getCms().readFile(res);
        if (CmsResourceTypeXmlContent.isXmlContent(file)) {
            CmsXmlContent xmlContent = CmsXmlContentFactory.unmarshal(getCms(), file);
            xmlContent.setAutoCorrectionEnabled(true);
            file = xmlContent.correctXmlStructure(getCms());
        }
        String encoding = CmsLocaleManager.getResourceEncoding(getCms(), file);
        String content = new String(file.getContents(), encoding);
        content = content.replaceAll(formatterSourceFolder.getRootPath(), formatterTargetFolder.getRootPath());
        file.setContents(content.getBytes(encoding));
        getCms().writeFile(file);
    }
}
Also used : I_CmsResourceType(org.opencms.file.types.I_CmsResourceType) CmsXmlContent(org.opencms.xml.content.CmsXmlContent) CmsFile(org.opencms.file.CmsFile) CmsResource(org.opencms.file.CmsResource)

Example 4 with CmsFile

use of org.opencms.file.CmsFile in project opencms-core by alkacon.

the class CmsModuleAddResourceTypeThread method copySampleFiles.

/**
 * Copies sample schema and resource type icons and adds the resources to the module.<p>
 *
 * @param module the module
 * @param moduleFolder the module folder name
 *
 * @throws CmsIllegalArgumentException in case something goes wrong copying the resources
 * @throws CmsException in case something goes wrong copying the resources
 */
private void copySampleFiles(CmsModule module, String moduleFolder) throws CmsIllegalArgumentException, CmsException {
    CmsObject cms = getCms();
    List<String> moduleResource = new ArrayList<String>(module.getResources());
    if (!cms.existsResource(moduleFolder)) {
        cms.createResource(moduleFolder, CmsResourceTypeFolder.getStaticTypeId());
        moduleResource.add(moduleFolder);
    }
    String schemaFolder = CmsStringUtil.joinPaths(moduleFolder, "schemas");
    if (!cms.existsResource(schemaFolder)) {
        cms.createResource(schemaFolder, CmsResourceTypeFolder.getStaticTypeId());
    }
    String schemaFile = CmsStringUtil.joinPaths(schemaFolder, m_resInfo.getName() + ".xsd");
    if (!cms.existsResource(schemaFile)) {
        cms.copyResource(SAMPLE_SCHEMA, schemaFile, CmsResource.COPY_AS_NEW);
        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(m_resInfo.getSchemaTypeName())) {
            // replace the sample schema type name with the provided name
            try {
                CmsFile schema = cms.readFile(schemaFile);
                OpenCms.getLocaleManager();
                String schemaContent = new String(schema.getContents(), CmsLocaleManager.getResourceEncoding(cms, schema));
                schemaContent = schemaContent.replaceAll(SAMPLE_SCHEMA_TYPE_NAME, m_resInfo.getSchemaTypeName());
                schema.setContents(schemaContent.getBytes());
                cms.writeFile(schema);
            } catch (Exception e) {
                LOG.error(e.getLocalizedMessage(), e);
                getReport().addError(e);
            }
        }
    }
    m_resInfo.setSchema(schemaFile);
    String filetypesFolder = "/system/workplace/resources/filetypes/";
    String smallIcon = CmsStringUtil.joinPaths(filetypesFolder, m_resInfo.getName() + ".png");
    if (!cms.existsResource(smallIcon)) {
        cms.copyResource(SAMPLE_ICON_SMALL, smallIcon, CmsResource.COPY_AS_NEW);
        moduleResource.add(smallIcon);
    }
    m_resInfo.setSmallIcon(m_resInfo.getName() + ".png");
    String bigIcon = CmsStringUtil.joinPaths(filetypesFolder, m_resInfo.getName() + "_big.png");
    if (!cms.existsResource(bigIcon)) {
        cms.copyResource(SAMPLE_ICON_BIG, bigIcon, CmsResource.COPY_AS_NEW);
        moduleResource.add(bigIcon);
    }
    m_resInfo.setBigIcon(m_resInfo.getName() + "_big.png");
    module.setResources(moduleResource);
}
Also used : CmsFile(org.opencms.file.CmsFile) CmsObject(org.opencms.file.CmsObject) ArrayList(java.util.ArrayList) CmsIllegalArgumentException(org.opencms.main.CmsIllegalArgumentException) CmsException(org.opencms.main.CmsException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 5 with CmsFile

use of org.opencms.file.CmsFile in project opencms-core by alkacon.

the class CmsResourceWrapperModulesNonLazy method createResource.

/**
 * @see org.opencms.file.wrapper.A_CmsResourceWrapper#createResource(org.opencms.file.CmsObject, java.lang.String, int, byte[], java.util.List)
 */
@Override
public CmsResource createResource(CmsObject cms, String resourcename, int type, byte[] content, List<CmsProperty> properties) throws CmsException, CmsIllegalArgumentException {
    if (checkAccess(cms) && matchParentPath(IMPORT_PATH, resourcename)) {
        CmsResource res = createFakeBinaryFile(resourcename, 0);
        CmsFile file = new CmsFile(res);
        file.setContents(content);
        OpenCms.getModuleManager().getImportExportRepository().importModule(CmsResource.getName(resourcename), content);
        m_importFileUpdateCache.put(resourcename, Long.valueOf(System.currentTimeMillis()));
        return file;
    } else {
        return super.createResource(cms, resourcename, type, content, properties);
    }
}
Also used : CmsFile(org.opencms.file.CmsFile) CmsResource(org.opencms.file.CmsResource)

Aggregations

CmsFile (org.opencms.file.CmsFile)303 CmsResource (org.opencms.file.CmsResource)141 CmsException (org.opencms.main.CmsException)114 CmsObject (org.opencms.file.CmsObject)106 CmsXmlContent (org.opencms.xml.content.CmsXmlContent)75 Locale (java.util.Locale)62 CmsProperty (org.opencms.file.CmsProperty)42 ArrayList (java.util.ArrayList)40 CmsUUID (org.opencms.util.CmsUUID)37 I_CmsXmlContentValue (org.opencms.xml.types.I_CmsXmlContentValue)27 UnsupportedEncodingException (java.io.UnsupportedEncodingException)25 CmsVfsResourceNotFoundException (org.opencms.file.CmsVfsResourceNotFoundException)24 CmsXmlPage (org.opencms.xml.page.CmsXmlPage)23 HashMap (java.util.HashMap)22 IOException (java.io.IOException)20 CmsXmlException (org.opencms.xml.CmsXmlException)20 Map (java.util.Map)17 ByteArrayInputStream (java.io.ByteArrayInputStream)15 CmsLock (org.opencms.lock.CmsLock)15 List (java.util.List)14