Helper method to determine the encoding of the given file in the VFS,
which must be set using the "content-encoding" property.<p>

@param cms the CmsObject
@param filename the name of the file which is to be checked
@return the encoding for the file
336477
protected String getFileEncoding(CmsObject cms, String filename) {

        try {
            return cms.readPropertyObject(filename, CmsPropertyDefinition.PROPERTY_CONTENT_ENCODING, true).getValue(
                OpenCms.getSystemInfo().getDefaultEncoding());
        } catch (CmsException e) {
            return OpenCms.getSystemInfo().getDefaultEncoding();
        }
    }
861465
protected void resolveDefaultMappings(CmsObject cms, CmsFile file, CmsXmlContent content) throws CmsException {

        for (Map.Entry<String, List<String>> e : m_elementMappings.entrySet()) {
            String path = e.getKey();
            List<String> mappings = e.getValue();
            if (mappings == null) {
                // nothing to do if we have no mappings at all
                continue;
            }
            for (int v = mappings.size() - 1; v >= 0; v--) {
                String mapping = mappings.get(v);
                if (!isMappingUsingDefault(path, mapping)) {
                    continue;
                }
                for (Locale locale : content.getLocales()) {
                    if (content.hasValue(path, locale)) {
                        continue;
                    } else {
                        String defaultValue = getDefault(cms, file, null, path, locale);
                        if (defaultValue != null) {
                            resolveMapping(cms, content, path, true, 0, locale, defaultValue);
                        }
                    }
                }

            }
        }
    }
1117883
private boolean hasContentInVfsOrImport(CmsResource resource) {

        if (m_contentFiles.contains(resource.getResourceId())) {
            return true;
        }
        try {
            List<CmsResource> resources = getCms().readSiblings(resource, CmsResourceFilter.ALL);
            if (!resources.isEmpty()) {
                return true;
            }
        } catch (CmsException e) {
            LOG.warn(e.getLocalizedMessage(), e);
        }
        return false;

    }
718142
public static String getContentType(String filename) {
		final ContentFileTypeMap map = ensureContentTypeManager();
		return map.getContentType(filename);
	}
262482
protected function detectEncodingOfFile($filename)
    {
        $encoding = '';
        $content = file_get_contents($filename);
        if ($content !== false) {
            $encoding = mb_detect_encoding($content, 'UTF-8, ISO-8859-1', true);
        }

        return $encoding;
    }
627793
private String getTemplateName(CmsObject cms, CmsResource resource) throws CmsException {

        String templateName = (String)(m_templateNameCache.getCachedObject(cms, resource.getRootPath()));
        if (templateName == null) {
            CmsProperty nameProperty = cms.readPropertyObject(
                resource,
                CmsPropertyDefinition.PROPERTY_TEMPLATE_ELEMENTS,
                false);
            String nameFromProperty = "";
            if (!nameProperty.isNullProperty()) {
                nameFromProperty = nameProperty.getValue();
            }
            m_templateNameCache.putCachedObject(cms, resource.getRootPath(), nameFromProperty);
            return nameFromProperty;
        } else {
            return templateName;
        }
    }
694499
public String getDescription(CmsObject cms) {

        if (m_description == null) {
            if (m_element.element(PARAM_DESCRIPTION) != null) {
                m_description = m_element.element(PARAM_DESCRIPTION).getText();
            }
            if (CmsStringUtil.isEmptyOrWhitespaceOnly(m_description)) {
                m_description = getParameterValue(cms, PARAM_DESCRIPTION);
                m_description = CmsEncoder.unescape(m_description, CmsEncoder.ENCODING_UTF_8);
            }
        }
        return m_description;
    }
635366
public static boolean isXmlContent(CmsResource resource) {

        boolean result = false;
        if (resource != null) {
            // avoid array index out of bound exception:
            if (!resource.isFolder()) {
                result = OpenCms.getResourceManager().getResourceType(resource) instanceof CmsResourceTypeXmlContent;
            }
        }
        return result;
    }
1075005
public static int getDialogAction(CmsObject cms) {

        String fileName = CmsResource.getName(cms.getRequestContext().getUri());
        if (fileName == null) {
            // file name could not be determined, return "see locked subresources" action
            return TYPE_LOCKS;
        } else if (fileName.equalsIgnoreCase("lock.jsp")) {
            // a "lock" action is requested
            return TYPE_LOCK;
        } else if (fileName.indexOf("change") != -1) {
            // a "steal lock" action is requested
            return TYPE_LOCKCHANGE;
        } else if (fileName.indexOf("unlock") != -1) {
            // an "unlock" action is requested
            return TYPE_UNLOCK;
        } else {
            // an "see locked subresources" action is requested
            return TYPE_LOCKS;
        }
    }
205157
protected Locale getLocaleToUse(CmsObject cms, CmsXmlContent xmlContent) {

        Locale contextLocale = cms.getRequestContext().getLocale();
        if (xmlContent.hasLocale(contextLocale)) {
            return contextLocale;
        }
        Locale defaultLocale = CmsLocaleManager.getDefaultLocale();
        if (xmlContent.hasLocale(defaultLocale)) {
            return defaultLocale;
        }
        if (!xmlContent.getLocales().isEmpty()) {
            return xmlContent.getLocales().get(0);
        } else {
            return defaultLocale;
        }
    }
1117257
public static String guessEncoding(byte[] content) {
        String encoding;
        try {
            encoding = guessEncodingByNutch(content);
        } catch (Exception ex) {
            return guessEncodingByMozilla(content);
        }

        if (encoding == null) {
            encoding = guessEncodingByMozilla(content);
            return encoding;
        } else {
            return encoding;
        }
    }
552707
private String readFile(String path) {

        String result = (String)CmsVfsMemoryObjectCache.getVfsMemoryObjectCache().getCachedObject(m_cms, path);
        if (result == null) {
            try {
                CmsFile file = m_cms.readFile(path);
                result = getContentAsString(file);
            } catch (CmsException e) {
                throw new RuntimeException(e);
            }
        }
        if (result != null) {
            CmsVfsMemoryObjectCache.getVfsMemoryObjectCache().putCachedObject(m_cms, path, result);
        }
        return result;

    }
152139
public List<Locale> getAvailableLocales(CmsObject cms, CmsResource resource) {

        String availableNames = null;
        try {
            availableNames = cms.readPropertyObject(
                resource,
                CmsPropertyDefinition.PROPERTY_AVAILABLE_LOCALES,
                true).getValue();
        } catch (CmsException exc) {
            LOG.debug("Could not read available locales property for resource " + resource.getRootPath(), exc);
        }

        List<Locale> result = null;
        if (availableNames != null) {
            result = getAvailableLocales(availableNames);
        }
        if ((result == null) || (result.size() == 0)) {
            return Collections.unmodifiableList(m_availableLocales);
        } else {
            return result;
        }
    }
840007
protected static byte[] getHistoricalResourceContent(CmsObject cms, String resource, String version) {

        if (CmsStringUtil.isNotEmpty(resource) && CmsStringUtil.isNotEmpty(version)) {
            // try to load the historical resource
            I_CmsHistoryResource res = null;
            String storedSiteRoot = cms.getRequestContext().getSiteRoot();
            try {
                cms.getRequestContext().setSiteRoot("/");
                res = cms.readResource(
                    cms.readResource(resource, CmsResourceFilter.ALL).getStructureId(),
                    Integer.parseInt(version));
            } catch (CmsException e) {
                // can usually be ignored
                if (LOG.isInfoEnabled()) {
                    LOG.info(e.getLocalizedMessage());
                }
                return "".getBytes();
            } finally {
                cms.getRequestContext().setSiteRoot(storedSiteRoot);
            }
            if (res.isFile()) {
                byte[] historyResourceContent = ((CmsFile)res).getContents();
                if ((historyResourceContent == null) || (historyResourceContent.length == 0)) {
                    try {
                        CmsFile file = cms.readFile((CmsResource)res);
                        historyResourceContent = file.getContents();
                    } catch (CmsException e) {
                        // ignore
                    }
                }
                historyResourceContent = CmsEncoder.changeEncoding(
                    historyResourceContent,
                    OpenCms.getSystemInfo().getDefaultEncoding(),
                    cms.getRequestContext().getEncoding());
                return historyResourceContent;
            }
        }
        return "".getBytes();
    }
325137
private byte[] replaceInContent(CmsFile file, byte[] contents) throws Exception {

        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(m_settings.getLocale())) {
            Locale contentLocale = CmsLocaleManager.getMainLocale(getCms(), file);
            if (!contentLocale.toString().equalsIgnoreCase(m_settings.getLocale())) {
                // content does not match the requested locale, skip it
                getReport().println(
                    Messages.get().container(Messages.RPT_SOURCESEARCH_NOT_MATCHED_0),
                    I_CmsReport.FORMAT_NOTE);
                return null;
            }
        }

        String encoding = CmsLocaleManager.getResourceEncoding(getCms(), file);
        String content = new String(contents, encoding);

        if (CmsSourceSearchForm.REGEX_ALL.equals(m_settings.getSearchpattern()) & !m_replace) {
            m_matchedResources.add(file);
            getReport().print(Messages.get().container(Messages.RPT_SOURCESEARCH_MATCHED_0), I_CmsReport.FORMAT_OK);
            return null;
        }

        Matcher matcher = Pattern.compile(m_settings.getSearchpattern()).matcher(content);

        if (matcher.find()) {
            // search pattern did match here, so take this file in the list with matches resources
            m_matchedResources.add(file);
            getReport().print(Messages.get().container(Messages.RPT_SOURCESEARCH_MATCHED_0), I_CmsReport.FORMAT_OK);
            if (m_replace) {
                if (m_settings.getType().equals(SearchType.renameContainer)) {

                    return renameNestedContainers(
                        file,
                        m_settings.getElementResource(),
                        m_settings.getReplacepattern().split(";")[0],
                        m_settings.getReplacepattern().split(";")[1]);
                }
                return matcher.replaceAll(m_settings.getReplacepattern()).getBytes(encoding);
            }
        } else {
            // search pattern did not match
            getReport().print(
                Messages.get().container(Messages.RPT_SOURCESEARCH_NOT_MATCHED_0),
                I_CmsReport.FORMAT_NOTE);
        }
        return null;
    }
607638
protected byte[] getFileBytes(String filename) {

        try {
            // is this a zip-file?
            if (m_importZip != null) {
                // yes
                ZipEntry entry = m_importZip.getEntry(filename);

                // path to file might be relative, too
                if ((entry == null) && filename.startsWith("/")) {
                    entry = m_importZip.getEntry(filename.substring(1));
                }
                if (entry == null) {
                    throw new ZipException(
                        Messages.get().getBundle().key(Messages.LOG_IMPORTEXPORT_FILE_NOT_FOUND_IN_ZIP_1, filename));
                }

                InputStream stream = m_importZip.getInputStream(entry);
                int size = new Long(entry.getSize()).intValue();
                return CmsFileUtil.readFully(stream, size);
            } else {
                // no - use directory
                File file = new File(m_importResource, filename);
                return CmsFileUtil.readFile(file);
            }
        } catch (FileNotFoundException fnfe) {
            if (LOG.isErrorEnabled()) {
                LOG.error(Messages.get().getBundle().key(Messages.ERR_IMPORTEXPORT_FILE_NOT_FOUND_1, filename), fnfe);
            }
            m_report.println(fnfe);
        } catch (IOException ioe) {
            if (LOG.isErrorEnabled()) {
                LOG.error(
                    Messages.get().getBundle().key(Messages.ERR_IMPORTEXPORT_ERROR_READING_FILE_1, filename),
                    ioe);
            }
            m_report.println(ioe);
        }
        // this will only be returned in case there was an exception
        return "".getBytes();
    }
713399
@SuppressWarnings("resource")
    public static byte[] readFile(String filename) throws IOException {

        // create input and output stream
        InputStream in = CmsFileUtil.class.getClassLoader().getResourceAsStream(filename);
        if (in == null) {
            throw new FileNotFoundException(filename);
        }

        return readFully(in);
    }