Search in sources :

Example 1 with CmsResourceTypeXmlContent

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

the class CmsLinkRewriter method rewriteContent.

/**
 * Rewrites the links included in the content itself.<p>
 *
 * @param file the file for which the links should be replaced
 * @param relations the original relations
 *
 * @throws CmsException if something goes wrong
 */
protected void rewriteContent(CmsFile file, Collection<CmsRelation> relations) throws CmsException {
    LOG.info("Rewriting in-content links for " + file.getRootPath());
    CmsPair<String, String> contentAndEncoding = decode(file);
    String content = "";
    if (OpenCms.getResourceManager().getResourceType(file) instanceof CmsResourceTypeXmlContent) {
        CmsXmlContent contentXml = CmsXmlContentFactory.unmarshal(m_cms, file);
        try {
            contentXml.validateXmlStructure(new CmsXmlEntityResolver(m_cms));
        } catch (CmsException e) {
            LOG.info("XML content was corrected automatically for resource " + file.getRootPath());
            contentXml.setAutoCorrectionEnabled(true);
            contentXml.correctXmlStructure(m_cms);
            try {
                content = new String(contentXml.marshal(), contentAndEncoding.getSecond());
            } catch (UnsupportedEncodingException e1) {
            // 
            }
        }
    }
    if (content.isEmpty()) {
        content = contentAndEncoding.getFirst();
    }
    String encodingForSave = contentAndEncoding.getSecond();
    String newContent = rewriteContentString(content);
    byte[] newContentBytes;
    try {
        newContentBytes = newContent.getBytes(encodingForSave);
    } catch (UnsupportedEncodingException e) {
        newContentBytes = newContent.getBytes();
    }
    file.setContents(newContentBytes);
    m_cms.writeFile(file);
}
Also used : CmsXmlContent(org.opencms.xml.content.CmsXmlContent) CmsXmlEntityResolver(org.opencms.xml.CmsXmlEntityResolver) CmsResourceTypeXmlContent(org.opencms.file.types.CmsResourceTypeXmlContent) CmsException(org.opencms.main.CmsException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 2 with CmsResourceTypeXmlContent

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

the class CmsLinkRewriter method rewriteLinks.

/**
 * Starts the link rewriting process.<p>
 *
 * @throws CmsException if something goes wrong
 */
public void rewriteLinks() throws CmsException {
    init();
    List<CmsRelation> relationsToCorrect = findRelationsFromTargetToSource();
    // group relations by the structure id of their source
    Multimap<CmsUUID, CmsRelation> relationsBySourceId = ArrayListMultimap.create();
    for (CmsRelation relation : relationsToCorrect) {
        LOG.info("Found relation which needs to be corrected: " + relation.getSourcePath() + " -> " + relation.getTargetPath() + " [" + relation.getType().getName() + "]");
        relationsBySourceId.put(relation.getSourceId(), relation);
    }
    // make sure we have a lock on the target folder before doing any write operations
    CmsLock lock = m_cms.getLock(m_targetPath);
    if (lock.isUnlocked() || !lock.isOwnedBy(m_cms.getRequestContext().getCurrentUser())) {
        // fail if locked by another user
        m_cms.lockResource(m_targetPath);
    }
    for (CmsUUID structureId : relationsBySourceId.keySet()) {
        Collection<CmsRelation> relationsForResource = relationsBySourceId.get(structureId);
        CmsResource resource = null;
        try {
            resource = getResource(structureId);
            rewriteLinks(resource, relationsForResource);
        } catch (Exception e) {
            LOG.error(e.getLocalizedMessage(), e);
        }
    }
    if (!m_rewriteAllXmlContents) {
        return;
    }
    for (Map.Entry<CmsUUID, CmsResource> entry : m_cachedResources.entrySet()) {
        CmsUUID key = entry.getKey();
        CmsResource resource = entry.getValue();
        if (isInTargets(resource.getRootPath()) && !m_rewrittenContent.contains(key)) {
            I_CmsResourceType resType = OpenCms.getResourceManager().getResourceType(resource.getTypeId());
            // rewrite content for other files so
            if (resType instanceof A_CmsResourceTypeLinkParseable) {
                try {
                    CmsFile file = m_cms.readFile(resource);
                    if (resType instanceof CmsResourceTypeXmlContent) {
                        CmsXmlContent content = CmsXmlContentFactory.unmarshal(m_cms, file);
                        try {
                            content.validateXmlStructure(new CmsXmlEntityResolver(m_cms));
                        } catch (CmsException e) {
                            LOG.info("XML content was corrected automatically for resource " + file.getRootPath());
                            content.setAutoCorrectionEnabled(true);
                            content.correctXmlStructure(m_cms);
                            file.setContents(content.marshal());
                        }
                    }
                    m_cms.writeFile(file);
                } catch (CmsException e) {
                    LOG.error(e.getLocalizedMessage(), e);
                }
            }
        }
    }
    copyLocaleRelations();
}
Also used : CmsResourceTypeXmlContent(org.opencms.file.types.CmsResourceTypeXmlContent) CmsRelation(org.opencms.relations.CmsRelation) CmsLock(org.opencms.lock.CmsLock) CmsLoaderException(org.opencms.loader.CmsLoaderException) CmsXmlException(org.opencms.xml.CmsXmlException) CmsIllegalArgumentException(org.opencms.main.CmsIllegalArgumentException) CmsException(org.opencms.main.CmsException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) I_CmsResourceType(org.opencms.file.types.I_CmsResourceType) CmsXmlContent(org.opencms.xml.content.CmsXmlContent) CmsXmlEntityResolver(org.opencms.xml.CmsXmlEntityResolver) CmsException(org.opencms.main.CmsException) A_CmsResourceTypeLinkParseable(org.opencms.file.types.A_CmsResourceTypeLinkParseable) CmsUUID(org.opencms.util.CmsUUID) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with CmsResourceTypeXmlContent

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

the class CmsDisplayAction method executeAction.

/**
 * @see org.opencms.ui.actions.I_CmsWorkplaceAction#executeAction(org.opencms.ui.I_CmsDialogContext)
 */
public void executeAction(I_CmsDialogContext context) {
    if (context.getResources().size() == 1) {
        CmsResource resource = context.getResources().get(0);
        if (OpenCms.getResourceManager().getResourceType(resource).getTypeName().equals(CmsListManager.RES_TYPE_LIST_CONFIG)) {
            CmsAppWorkplaceUi.get().showApp(OpenCms.getWorkplaceAppManager().getAppConfiguration(CmsListManagerConfiguration.APP_ID), A_CmsWorkplaceApp.addParamToState("", CmsEditor.RESOURCE_ID_PREFIX, resource.getStructureId().toString()));
        } else {
            CmsObject cms = context.getCms();
            try {
                cms = OpenCms.initCmsObject(cms);
                cms.getRequestContext().setUri(cms.getSitePath(resource));
            } catch (CmsException e) {
            // It's ok, we stick to the original context.
            }
            boolean isOnline = cms.getRequestContext().getCurrentProject().isOnlineProject();
            String link;
            if (isOnline && !(CmsStringUtil.isEmptyOrWhitespaceOnly(cms.getRequestContext().getSiteRoot()) || OpenCms.getSiteManager().isSharedFolder(cms.getRequestContext().getSiteRoot()))) {
                // use the online link only in case the current site is not the root site or the shared folder
                link = OpenCms.getLinkManager().getOnlineLink(cms, cms.getSitePath(resource));
            } else {
                link = OpenCms.getLinkManager().substituteLink(cms, resource);
            }
            if (isOnline || !(OpenCms.getResourceManager().getResourceType(resource) instanceof CmsResourceTypeXmlContent)) {
                A_CmsUI.get().openPageOrWarn(link, ONLINE_WINDOW_NAME);
            } else {
                A_CmsUI.get().getPage().setLocation(link);
            }
        }
    }
}
Also used : CmsObject(org.opencms.file.CmsObject) CmsResource(org.opencms.file.CmsResource) CmsException(org.opencms.main.CmsException) CmsResourceTypeXmlContent(org.opencms.file.types.CmsResourceTypeXmlContent)

Example 4 with CmsResourceTypeXmlContent

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

the class CmsEditContentAction method getVisibility.

/**
 * @see org.opencms.ui.actions.A_CmsWorkplaceAction#getVisibility(org.opencms.ui.I_CmsDialogContext)
 */
@Override
public CmsMenuItemVisibilityMode getVisibility(I_CmsDialogContext context) {
    Set<String> validContexts = Sets.newHashSet();
    for (AdeContext adecontext : Arrays.asList(AdeContext.gallery, AdeContext.resourceinfo)) {
        validContexts.add(adecontext.name());
    }
    if (!validContexts.contains(context.getAppId())) {
        return CmsMenuItemVisibilityMode.VISIBILITY_INVISIBLE;
    }
    List<CmsResource> resources = context.getResources();
    CmsResource resource = resources.get(0);
    I_CmsResourceType type = OpenCms.getResourceManager().getResourceType(resource);
    if ((type instanceof CmsResourceTypeXmlContent) && !CmsResourceTypeXmlContainerPage.isContainerPage(resource)) {
        return CmsStandardVisibilityCheck.DEFAULT.getSingleVisibility(context.getCms(), resource);
    } else {
        return CmsMenuItemVisibilityMode.VISIBILITY_INVISIBLE;
    }
}
Also used : I_CmsResourceType(org.opencms.file.types.I_CmsResourceType) CmsResource(org.opencms.file.CmsResource) CmsResourceTypeXmlContent(org.opencms.file.types.CmsResourceTypeXmlContent) AdeContext(org.opencms.gwt.shared.CmsCoreData.AdeContext)

Example 5 with CmsResourceTypeXmlContent

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

the class CmsXmlContentRepairDialog method getXmlContentResourceTypes.

/**
 * Returns the selector widget options to build a XML content resource type selector widget.<p>
 *
 * @return the selector widget options to build a XML content resource type selector widget
 */
private List getXmlContentResourceTypes() {
    // get all available resource types and filter XML content resource types
    List resTypes = OpenCms.getResourceManager().getResourceTypes();
    Iterator i = resTypes.iterator();
    List resTypeNames = new ArrayList(resTypes.size());
    while (i.hasNext()) {
        I_CmsResourceType resType = (I_CmsResourceType) i.next();
        if (!(resType instanceof CmsResourceTypeXmlContent)) {
            // this is not XML content resource type, skip it
            continue;
        }
        if (!resType.getTypeName().equals(TYPE_XMLCONTENT)) {
            resTypeNames.add(resType.getTypeName());
        }
    }
    // create the selector options
    List result = new ArrayList(resTypeNames.size() + 2);
    // add empty "please select" option to selector
    result.add(new CmsSelectWidgetOption("", true, key(Messages.GUI_XMLCONTENTREPAIR_DIALOG_RESTYPE_SELECT_0)));
    // sort the resource type names alphabetically
    Collections.sort(resTypeNames);
    i = resTypeNames.iterator();
    while (i.hasNext()) {
        // add all resource type names to the selector
        String resTypeName = (String) i.next();
        result.add(new CmsSelectWidgetOption(resTypeName));
    }
    // add option for generic XML content without "own" resource types at the end
    result.add(new CmsSelectWidgetOption(TYPE_XMLCONTENT, false, key(Messages.GUI_XMLCONTENTREPAIR_DIALOG_RESTYPE_GENERIC_0)));
    return result;
}
Also used : I_CmsResourceType(org.opencms.file.types.I_CmsResourceType) CmsSelectWidgetOption(org.opencms.widgets.CmsSelectWidgetOption) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) CmsResourceTypeXmlContent(org.opencms.file.types.CmsResourceTypeXmlContent) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

CmsResourceTypeXmlContent (org.opencms.file.types.CmsResourceTypeXmlContent)31 I_CmsResourceType (org.opencms.file.types.I_CmsResourceType)25 CmsResource (org.opencms.file.CmsResource)18 CmsException (org.opencms.main.CmsException)17 ArrayList (java.util.ArrayList)7 CmsContainerElementBean (org.opencms.xml.containerpage.CmsContainerElementBean)7 UnsupportedEncodingException (java.io.UnsupportedEncodingException)6 I_CmsEditHandler (org.opencms.workplace.editors.directedit.I_CmsEditHandler)6 Locale (java.util.Locale)5 CmsResourceTypeXmlPage (org.opencms.file.types.CmsResourceTypeXmlPage)5 CmsFile (org.opencms.file.CmsFile)4 CmsObject (org.opencms.file.CmsObject)4 CmsUUID (org.opencms.util.CmsUUID)4 CmsExplorerTypeSettings (org.opencms.workplace.explorer.CmsExplorerTypeSettings)4 CmsResourceUtil (org.opencms.workplace.explorer.CmsResourceUtil)4 CmsXmlContent (org.opencms.xml.content.CmsXmlContent)4 HashMap (java.util.HashMap)3 CmsProperty (org.opencms.file.CmsProperty)3 CmsResourceTypeImage (org.opencms.file.types.CmsResourceTypeImage)3 CmsLock (org.opencms.lock.CmsLock)3