use of org.opencms.file.types.CmsResourceTypeXmlPage in project opencms-core by alkacon.
the class CmsResourceWrapperXmlPage method writeFile.
/**
* @see org.opencms.file.wrapper.A_CmsResourceWrapper#writeFile(org.opencms.file.CmsObject, org.opencms.file.CmsFile)
*/
@Override
public CmsFile writeFile(CmsObject cms, CmsFile resource) throws CmsException {
CmsResource xmlPage = cms.readResource(resource.getStructureId());
// CmsResource xmlPage = findXmlPage(cms, resource.getRootPath());
if (xmlPage != null) {
I_CmsResourceType resType = OpenCms.getResourceManager().getResourceType(xmlPage.getTypeId());
if (resType instanceof CmsResourceTypeXmlPage) {
String path = getSubPath(cms, xmlPage, cms.getRequestContext().removeSiteRoot(resource.getRootPath()));
CmsFile file = cms.readFile(xmlPage);
String[] tokens = path.split("/");
if (tokens.length == 2) {
CmsXmlPage xml = CmsXmlPageFactory.unmarshal(cms, file);
// cut off the html suffix
String name = tokens[1];
if (name.endsWith("." + EXTENSION_ELEMENT)) {
name = name.substring(0, name.length() - 5);
}
// set content
String content = getStringValue(cms, file, resource.getContents());
content = CmsStringUtil.extractHtmlBody(content).trim();
xml.setStringValue(cms, name, CmsLocaleManager.getLocale(tokens[0]), content);
// write file
file.setContents(xml.marshal());
cms.writeFile(file);
}
return file;
}
}
return null;
}
use of org.opencms.file.types.CmsResourceTypeXmlPage in project opencms-core by alkacon.
the class CmsResourceComparisonDialog method displayDialog.
/**
* Display method for two list dialogs.<p>
*
* @throws Exception if something goes wrong
*/
public void displayDialog() throws Exception {
CmsResourceInfoDialog fileInfo = new CmsResourceInfoDialog(getJsp()) {
@Override
protected String defaultActionHtmlEnd() {
return "";
}
};
fileInfo.displayDialog(true);
if (fileInfo.isForwarded()) {
return;
}
CmsPropertyComparisonList propertyDiff = new CmsPropertyComparisonList(getJsp());
CmsAttributeComparisonList attributeDiff = new CmsAttributeComparisonList(getJsp());
List<A_CmsListDialog> lists = new ArrayList<A_CmsListDialog>();
lists.add(attributeDiff);
I_CmsResourceType resourceType = OpenCms.getResourceManager().getResourceType(propertyDiff.getResourceType());
if ((resourceType instanceof CmsResourceTypeXmlContent) || (resourceType instanceof CmsResourceTypeXmlPage)) {
// display attributes, properties and compared elements
CmsElementComparisonList contentDiff = new CmsElementComparisonList(getJsp());
// get the content of the resource1 and resource2
byte[] content1 = readFile(getCms(), propertyDiff.getResource1().getStructureId(), getParamVersion1()).getContents();
byte[] content2 = readFile(getCms(), propertyDiff.getResource2().getStructureId(), getParamVersion2()).getContents();
// display the content comparison only if both files has contents
if ((content1.length > 0) && (content2.length > 0)) {
lists.add(contentDiff);
}
lists.add(propertyDiff);
CmsMultiListDialog threeLists = new CmsMultiListDialog(lists);
// perform the active list actions
threeLists.displayDialog(true);
// write the dialog just if no list has been forwarded
if (threeLists.isForwarded()) {
return;
}
fileInfo.writeDialog();
threeLists.writeDialog();
} else if (resourceType instanceof CmsResourceTypeImage) {
// display attributes, properties and images
lists.add(propertyDiff);
CmsMultiListDialog twoLists = new CmsMultiListDialog(lists) {
/**
* @see org.opencms.workplace.list.CmsMultiListDialog#defaultActionHtmlEnd()
*/
@Override
public String defaultActionHtmlEnd() {
return "";
}
};
twoLists.displayDialog(true);
if (twoLists.isForwarded()) {
return;
}
CmsImageComparisonDialog images = new CmsImageComparisonDialog(getJsp());
fileInfo.writeDialog();
twoLists.writeDialog();
// this is very dangerous
// it is possible that here a forward is tried, what should not be sence we already wrote to the output stream.
// CmsImageComparisonDialog should implement isForwarded, writeDialog and displayDialog(boolean) methods
images.displayDialog();
} else if (resourceType instanceof CmsResourceTypePointer) {
lists.add(propertyDiff);
CmsMultiListDialog twoLists = new CmsMultiListDialog(lists) {
/**
* @see org.opencms.workplace.list.CmsMultiListDialog#defaultActionHtmlEnd()
*/
@Override
public String defaultActionHtmlEnd() {
return "";
}
};
twoLists.displayDialog(true);
if (twoLists.isForwarded()) {
return;
}
CmsPointerComparisonDialog pointers = new CmsPointerComparisonDialog(getJsp());
fileInfo.writeDialog();
twoLists.writeDialog();
// same as for CmsImageComparisonDialog
pointers.displayDialog();
} else if (propertyDiff.getResource1().isFile()) {
// display attributes and properties
lists.add(propertyDiff);
CmsMultiListDialog twoLists = new CmsMultiListDialog(lists);
twoLists.displayDialog(true);
if (twoLists.isForwarded()) {
return;
}
CmsResource resource1 = propertyDiff.getResource1();
CmsResource resource2 = propertyDiff.getResource2();
String path1 = resource1.getRootPath();
String path2 = resource2.getRootPath();
byte[] content1 = readFile(getCms(), resource1.getStructureId(), getParamVersion1()).getContents();
byte[] content2 = readFile(getCms(), resource2.getStructureId(), getParamVersion2()).getContents();
String originalSource = null;
String copySource = null;
I_CmsTextExtractor textExtractor = null;
// only if both files have contents
if ((content1.length > 0) && (content2.length > 0)) {
if (path1.endsWith(".pdf") && path2.endsWith(".pdf")) {
textExtractor = CmsExtractorPdf.getExtractor();
} else if (path1.endsWith(".doc") && path2.endsWith(".doc")) {
textExtractor = CmsExtractorMsOfficeOLE2.getExtractor();
} else if (path1.endsWith(".xls") && path2.endsWith(".xls")) {
textExtractor = CmsExtractorMsOfficeOLE2.getExtractor();
} else if (path1.endsWith(".rtf") && path2.endsWith(".rtf")) {
textExtractor = CmsExtractorRtf.getExtractor();
} else if (path1.endsWith(".ppt") && path2.endsWith(".ppt")) {
textExtractor = CmsExtractorMsOfficeOLE2.getExtractor();
}
}
if (textExtractor != null) {
try {
// extract the content
originalSource = textExtractor.extractText(content1).getContent();
copySource = textExtractor.extractText(content2).getContent();
} catch (Exception e) {
// something goes wrong on extracting content
// set the content to null, so the content dialog will not be shown
originalSource = null;
copySource = null;
LOG.error(e.getMessage(), e);
}
} else if ((resourceType instanceof CmsResourceTypePlain) || (resourceType instanceof CmsResourceTypeJsp)) {
originalSource = new String(content1);
copySource = new String(content2);
}
fileInfo.writeDialog();
twoLists.writeDialog();
if (CmsStringUtil.isNotEmpty(originalSource) && CmsStringUtil.isNotEmpty(copySource)) {
m_differenceDialog.setCopySource(copySource);
m_differenceDialog.setOriginalSource(originalSource);
// same as for CmsImageComparisonDialog
m_differenceDialog.displayDialog();
}
} else {
// display attributes and properties
lists.add(propertyDiff);
CmsMultiListDialog twoLists = new CmsMultiListDialog(lists);
twoLists.displayDialog(true);
if (twoLists.isForwarded()) {
return;
}
fileInfo.writeDialog();
twoLists.writeDialog();
}
}
use of org.opencms.file.types.CmsResourceTypeXmlPage in project opencms-core by alkacon.
the class CmsTextDiff method diff.
/**
* @see org.opencms.ui.dialogs.history.diff.I_CmsDiffProvider#diff(org.opencms.file.CmsObject, org.opencms.gwt.shared.CmsHistoryResourceBean, org.opencms.gwt.shared.CmsHistoryResourceBean)
*/
public Optional<Component> diff(CmsObject cms, CmsHistoryResourceBean v1, CmsHistoryResourceBean v2) throws CmsException {
CmsResource resource1 = A_CmsAttributeDiff.readResource(cms, v1);
String encoding = CmsLocaleManager.getResourceEncoding(cms, resource1);
I_CmsResourceType type = OpenCms.getResourceManager().getResourceType(resource1);
if ((type instanceof CmsResourceTypeXmlContent) || (type instanceof CmsResourceTypePlain) || (type instanceof CmsResourceTypeJsp) || (type instanceof CmsResourceTypeXmlPage) || (type instanceof CmsResourceTypePointer) || (type instanceof CmsResourceTypeBinary)) {
CmsResource resource2 = A_CmsAttributeDiff.readResource(cms, v2);
String path1 = resource1.getRootPath();
String path2 = resource2.getRootPath();
CmsFile file1 = cms.readFile(resource1);
CmsFile file2 = cms.readFile(resource2);
byte[] content1 = file1.getContents();
byte[] content2 = file2.getContents();
String originalSource = null;
String copySource = null;
I_CmsTextExtractor textExtractor = null;
// only if both files have contents
if ((content1.length > 0) && (content2.length > 0)) {
if (path1.endsWith(".pdf") && path2.endsWith(".pdf")) {
textExtractor = CmsExtractorPdf.getExtractor();
} else if (path1.endsWith(".doc") && path2.endsWith(".doc")) {
textExtractor = CmsExtractorMsOfficeOLE2.getExtractor();
} else if (path1.endsWith(".xls") && path2.endsWith(".xls")) {
textExtractor = CmsExtractorMsOfficeOLE2.getExtractor();
} else if (path1.endsWith(".rtf") && path2.endsWith(".rtf")) {
textExtractor = CmsExtractorRtf.getExtractor();
} else if (path1.endsWith(".ppt") && path2.endsWith(".ppt")) {
textExtractor = CmsExtractorMsOfficeOLE2.getExtractor();
}
}
if (textExtractor != null) {
try {
// extract the content
originalSource = textExtractor.extractText(content1).getContent();
copySource = textExtractor.extractText(content2).getContent();
} catch (Exception e) {
// something goes wrong on extracting content
// set the content to null, so the content dialog will not be shown
originalSource = null;
copySource = null;
LOG.error(e.getMessage(), e);
}
} else if ((type instanceof CmsResourceTypePlain) || (type instanceof CmsResourceTypeJsp) || (type instanceof CmsResourceTypePointer)) {
try {
originalSource = new String(content1, encoding);
copySource = new String(content2, encoding);
} catch (UnsupportedEncodingException e) {
LOG.error(e.getLocalizedMessage(), e);
}
}
if ((copySource == null) || (originalSource == null)) {
return Optional.absent();
}
try {
CmsTextDiffPanel diffPanel = new CmsTextDiffPanel(originalSource, copySource, false, true);
diffPanel.setWidth("100%");
Panel panel = new Panel(CmsVaadinUtils.getMessageText(Messages.GUI_HISTORY_DIALOG_TEXT_COMPARISON_CAPTION_0));
panel.setWidth("100%");
VerticalLayout vl = new VerticalLayout();
vl.setMargin(true);
vl.addComponent(diffPanel);
panel.setContent(vl);
return Optional.<Component>fromNullable(panel);
} catch (Exception e) {
LOG.error(e.getLocalizedMessage(), e);
return Optional.absent();
}
} else {
return Optional.absent();
}
}
use of org.opencms.file.types.CmsResourceTypeXmlPage in project opencms-core by alkacon.
the class CmsResourceWrapperXmlPage method findXmlPage.
/**
* Returns the {@link CmsResource} of the xml page which belongs to
* the given resource name (full path).<p>
*
* It works up the path till a resource for the path exists in the VFS.
* If the found resource is a xml page, this resource is returned. If
* the path does not belong to a xml page <code>null</code> will be returned.<p>
*
* @param cms the initialized CmsObject
* @param resourcename the name of the resource (full path) to check
*
* @return the found resource of type xml page or null if not found
*/
private CmsResource findXmlPage(CmsObject cms, String resourcename) {
// get the full folder path of the resource to start from
String path = cms.getRequestContext().removeSiteRoot(resourcename);
// the path without the trailing slash
// for example: .../xmlpage.xml/ -> .../xmlpagepage.xml
String reducedPath = CmsFileUtil.removeTrailingSeparator(path);
do {
// check if a resource without the trailing shalsh exists
boolean existResource = cms.existsResource(reducedPath);
// check if the current folder exists
if (cms.existsResource(path) || existResource) {
// prove if a resource without the trailing slash does exist
if (existResource) {
// a resource without the trailing slash does exist, so take the path without the trailing slash
path = reducedPath;
}
try {
CmsResource res = cms.readResource(path);
I_CmsResourceType resType = OpenCms.getResourceManager().getResourceType(res.getTypeId());
if (resType instanceof CmsResourceTypeXmlPage) {
return res;
} else {
break;
}
} catch (CmsException ex) {
break;
}
} else {
// folder does not exist, go up one folder
path = CmsResource.getParentFolder(path);
if (path.endsWith("/")) {
path = path.substring(0, path.length() - 1);
}
}
if (CmsStringUtil.isEmpty(path)) {
// site root or root folder reached and no folder found
break;
}
} while (true);
return null;
}
use of org.opencms.file.types.CmsResourceTypeXmlPage in project opencms-core by alkacon.
the class CmsObjectWrapper method needUtf8Marker.
/**
* Checks if the resource type needs an UTF-8 marker.<p>
*
* If the encoding of the resource is "UTF-8" and the resource
* type is one of the following:<br/>
* <ul>
* <li>{@link CmsResourceTypeJsp}</li>
* <li>{@link CmsResourceTypePlain}</li>
* <li>{@link CmsResourceTypeXmlContent}</li>
* <li>{@link CmsResourceTypeXmlPage}</li>
* </ul>
*
* it needs an UTF-8 marker.<p>
*
* @param res the resource to check if the content needs a UTF-8 marker
*
* @return <code>true</code> if the resource needs an UTF-8 maker otherwise <code>false</code>
*/
private boolean needUtf8Marker(CmsResource res) {
if (!m_addByteOrderMark) {
return false;
}
// if the encoding of the resource is not UTF-8 return false
String encoding = CmsLocaleManager.getResourceEncoding(m_cms, res);
boolean result = false;
if (CmsEncoder.ENCODING_UTF_8.equals(encoding)) {
try {
I_CmsResourceType resType = OpenCms.getResourceManager().getResourceType(res.getTypeId());
if (resType instanceof CmsResourceTypeJsp) {
result = true;
} else if (resType instanceof CmsResourceTypePlain) {
result = true;
} else if (resType instanceof CmsResourceTypeXmlContent) {
result = true;
} else if (resType instanceof CmsResourceTypeXmlPage) {
result = true;
}
} catch (CmsLoaderException e) {
LOG.debug(e.getLocalizedMessage(), e);
}
}
return result;
}
Aggregations