📄 cmshtmlimport.java
字号:
String folder = vfsFolderName.substring(path.length(), vfsFolderName.length());
try {
// try to find a meta.properties file in the folder
String propertyFileName = foldername + File.separator + META_PROPERTIES;
boolean metaPropertiesFound = false;
ExtendedProperties propertyFile = new ExtendedProperties();
try {
propertyFile.load(new FileInputStream(new File(propertyFileName)));
metaPropertiesFound = true;
} catch (Exception e1) {
// do nothing if the propertyfile could not be loaded since it is not required
// that such s file does exist
}
// now copy all values from the propertyfile to the already found properties of the
// new folder in OpenCms
// only do this if we have found a meta.properties file
if (metaPropertiesFound) {
Enumeration enu = propertyFile.keys();
String property = "";
while (enu.hasMoreElements()) {
// get property and value
try {
property = (String)enu.nextElement();
String propertyvalue = (String)propertyFile.get(property);
// copy to the properties of the OpenCms folder
properties.put(property, propertyvalue);
} catch (Exception e2) {
// just skip this property if it could ne be read.
e2.printStackTrace();
}
}
// check if we have to set the navpos property.
if (properties.get(CmsPropertyDefinition.PROPERTY_NAVPOS) == null) {
// set the position in the folder as navpos
// we have to add one to the postion, since it is counted from 0
properties.put(CmsPropertyDefinition.PROPERTY_NAVPOS, (position + 1) + "");
}
// check if we have to set the navpos property.
if (properties.get(CmsPropertyDefinition.PROPERTY_NAVTEXT) == null) {
// set the foldername in the folder as navtext
String navtext = folder.substring(1, 2).toUpperCase()
+ folder.substring(2, folder.length() - 1);
properties.put(CmsPropertyDefinition.PROPERTY_NAVTEXT, navtext);
}
} else {
// if there was no meta.properties file, no properties should be added to the
// folder
properties = new Hashtable();
}
// try to read the folder, it its there we must not create it again
try {
m_cmsObject.readFolder(path + folder);
m_cmsObject.lockResource(path + folder);
} catch (CmsException e1) {
// the folder was not there, so create it
m_cmsObject.createResource(path + folder, CmsResourceTypeFolder.getStaticTypeId());
}
// create all properties and put them in an ArrayList
Enumeration enu = properties.keys();
List propertyList = new ArrayList();
while (enu.hasMoreElements()) {
// get property and value
String propertyKey = (String)enu.nextElement();
String propertyVal = (String)properties.get(propertyKey);
CmsProperty property = new CmsProperty(propertyKey, propertyVal, propertyVal);
// create implicitly if Property doesn't exist already
property.setAutoCreatePropertyDefinition(true);
// add new property to the list
propertyList.add(property);
}
// try to write the property Objects
try {
m_cmsObject.writePropertyObjects(path + folder, propertyList);
} catch (CmsException e1) {
e1.printStackTrace();
}
m_report.println(
org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0),
I_CmsReport.FORMAT_OK);
} catch (CmsException e) {
m_report.println(e);
LOG.error(e);
}
}
}
/**
* Compares 2 pathes for the base part which have both equal.
*
* @param path1 the first path to compare
* @param path2 the second path to compare
* @return the base path of both which are equal
*/
private String getBasePath(String path1, String path2) {
StringBuffer base = new StringBuffer();
path1 = path1.replace('\\', '/');
path2 = path2.replace('\\', '/');
String[] parts1 = path1.split("/");
String[] parts2 = path2.split("/");
for (int i = 0; i < parts1.length; i++) {
if (i >= parts2.length) {
break;
}
if (parts1[i].equals(parts2[i])) {
base.append(parts1[i] + "/");
}
}
return base.toString();
}
/**
* Creates the filename of the file of the external link.
*
* @param link the link to get the file path for.
* @return the filename of the file for the external link.
*/
private String getExternalLinkFile(String link) {
String filename = link.substring(link.indexOf("://") + 3, link.length());
filename = m_cmsObject.getRequestContext().getFileTranslator().translateResource(filename.replace('/', '-'));
return filename;
}
/**
* Returns a byte array containing the content of server FS file.<p>
*
* @param file the name of the file to read
* @return bytes[] the content of the file
* @throws CmsException if something goes wrong
*/
private byte[] getFileBytes(File file) throws CmsException {
byte[] buffer = null;
FileInputStream fileStream = null;
int charsRead;
int size;
try {
fileStream = new FileInputStream(file);
charsRead = 0;
size = new Long(file.length()).intValue();
buffer = new byte[size];
while (charsRead < size) {
charsRead += fileStream.read(buffer, charsRead, size - charsRead);
}
return buffer;
} catch (IOException e) {
throw new CmsDbIoException(
Messages.get().container(Messages.ERR_GET_FILE_BYTES_1, file.getAbsolutePath()),
e);
} finally {
try {
if (fileStream != null) {
fileStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* Returns the OpenCms file type of a real filesystem file. <p>
*
* This is made by checking the extension.
*
* @param filename the name of the file in the real filesystem
*
* @return the id of the OpenCms file type
*
* @throws Exception if something goes wrong
*/
private int getFileType(String filename) throws Exception {
String extension = "";
if (filename.indexOf(".") > -1) {
extension = filename.substring((filename.lastIndexOf(".")));
}
String typename = (String)m_extensions.get(extension.toLowerCase());
if (typename == null) {
typename = "binary";
}
CmsResourceManager resourceManager = OpenCms.getResourceManager();
return resourceManager.getResourceType(typename).getTypeId();
}
/**
* Gets a valid VfsName form a given name in the real filesystem.<p>
*
* This name will ater be used for all link translations during the HTML-parsing process.
* @param relativeName the name in the real fielsystem, relative to the start folder
* @param name the name of the file
* @param isFile flag to indicate that the resource is a file
*
* @return a valid name in the VFS
*
* @throws Exception if something goes wrong
*/
private String getVfsName(String relativeName, String name, boolean isFile) throws Exception {
// first translate all fileseperators to the valid "/" in OpenCms
String vfsName = relativeName.replace('\\', '/');
// the resource is a file
if (isFile) {
// we must check if it might be copied into a gallery. this can be done by checking the
// file extension
int filetype = getFileType(name);
// there is no name before the ".extension"
if (name.indexOf(".") == 0) {
name = "unknown" + name;
int dot = relativeName.lastIndexOf(".");
relativeName = relativeName.substring(0, dot) + name;
}
// depending on the filetype, the resource must be moved into a special folder in
// OpenCms:
// images -> move into image gallery, if flag to leave at original location is off
// binary -> move into download gallery, if flag to leaave at original location is off
// plain -> move into destination folder
// other -> move into download gallery, if flag to leaave at original location is off
if ((CmsResourceTypeImage.getStaticTypeId() == filetype) && (!m_leaveImagesMode)) {
// move to image gallery
// as the image gallery is "flat", we must use the file name and not the complete
// relative name
vfsName = m_imageGallery + name;
} else if ((CmsResourceTypePlain.getStaticTypeId() == filetype)
|| (m_leaveImagesMode)
|| (m_leaveDownloadsMode)) {
// move to destination folder
//vfsName=m_destinationDir+relativeName;
// we have to check if there is a folder with the same name but without extension
// if so, we will move the file into the folder and name it "index.html"
String folderName = relativeName;
if (folderName.indexOf(".") > 0) {
folderName = folderName.substring(0, folderName.indexOf("."));
}
folderName = m_inputDir + "\\" + folderName;
File folder = new File(folderName);
if ((folder != null) && (folder.isDirectory())) {
vfsName = m_destinationDir + relativeName.substring(0, relativeName.indexOf(".")) + "/index.html";
// System.err.println("MOVING "+ relativeName + " -> " + name.substring(0,name.indexOf("."))+"/index.html");
} else {
// move to destination folder
vfsName = m_destinationDir + relativeName;
}
} else {
// everything else will be moved to the download gallery.
// as the download gallery is "flat", we must use the file name and not the complete
// relative name
vfsName = m_downloadGallery + name;
}
// now we have the filename in the VFS. its possible that a file with the same name
// is already existing, in this case, we have to adjust the filename.
return validateFilename(vfsName);
} else {
// folders are always moved to the destination folder
vfsName = m_destinationDir + vfsName + "/";
return vfsName;
}
}
/**
* Tests if a filename is an external name, i.e. this name does not point into the OpenCms Vfs.<p>
*
* A filename is an external name if it contains the string "://", e.g. "http://" or "ftp://"
* @param filename the filename to test
* @return true or false
*/
private boolean isExternal(String filename) {
boolean external = false;
if (filename.indexOf("://") > 0) {
external = true;
}
return external;
}
/**
* Checks if m_element is valid element.<p>
*
* @return true if element is valid, otherwise false
*/
private boolean isValidElement() {
boolean validElement = false;
List elementList = new ArrayList();
try {
// get Elements of template stored in Property "template-elements"
String elements = m_cms.property(CmsPropertyDefinition.PROPERTY_TEMPLATE_ELEMENTS, m_template);
// template may contain more than one Element
// Elements are seperated by the delimiter ","
if (elements != null) {
StringTokenizer T = new StringTokenizer(elements, ",");
while (T.hasMoreTokens()) {
// current element probably looks like "body*|Body" <name><mandatory>|<nicename>
String currentElement = T.nextToken();
int sepIndex = currentElement.indexOf("|");
if (sepIndex != -1) {
// current element == "body*"
currentElement = currentElement.substring(0, sepIndex);
}
if (currentElement.endsWith("*")) {
// current element == "body"
currentElement = currentElement.substring(0, currentElement.length() - 1);
}
elementList.add(currentElement);
}
}
if (elementList.contains(m_element)) {
validElement = true;
}
} catch (Exception e) {
e.printStackTrace();
}
return validElement;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -