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);
}
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();
}
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);
}
}
}
}
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;
}
}
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;
}
Aggregations