Builds the diff view for the XML text.<p>

@param cms the CMS context
@param file1 the first file
@param file2 the second file

@return the diff view
197799
private Component buildWholeFileDiffView(CmsObject cms, CmsFile file1, CmsFile file2) {

        String encoding = "UTF-8";
        try {
            CmsXmlContent content1 = CmsXmlContentFactory.unmarshal(cms, file1);
            encoding = content1.getEncoding();
        } catch (CmsException e) {
            String rootPath = file1.getRootPath();
            LOG.error(
                "Could not unmarshal file " + rootPath + " for determining encoding: " + e.getLocalizedMessage(),
                e);
        }
        String text1 = decode(file1.getContents(), encoding);
        String text2 = decode(file2.getContents(), encoding);
        CmsTextDiffPanel diffPanel = new CmsTextDiffPanel(text1, text2, false, true);
        return diffPanel;

    }
169971
private void initXmlBundle() throws CmsException {

        CmsFile file = m_cms.readFile(m_resource);
        m_bundleFiles.put(null, m_resource);
        m_xmlBundle = CmsXmlContentFactory.unmarshal(m_cms, file);
        initKeySetForXmlBundle();

    }
71307
protected CmsJlanNetworkFile nextFile() {

        if (!hasMoreFiles()) {
            return null;
        }
        CmsJlanNetworkFile file = m_files.get(m_position);
        m_position += 1;
        return file;
    }
690255
private CmsElementView initViews(CmsUUID startId) {

        Map<CmsUUID, CmsElementView> viewMap = OpenCms.getADEManager().getElementViews(A_CmsUI.getCmsObject());
        List<CmsElementView> viewList = new ArrayList<CmsElementView>(viewMap.values());
        Collections.sort(viewList, new Comparator<CmsElementView>() {

            public int compare(CmsElementView arg0, CmsElementView arg1) {

                return ComparisonChain.start().compare(arg0.getOrder(), arg1.getOrder()).result();
            }

        });
        getViewSelector().setItemCaptionMode(ItemCaptionMode.EXPLICIT);
        m_typeHelper = createTypeHelper();
        m_typeHelper.precomputeTypeLists(
            A_CmsUI.getCmsObject(),
            m_folderResource.getRootPath(),
            A_CmsUI.getCmsObject().getRequestContext().removeSiteRoot(m_folderResource.getRootPath()),
            viewList,
            null);

        // also collect types in LinkedHashMap to preserve order and ensure uniqueness
        LinkedHashMap<String, CmsResourceTypeBean> allTypes = Maps.newLinkedHashMap();

        for (CmsElementView view : viewList) {

            if (view.hasPermission(A_CmsUI.getCmsObject(), m_folderResource)) {

                List<CmsResourceTypeBean> typeBeans = m_typeHelper.getPrecomputedTypes(view);

                if (typeBeans.isEmpty()) {
                    continue;
                }
                for (CmsResourceTypeBean typeBean : typeBeans) {
                    allTypes.put(typeBean.getType(), typeBean);
                }
                getViewSelector().addItem(view.getId());
                getViewSelector().setItemCaption(
                    view.getId(),
                    view.getTitle(A_CmsUI.getCmsObject(), A_CmsUI.get().getLocale()));
            }
        }
        getViewSelector().addItem(VIEW_ALL.getId());
        getViewSelector().setItemCaption(VIEW_ALL.getId(), CmsVaadinUtils.getMessageText(Messages.GUI_VIEW_ALL_0));
        m_allTypes = Lists.newArrayList(allTypes.values());
        if (allTypes.size() <= 8) {
            startId = ID_VIEW_ALL;
        }
        if (getViewSelector().getItem(startId) == null) {
            startId = (CmsUUID)(getViewSelector().getItemIds().iterator().next());
        }

        getViewSelector().addValueChangeListener(new ValueChangeListener() {

            private static final long serialVersionUID = 1L;

            public void valueChange(ValueChangeEvent event) {

                CmsUUID viewId = (CmsUUID)(event.getProperty().getValue());
                CmsElementView selectedView;
                if (viewId.equals(ID_VIEW_ALL)) {
                    selectedView = VIEW_ALL;
                } else {
                    selectedView = OpenCms.getADEManager().getElementViews(A_CmsUI.getCmsObject()).get(
                        event.getProperty().getValue());
                }
                init(selectedView, useDefault());
                if (selectedView != VIEW_ALL) {
                    VaadinService.getCurrentRequest().getWrappedSession().setAttribute(
                        SETTING_STANDARD_VIEW,
                        (event.getProperty().getValue()));
                }
            }
        });
        if (startId.equals(ID_VIEW_ALL)) {
            return VIEW_ALL;
        } else {
            return OpenCms.getADEManager().getElementViews(A_CmsUI.getCmsObject()).get(startId);
        }
    }
361208
public static CmsExtractionResult extractXmlContent(CmsObject cms, CmsResource resource, I_CmsSearchIndex index)
    throws CmsException {

        return extractXmlContent(cms, resource, index, null);
    }
180912
private void createNewContainerElements(CmsObject cms, CmsXmlContainerPage page, String sitePath)
    throws CmsException {

        CmsADEConfigData configData = OpenCms.getADEManager().lookupConfiguration(cms, cms.addSiteRoot(sitePath));
        Locale contentLocale = OpenCms.getLocaleManager().getDefaultLocale(cms, CmsResource.getFolderPath(sitePath));
        CmsObject cloneCms = OpenCms.initCmsObject(cms);
        cloneCms.getRequestContext().setLocale(contentLocale);
        CmsContainerPageBean pageBean = page.getContainerPage(cms);
        boolean needsChanges = false;
        List<CmsContainerBean> updatedContainers = new ArrayList<CmsContainerBean>();
        for (CmsContainerBean container : pageBean.getContainers().values()) {
            List<CmsContainerElementBean> updatedElements = new ArrayList<CmsContainerElementBean>();
            for (CmsContainerElementBean element : container.getElements()) {
                if (element.isCreateNew() && !element.isGroupContainer(cms) && !element.isInheritedContainer(cms)) {
                    needsChanges = true;
                    String typeName = OpenCms.getResourceManager().getResourceType(element.getResource()).getTypeName();
                    CmsResourceTypeConfig typeConfig = configData.getResourceType(typeName);
                    if (typeConfig == null) {
                        throw new IllegalArgumentException(
                            "Can not copy template model element '"
                                + element.getResource().getRootPath()
                                + "' because the resource type '"
                                + typeName
                                + "' is not available in this sitemap.");
                    }

                    CmsResource newResource = typeConfig.createNewElement(
                        cloneCms,
                        element.getResource(),
                        CmsResource.getParentFolder(cms.getRequestContext().addSiteRoot(sitePath)));
                    CmsContainerElementBean newBean = new CmsContainerElementBean(
                        newResource.getStructureId(),
                        element.getFormatterId(),
                        element.getIndividualSettings(),
                        false);
                    updatedElements.add(newBean);
                } else {
                    updatedElements.add(element);
                }
            }

            CmsContainerBean updatedContainer = new CmsContainerBean(
                container.getName(),
                container.getType(),
                container.getParentInstanceId(),
                container.isRootContainer(),
                container.getMaxElements(),
                updatedElements);
            updatedContainers.add(updatedContainer);
        }
        if (needsChanges) {
            CmsContainerPageBean updatedPage = new CmsContainerPageBean(updatedContainers);
            page.writeContainerPage(cms, updatedPage);
        }
    }
1278103
public void setCms(CmsObject cms) {

        m_cms = cms;
        m_request = cms.getRequestContext();
        m_referenceProject = null;
        m_projectResources = null;
        m_messages = null;
    }
1339585
public static String renderTemplate(
        CmsObject cms,
        String template,
        CmsResource content,
        Map<String, Object> contextObjects) {

        return renderTemplate(cms, template, new CmsJspContentAccessBean(cms, content), contextObjects);
    }
282561
protected void doLoadNextFile() throws CmsException {

        super.doLoadNextResource();
        if (m_resource == null) {
            return;
        }

        // upgrade the resource to a file
        CmsFile file = m_cms.readFile(m_resource);

        // unmarshal the XML content from the resource, don't use unmarshal(CmsObject, CmsResource)
        // as no support for getting the historic version that has been cached by a CmsHistoryResourceHandler
        // will come from there!
        m_content = CmsXmlContentFactory.unmarshal(m_cms, file, pageContext.getRequest());

        // check if locale is available
        m_contentLocale = m_locale;
        if (!m_content.hasLocale(m_contentLocale)) {
            Iterator<Locale> it = OpenCms.getLocaleManager().getDefaultLocales().iterator();
            while (it.hasNext()) {
                Locale locale = it.next();
                if (m_content.hasLocale(locale)) {
                    // found a matching locale
                    m_contentLocale = locale;
                    break;
                }
            }
        }
    }
1100259
public static boolean contentEqualsIgnoreEOL(File file1, File file2, Charset charset) throws IORuntimeException {
		boolean file1Exists = file1.exists();
		if (file1Exists != file2.exists()) {
			return false;
		}

		if (!file1Exists) {
			// 两个文件都不存在，返回true
			return true;
		}

		if (file1.isDirectory() || file2.isDirectory()) {
			// 不比较目录
			throw new IORuntimeException("Can't compare directories, only files");
		}

		if (equals(file1, file2)) {
			// 同一个文件
			return true;
		}

		Reader input1 = null;
		Reader input2 = null;
		try {
			input1 = getReader(file1, charset);
			input2 = getReader(file2, charset);
			return IoUtil.contentEqualsIgnoreEOL(input1, input2);
		} finally {
			IoUtil.close(input1);
			IoUtil.close(input2);
		}
	}
482716
public void load(CmsObject cms, CmsResource resource) throws CmsException {

        CmsFile file = cms.readFile(resource);
        CmsObject rootCms = OpenCms.initCmsObject(cms);
        rootCms.getRequestContext().setSiteRoot("");
        CmsXmlContent content = CmsXmlContentFactory.unmarshal(cms, file);
        Locale en = new Locale("en");
        for (I_CmsXmlContentValue value : content.getValues(N_INCLUDE, en)) {
            String include = value.getStringValue(rootCms);
            if (!CmsStringUtil.isEmpty(include)) {
                m_includes.add(include);
            }
        }
        for (I_CmsXmlContentValue value : content.getValues(N_EXCLUDE, en)) {
            String exclude = value.getStringValue(rootCms);
            if (!CmsStringUtil.isEmpty(exclude)) {
                m_excludes.add(exclude);
            }
        }
        I_CmsXmlContentValue robotsValue = content.getValue(N_MODE, en);
        m_mode = robotsValue.getStringValue(rootCms);

        I_CmsXmlContentValue robotsTxtTextValue = content.getValue(N_ROBOTS_TXT_TEXT, en);
        if (robotsTxtTextValue != null) {
            m_robotsTxtText = robotsTxtTextValue.getStringValue(rootCms);
        }

        I_CmsXmlContentValue computeContPageDates = content.getValue(N_COMPUTE_CONTAINER_PAGE_DATES, en);
        if (computeContPageDates != null) {
            m_computeContainerPageDates = Boolean.parseBoolean(computeContPageDates.getStringValue(rootCms));
        }

        I_CmsXmlContentValue generatorClassValue = content.getValue(N_GENERATOR_CLASS, en);
        if (generatorClassValue != null) {
            m_generatorClassName = generatorClassValue.getStringValue(rootCms);
        }

        I_CmsXmlContentValue cacheValue = content.getValue(N_CACHE, en);
        if (cacheValue != null) {
            m_useCache = Boolean.parseBoolean(cacheValue.getStringValue(rootCms));
        }

        I_CmsXmlContentValue serverUrlValue = content.getValue(N_SERVER_URL, en);
        if (serverUrlValue != null) {
            m_serverUrl = serverUrlValue.getStringValue(rootCms);
        }
    }
339955
public static <E> Set<E> differenceView(final Set<E> set1, final Set<?> set2) {
		return Sets.difference(set1, set2);
	}
1056711
public static Diff compareXML(String control, Reader test)
        throws SAXException, IOException {
        return new Diff(new StringReader(control), test);
    }
625246
private byte[] replaceInXml(CmsFile cmsFile) throws Exception {

        Exception e = null;
        CmsXmlContent xmlContent = CmsXmlContentFactory.unmarshal(getCms(), cmsFile);
        Pattern pattern = Pattern.compile(m_settings.getSearchpattern());
        // loop over the locales of the content
        boolean modified = false;
        boolean matched = false;
        String requestedLocale = m_settings.getLocale();
        for (Locale locale : xmlContent.getLocales()) {
            if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(requestedLocale)
                && !locale.toString().equalsIgnoreCase(requestedLocale)) {
                // does not match the requested locale, skip it
                continue;
            }
            // loop over the available element paths of the current content locale
            List<String> paths = xmlContent.getNames(locale);
            for (String xpath : paths) {
                // try to get the value extraction for the current element path
                I_CmsXmlContentValue value = xmlContent.getValue(xpath, locale);
                if (value.isSimpleType()) {
                    try {
                        String currPath = value.getPath();
                        if (CmsStringUtil.isEmptyOrWhitespaceOnly(m_settings.getXpath())
                            || currPath.equals(m_settings.getXpath())
                            || (CmsXmlUtils.removeXpath(currPath).equals(m_settings.getXpath()))) {
                            // xpath match
                            String oldVal = value.getStringValue(getCms());
                            Matcher matcher = pattern.matcher(oldVal);
                            matcher = Pattern.compile(m_settings.getSearchpattern()).matcher(oldVal);
                            if (matcher.find()) {
                                matched = true;
                                m_matchedResources.add(cmsFile);
                                if (m_replace) {
                                    String newVal = matcher.replaceAll(m_settings.getReplacepattern());
                                    if (!oldVal.equals(newVal)) {
                                        value.setStringValue(getCms(), newVal);
                                        modified = true;
                                    }
                                }
                            }
                        }
                    } catch (Exception ex) {
                        // log and go on
                        LOG.error(ex.getMessage(), ex);
                        e = ex;
                    }
                }
            }
        }
        if (e != null) {
            throw e;
        }
        if (matched) {
            getReport().println(Messages.get().container(Messages.RPT_SOURCESEARCH_MATCHED_0), I_CmsReport.FORMAT_OK);
        } else {
            getReport().println(
                Messages.get().container(Messages.RPT_SOURCESEARCH_NOT_MATCHED_0),
                I_CmsReport.FORMAT_NOTE);
        }
        if (modified) {
            return xmlContent.marshal();
        }
        return null;
    }
1342764
public void parse(CmsFile file) throws CmsException {

        CmsXmlContent content = CmsXmlContentFactory.unmarshal(m_cms, file);
        for (Locale locale : content.getLocales()) {
            m_currentLocale = locale;
            CmsXmlContentRootLocation rootLocation = new CmsXmlContentRootLocation(content, locale);
            parseConfigurationGroup(rootLocation);
        }
    }
857961
public CmsResource createFolder(CmsObject cms) throws CmsException {

        if (m_folder != null) {
            return m_folder;
        }
        return cms.createResource(
            m_path,
            OpenCms.getResourceManager().getResourceType(CmsResourceTypeFolder.RESOURCE_TYPE_NAME).getTypeId());
    }
1111891
public static String formatContentValueForDiffTable(
        CmsObject cms,
        CmsElementComparison comparison,
        String origValue) {

        String result = CmsStringUtil.substitute(CmsStringUtil.trimToSize(origValue, 60), "\n", "");

        // formatting DateTime
        if (comparison instanceof CmsXmlContentElementComparison) {
            if (((CmsXmlContentElementComparison)comparison).getType().equals(CmsXmlDateTimeValue.TYPE_NAME)) {
                if (CmsStringUtil.isNotEmpty(result)) {

                    result = CmsDateUtil.getDateTime(
                        new Date(Long.parseLong(result)),
                        DateFormat.SHORT,
                        cms.getRequestContext().getLocale());
                }
            }
        }
        return result;
    }