📄 cmsnewresourcelink.java
字号:
/*
* File : $Source: /usr/local/cvs/opencms/src/com/opencms/workplace/CmsNewResourceLink.java,v $
* Date : $Date: 2004/01/07 10:57:09 $
* Version: $Revision: 1.35.2.1 $
*
* This library is part of OpenCms -
* the Open Source Content Mananagement System
*
* Copyright (C) 2001 The OpenCms Group
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* For further information about OpenCms, please see the
* OpenCms Website: http://www.opencms.org
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package com.opencms.workplace;
import com.opencms.core.CmsException;
import com.opencms.core.I_CmsConstants;
import com.opencms.core.I_CmsSession;
import com.opencms.file.CmsFile;
import com.opencms.file.CmsFolder;
import com.opencms.file.CmsLinkCheck;
import com.opencms.file.CmsObject;
import com.opencms.file.CmsResource;
import com.opencms.util.Encoder;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Map;
import java.util.Vector;
/**
* Template class for displaying the new resource screen for a new link
* of the OpenCms workplace.<P>
* Reads template files of the content type <code>CmsXmlWpTemplateFile</code>.
*
* @author Michael Emmerich
* @version $Revision: 1.35.2.1 $ $Date: 2004/01/07 10:57:09 $
*/
public class CmsNewResourceLink extends CmsWorkplaceDefault implements I_CmsWpConstants,I_CmsConstants {
private static final String C_PARA_KEEP_PROPERTIES = "keepproperties";
private static final String C_PARA_ADD_TO_NAV = "addtonav";
private static final int DEBUG = 0;
/**
* Overwrites the getContent method of the CmsWorkplaceDefault.<br>
* Gets the content of the new resource othertype template and processed the data input.
* @param cms The CmsObject.
* @param templateFile The lock template file
* @param elementName not used
* @param parameters Parameters of the request and the template.
* @param templateSelector Selector of the template tag to be displayed.
* @return Bytearry containing the processed data of the template.
* @throws Throws CmsException if something goes wrong.
*/
public byte[] getContent(CmsObject cms, String templateFile, String elementName,
Hashtable parameters, String templateSelector) throws CmsException {
String error = "";
boolean checkurl = true;
String filename = null;
String link = null;
String foldername = null;
String type = null;
I_CmsSession session = cms.getRequestContext().getSession(true);
// get the document to display
CmsXmlWpTemplateFile xmlTemplateDocument = new CmsXmlWpTemplateFile(cms, templateFile);
CmsXmlLanguageFile lang = xmlTemplateDocument.getLanguageFile();
// clear session values on first load
String initial = (String)parameters.get(C_PARA_INITIAL);
if(initial != null){
// remove all session values
session.removeValue(C_PARA_FILE);
session.removeValue(C_PARA_LINK);
session.removeValue(C_PARA_VIEWFILE);
session.removeValue(C_PARA_NAVPOS);
session.removeValue(C_PARA_NAVTEXT);
session.removeValue("lasturl");
}
// get the lasturl from parameters or from session
String lastUrl = getLastUrl(cms, parameters);
if(lastUrl != null){
session.putValue("lasturl", lastUrl);
}
// get the linkname and the linkurl
filename = cms.getRequestContext().getRequest().getParameter(C_PARA_FILE);
if(filename != null) {
filename = filename.trim();
session.putValue(C_PARA_FILE, filename);
} else {
// try to get the value from the session, e.g. after an error
filename = (String)session.getValue(C_PARA_FILE)!=null?(String)session.getValue(C_PARA_FILE):"";
}
link = cms.getRequestContext().getRequest().getParameter(C_PARA_LINK);
if(link != null) {
link = link.trim();
session.putValue(C_PARA_LINK, link);
} else {
// try to get the value from the session, e.g. after an error
link = (String)session.getValue(C_PARA_LINK)!=null?(String)session.getValue(C_PARA_LINK):"";
}
// get the parameters
String navtitle = (String)parameters.get(C_PARA_NAVTEXT);
if(navtitle != null) {
session.putValue(C_PARA_NAVTEXT, navtitle);
} else {
// try to get the value from the session, e.g. after an error
navtitle = (String)session.getValue(C_PARA_NAVTEXT)!=null?(String)session.getValue(C_PARA_NAVTEXT):"";
}
String navpos = (String)parameters.get(C_PARA_NAVPOS);
if(navpos != null) {
session.putValue(C_PARA_NAVPOS, navpos);
} else {
// try to get the value from the session, e.g. after an error
navpos = (String)session.getValue(C_PARA_NAVPOS)!=null?(String)session.getValue(C_PARA_NAVPOS):"";
}
String dummy = (String)parameters.get(CmsNewResourceLink.C_PARA_KEEP_PROPERTIES);
if (DEBUG>0) System.out.println( "parameter " + CmsNewResourceLink.C_PARA_KEEP_PROPERTIES + ": " + dummy );
boolean keepTargetProperties = false;
if (dummy!=null) {
session.putValue(CmsNewResourceLink.C_PARA_KEEP_PROPERTIES, dummy);
}
else {
dummy = (String)session.getValue(CmsNewResourceLink.C_PARA_KEEP_PROPERTIES)!=null?(String)session.getValue(CmsNewResourceLink.C_PARA_KEEP_PROPERTIES):"true";
}
keepTargetProperties = dummy.trim().equalsIgnoreCase("true");
dummy = (String)parameters.get(CmsNewResourceLink.C_PARA_ADD_TO_NAV);
if (DEBUG>0) System.out.println( "parameter " + CmsNewResourceLink.C_PARA_ADD_TO_NAV + ": " + dummy );
boolean addToNav = false;
if (dummy!=null) {
session.putValue(CmsNewResourceLink.C_PARA_ADD_TO_NAV, dummy);
}
else {
dummy = (String)session.getValue(CmsNewResourceLink.C_PARA_ADD_TO_NAV)!=null?(String)session.getValue(CmsNewResourceLink.C_PARA_ADD_TO_NAV):"false";
}
addToNav = dummy.trim().equalsIgnoreCase("true");
String notChange = (String)parameters.get("newlink");
CmsResource linkResource = null;
String step = cms.getRequestContext().getRequest().getParameter("step");
// set the values e.g. after an error
xmlTemplateDocument.setData("LINKNAME", filename);
xmlTemplateDocument.setData("LINKVALUE", link);
xmlTemplateDocument.setData("NAVTITLE", Encoder.escapeHtml(navtitle));
xmlTemplateDocument.setData("KEEPPROPERTIES", keepTargetProperties==true ? "true" : "false" );
xmlTemplateDocument.setData("ADDTONAV", addToNav==true ? "true" : "false" );
// if an existing link should be edited show the change page
if(notChange != null && notChange.equals("false") && step == null) {
try{
CmsFile currentFile = cms.readFile(filename);
String content = new String(currentFile.getContents());
xmlTemplateDocument.setData("LINKNAME", currentFile.getName());
xmlTemplateDocument.setData("LINK", currentFile.getAbsolutePath());
xmlTemplateDocument.setData("LINKVALUE", content);
templateSelector = "change";
} catch (CmsException e){
error = e.getShortException();
}
}
// get the current phase of this wizard
if(step != null) {
// step 1 - show the final selection screen
if(step.equals("1") || step.equals("2")) {
try{
// step 1 - create the link with checking http-link
// step 2 - create the link without link check
// get folder- and filename
foldername = (String)session.getValue(C_PARA_FILELIST);
if(foldername == null) {
foldername = cms.rootFolder().getAbsolutePath();
}
String title = lang.getLanguageValue("explorer.linkto") + " " + link;
type = "link";
if(notChange != null && notChange.equals("false")) {
// change old file
CmsFile editFile = cms.readFile(filename);
String oldLink = new String( editFile.getContents() );
editFile.setContents(link.getBytes());
if(step.equals("1")){
if(!link.startsWith("/")){
checkurl = CmsLinkCheck.checkUrl(link);
}
}
if(checkurl){
cms.writeFile(editFile);
cms.writeProperty(filename, C_PROPERTY_TITLE, title);
cms.doDecrementLinkCountForResource( oldLink );
cms.doIncrementLinkCountForResource( link );
cms.linkResourceToTarget( filename, link );
}
linkResource = (CmsResource)editFile;
} else {
// create the new file
Hashtable prop = new Hashtable();
prop.put(C_PROPERTY_TITLE, title);
if(step.equals("1")){
if(!link.startsWith("/")){
checkurl = CmsLinkCheck.checkUrl(link);
}
}
if(checkurl){
Map targetProperties = null;
if (keepTargetProperties) {
try {
targetProperties = cms.readProperties(link);
}
catch (Exception e) {}
}
linkResource = cms.createResource( foldername + filename, type, prop, link.getBytes(), targetProperties );
}
}
// now check if navigation informations have to be added to the new page.
if(addToNav && checkurl) {
cms.writeProperty(linkResource.getAbsolutePath(), C_PROPERTY_NAVTEXT, navtitle);
// update the navposition.
if(navpos != null) {
updateNavPos(cms, linkResource, navpos);
}
}
// remove values from session
session.removeValue(C_PARA_FILE);
session.removeValue(C_PARA_VIEWFILE);
session.removeValue(C_PARA_LINK);
session.removeValue(C_PARA_NAVTEXT);
session.removeValue(C_PARA_NAVPOS);
// now return to appropriate filelist
} catch (CmsException e){
error = e.getShortException();
}
if(checkurl && ("".equals(error.trim()))){
try {
if(lastUrl != null) {
cms.getRequestContext().getResponse().sendRedirect(lastUrl);
} else {
cms.getRequestContext().getResponse().sendCmsRedirect(getConfigFile(cms).getWorkplaceActionPath()
+ C_WP_EXPLORER_FILELIST);
}
} catch(Exception e) {
throw new CmsException("Redirect fails :" + getConfigFile(cms).getWorkplaceActionPath()
+ C_WP_EXPLORER_FILELIST, CmsException.C_UNKNOWN_EXCEPTION, e);
}
return null;
}
}
} else {
session.removeValue(C_PARA_FILE);
session.removeValue(C_PARA_VIEWFILE);
session.removeValue(C_PARA_LINK);
session.removeValue(C_PARA_NAVTEXT);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -