📄 cmsxmlcontenteditor.java
字号:
// now create the resource, fill it with the marshalled XML and write it back to the VFS
getCms().createResource(newFileName, templateFile.getTypeId());
// re-read the created resource
CmsFile newFile = getCms().readFile(newFileName, CmsResourceFilter.ALL);
newFile.setContents(newContent.marshal());
// write the file with the updated content
getCms().writeFile(newFile);
// wipe out parameters for the editor to ensure proper operation
setParamNewLink(null);
setParamAction(null);
setParamResource(newFileName);
setAction(ACTION_DEFAULT);
// create the temporary file to work with
setParamTempfile(createTempFile());
// set the member variables for the content
m_file = getCms().readFile(getParamTempfile(), CmsResourceFilter.ALL);
m_content = newContent;
} catch (CmsException e) {
if (LOG.isErrorEnabled()) {
LOG.error(Messages.get().getBundle().key(Messages.LOG_CREATE_XML_CONTENT_ITEM_1, m_paramNewLink), e);
}
throw new JspException(e);
} finally {
try {
// delete the new file
getCms().deleteResource(newFileName, CmsResource.DELETE_PRESERVE_SIBLINGS);
} catch (CmsException e) {
// ignore
}
}
}
/**
* Performs the preview xml content action in a new browser window.<p>
*
* @throws IOException if redirect fails
* @throws JspException if inclusion of error page fails
*/
public void actionPreview() throws IOException, JspException {
try {
// save content of the editor only to the temporary file
setEditorValues(getElementLocale());
writeContent();
// remove eventual release & expiration date from temporary file to make preview work
getCms().setDateReleased(getParamTempfile(), CmsResource.DATE_RELEASED_DEFAULT, false);
getCms().setDateExpired(getParamTempfile(), CmsResource.DATE_EXPIRED_DEFAULT, false);
} catch (CmsException e) {
// show error page
showErrorPage(this, e);
}
// get preview uri from content handler
String previewUri = m_content.getContentDefinition().getContentHandler().getPreview(
getCms(),
m_content,
getParamTempfile());
// create locale request parameter
StringBuffer param = new StringBuffer(8);
if (previewUri.indexOf('?') != -1) {
param.append("&");
} else {
param.append("?");
}
param.append(CmsLocaleManager.PARAMETER_LOCALE);
param.append("=");
param.append(getParamElementlanguage());
// redirect to the temporary file with currently active element language or to the specified preview uri
sendCmsRedirect(previewUri + param);
}
/**
* Performs the save content action.<p>
*
* @see org.opencms.workplace.editors.CmsEditor#actionSave()
*/
public void actionSave() throws JspException {
actionSave(getElementLocale());
if (getAction() != ACTION_CANCEL) {
// save successful, set save action
setAction(ACTION_SAVE);
}
}
/**
* Performs the save content action.<p>
*
* This is also used when changing the element language.<p>
*
* @param locale the locale to save the content
* @throws JspException if including the error page fails
*/
public void actionSave(Locale locale) throws JspException {
try {
setEditorValues(locale);
// check if content has errors
if (!getErrorHandler().hasErrors()) {
// no errors found, write content and copy temp file contents
writeContent();
commitTempFile();
// set the modified parameter
setParamModified(Boolean.TRUE.toString());
}
} catch (CmsException e) {
showErrorPage(e);
}
}
/**
* Adds an optional element to the xml content or removes an optional element from the xml content.<p>
*
* Depends on the given action value.<p>
*
* @throws JspException if including the error page fails
*/
public void actionToggleElement() throws JspException {
// set editor values from request
try {
setEditorValues(getElementLocale());
} catch (CmsXmlException e) {
// an error occured while trying to set the values, stop action
showErrorPage(e);
return;
}
// validate the content values
if (!getErrorHandler().hasErrors()) {
// get the necessary parameters to add/remove the element
int index = 0;
try {
index = Integer.parseInt(getParamElementIndex());
} catch (Exception e) {
// ignore, should not happen
}
if (getAction() == ACTION_ELEMENT_REMOVE) {
// remove the value
m_content.removeValue(getParamElementName(), getElementLocale(), index);
} else {
// add the new value after the clicked element
if (m_content.hasValue(getParamElementName(), getElementLocale())) {
// when other values are present, increase index to use right position
index += 1;
}
m_content.addValue(getCms(), getParamElementName(), getElementLocale(), index);
}
if (getErrorHandler().hasWarnings(getElementLocale())) {
// there were warnings for the edited content, reset error handler to avoid display issues
resetErrorHandler();
}
try {
// write the modified content to the temporary file
writeContent();
} catch (CmsException e) {
// an error occured while trying to save
showErrorPage(e);
}
}
}
/**
* Builds the html String for the element language selector.<p>
*
* This method has to use the resource request parameter because the temporary file is
* not available in the upper button frame.<p>
*
* @param attributes optional attributes for the <select> tag
* @return the html for the element language selectbox
*/
public String buildSelectElementLanguage(String attributes) {
return buildSelectElementLanguage(attributes, getParamResource(), getElementLocale());
}
/**
* @see org.opencms.widgets.I_CmsWidgetDialog#getButtonStyle()
*/
public int getButtonStyle() {
return getSettings().getUserSettings().getEditorButtonStyle();
}
/**
* @see org.opencms.workplace.editors.CmsEditor#getEditorResourceUri()
*/
public String getEditorResourceUri() {
return getSkinUri() + "editors/" + EDITOR_TYPE + "/";
}
/**
* Returns the current element locale.<p>
*
* @return the current element locale
*/
public Locale getElementLocale() {
if (m_elementLocale == null) {
if (CmsStringUtil.isNotEmpty(getParamElementlanguage()) && !"null".equals(getParamElementlanguage())) {
m_elementLocale = CmsLocaleManager.getLocale(getParamElementlanguage());
} else {
initElementLanguage();
m_elementLocale = CmsLocaleManager.getLocale(getParamElementlanguage());
}
}
return m_elementLocale;
}
/**
* @see org.opencms.widgets.I_CmsWidgetDialog#getHelpMessageIds()
*/
public Set getHelpMessageIds() {
if (m_helpMessageIds == null) {
m_helpMessageIds = new HashSet();
}
return m_helpMessageIds;
}
/**
* Returns the index of the element to add or remove.<p>
*
* @return the index of the element to add or remove
*/
public String getParamElementIndex() {
return m_paramElementIndex;
}
/**
* Returns the name of the element to add or remove.<p>
*
* @return the name of the element to add or remove
*/
public String getParamElementName() {
return m_paramElementName;
}
/**
* Returns the "new link" parameter.<p>
*
* @return the "new link" parameter
*/
public String getParamNewLink() {
return m_paramNewLink;
}
/**
* @see org.opencms.widgets.I_CmsWidgetDialog#getUserAgent()
*/
public String getUserAgent() {
return getJsp().getRequest().getHeader(CmsRequestUtil.HEADER_USER_AGENT);
}
/**
* Generates the HTML form for the XML content editor.<p>
*
* @return the HTML that generates the form for the XML editor
*/
public String getXmlEditorForm() {
// set "editor mode" attribute (required for link replacement in the root site)
getCms().getRequestContext().setAttribute(CmsRequestContext.ATTRIBUTE_EDITOR, new Boolean(true));
// add customized message bundle eventually specified in XSD of XML content
addMessages(m_content.getContentDefinition().getContentHandler().getMessages(getLocale()));
return getXmlEditorForm(m_content.getContentDefinition(), "", true).toString();
}
/**
* Generates the HTML for the end of the html editor form page.<p>
*
* @return the HTML for the end of the html editor form page
* @throws JspException if including the error page fails
*/
public String getXmlEditorHtmlEnd() throws JspException {
StringBuffer result = new StringBuffer(16384);
if (m_optionalElementPresent) {
// disabled optional element(s) present, reset widgets to show help bubbles on optional form entries
resetWidgetCollector();
}
try {
// get all widgets from collector
Iterator i = getWidgetCollector().getWidgets().keySet().iterator();
while (i.hasNext()) {
// get the value of the widget
String key = (String)i.next();
I_CmsXmlContentValue value = (I_CmsXmlContentValue)getWidgetCollector().getValues().get(key);
I_CmsWidget widget = (I_CmsWidget)getWidgetCollector().getWidgets().get(key);
result.append(widget.getDialogHtmlEnd(getCms(), this, (I_CmsWidgetParameter)value));
}
// add empty help text layer
result.append("<div class=\"help\" id=\"helpText\" ");
result.append("onmouseover=\"showHelpText();\" onmouseout=\"hideHelpText();\"></div>\n");
// add empty element button layer
result.append("<div class=\"xmlButtons\" id=\"xmlElementButtons\" ");
result.append("onmouseover=\"checkElementButtons(true);\" onmouseout=\"checkElementButtons(false);\"></div>\n");
// return the HTML
return result.toString();
} catch (Exception e) {
showErrorPage(e);
return "";
}
}
/**
* Generates the javascript includes for the used widgets in the editor form.<p>
*
* @return the javascript includes for the used widgets
* @throws JspException if including the error page fails
*/
public String getXmlEditorIncludes() throws JspException {
StringBuffer result = new StringBuffer(32);
try {
// iterate over unique widgets from collector
Iterator i = getWidgetCollector().getUniqueWidgets().iterator();
while (i.hasNext()) {
I_CmsWidget widget = (I_CmsWidget)i.next();
result.append(widget.getDialogIncludes(getCms(), this));
result.append("\n");
}
} catch (Exception e) {
showErrorPage(e);
}
return result.toString();
}
/**
* Generates the javascript initialization calls for the used widgets in the editor form.<p>
*
* @return the javascript initialization calls for the used widgets
* @throws JspException if including the error page fails
*/
public String getXmlEditorInitCalls() throws JspException {
StringBuffer result = new StringBuffer(32);
try {
// iterate over unique widgets from collector
Iterator i = getWidgetCollector().getUniqueWidgets().iterator();
while (i.hasNext()) {
I_CmsWidget widget = (I_CmsWidget)i.next();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -