📄 cmshtmlimport.java
字号:
*
* This should only be done when the import has been done.
*/
private void clear() {
m_fileIndex = null;
m_externalLinks = null;
m_imageInfo = null;
}
/**
* Copies all HTML files to the VFS.<p>
*
* @param startfolder startfolder the folder to start with
*
* @throws Exception if something goes wrong
*/
private void copyHtmlFiles(String startfolder) throws Exception {
try {
File folder = new File(startfolder);
// get all subresources
File[] subresources = folder.listFiles();
// now loop through all subresources
for (int i = 0; i < subresources.length; i++) {
// if the subresource is a folder, get all subresources of it as well
if (subresources[i].isDirectory()) {
// first, create the folder in the VFS
Hashtable properties = new Hashtable();
createFolder(subresources[i].getAbsolutePath(), i, properties);
// now process all rescources inside of the folder
copyHtmlFiles(subresources[i].getAbsolutePath());
} else {
// create a new file in the VFS
String vfsFileName = (String)m_fileIndex.get(subresources[i].getAbsolutePath().replace('\\', '/'));
// check if this is an Html file, do only import and parse those
int type = getFileType(vfsFileName);
if (CmsResourceTypePlain.getStaticTypeId() == type) {
Hashtable properties = new Hashtable();
// the subresource is a file, so start the parsing process
String content = new String();
try {
content = parseHtmlFile(subresources[i], properties);
} catch (CmsException e) {
m_report.println(e);
}
properties.put("template", m_template);
// create the file in the VFS
createFile(subresources[i].getAbsolutePath(), i, content, properties);
}
}
}
} catch (Exception e) {
LOG.error(e);
}
}
/**
* Copies all files except HTML files to the VFS.<p>
*
* @param startfolder startfolder the folder to start with
*/
private void copyOtherFiles(String startfolder) {
try {
File folder = new File(startfolder);
// get all subresources
File[] subresources = folder.listFiles();
// now loop through all subresources
for (int i = 0; i < subresources.length; i++) {
// if the subresource is a folder, get all subresources of it as well
if (subresources[i].isDirectory()) {
copyOtherFiles(subresources[i].getAbsolutePath());
} else {
// do not import the "meta.properties" file
if (!subresources[i].getName().equals(META_PROPERTIES)) {
// create a new file in the VFS
String vfsFileName = (String)m_fileIndex.get(subresources[i].getAbsolutePath().replace(
'\\',
'/'));
// get the file type of the FS file
int type = getFileType(vfsFileName);
if (CmsResourceTypePlain.getStaticTypeId() != type) {
if (isExternal(vfsFileName)) {
m_report.print(
Messages.get().container(Messages.RPT_SKIP_EXTERNAL_0),
I_CmsReport.FORMAT_NOTE);
m_report.print(org.opencms.report.Messages.get().container(
org.opencms.report.Messages.RPT_ARGUMENT_1,
subresources[i]));
m_report.print(org.opencms.report.Messages.get().container(
org.opencms.report.Messages.RPT_DOTS_0));
m_report.print(
Messages.get().container(Messages.RPT_ARROW_RIGHT_0),
I_CmsReport.FORMAT_NOTE);
m_report.println(org.opencms.report.Messages.get().container(
org.opencms.report.Messages.RPT_ARGUMENT_1,
vfsFileName));
} else {
m_report.print(
Messages.get().container(Messages.RPT_IMPORT_0),
I_CmsReport.FORMAT_NOTE);
m_report.print(org.opencms.report.Messages.get().container(
org.opencms.report.Messages.RPT_ARGUMENT_1,
vfsFileName));
m_report.print(org.opencms.report.Messages.get().container(
org.opencms.report.Messages.RPT_DOTS_0));
// get the content of the FS file
byte[] content = getFileBytes(subresources[i]);
// get the filename from the fileIndex list
// check if there are some image infos stored for this resource
List properties = new ArrayList();
String altText = (String)m_imageInfo.get(subresources[i].getAbsolutePath().replace(
'\\',
'/'));
CmsProperty property1 = new CmsProperty(
CmsPropertyDefinition.PROPERTY_DESCRIPTION,
altText,
altText);
CmsProperty property2 = new CmsProperty(
CmsPropertyDefinition.PROPERTY_TITLE,
altText,
altText);
// add them to the title and description property
if (altText != null) {
properties.add(property1);
properties.add(property2);
}
// create the file
if (!m_overwriteMode) {
m_cmsObject.createResource(vfsFileName, type, content, properties);
} else {
try {
CmsLock lock = m_cmsObject.getLock(vfsFileName);
if (lock.getType() != CmsLock.TYPE_EXCLUSIVE) {
m_cmsObject.lockResource(vfsFileName);
}
m_cmsObject.deleteResource(
vfsFileName,
CmsResource.DELETE_PRESERVE_SIBLINGS);
} catch (CmsException e) {
// the file did not exist, so create it
} finally {
m_cmsObject.createResource(vfsFileName, type, content, properties);
}
m_report.print(
Messages.get().container(Messages.RPT_OVERWRITE_0),
I_CmsReport.FORMAT_NOTE);
m_report.print(org.opencms.report.Messages.get().container(
org.opencms.report.Messages.RPT_DOTS_0));
}
m_report.println(org.opencms.report.Messages.get().container(
org.opencms.report.Messages.RPT_OK_0), I_CmsReport.FORMAT_OK);
}
}
}
}
}
} catch (Exception e) {
LOG.error(e);
m_report.println(e);
}
}
/**
* Creates all external links, which were found during the HTML-page processing.<p>
*
*/
private void createExternalLinks() {
// loop through all links
Iterator i = m_externalLinks.iterator();
while (i.hasNext()) {
String linkUrl = (String)i.next();
String filename = linkUrl.substring(linkUrl.indexOf("://") + 3, linkUrl.length());
filename = m_cmsObject.getRequestContext().getFileTranslator().translateResource(filename.replace('/', '-'));
m_report.print(Messages.get().container(Messages.RPT_CREATE_EXTERNAL_LINK_0), I_CmsReport.FORMAT_NOTE);
m_report.print(org.opencms.report.Messages.get().container(
org.opencms.report.Messages.RPT_ARGUMENT_1,
filename));
m_report.print(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0));
List properties = new ArrayList();
CmsProperty property1 = new CmsProperty(
CmsPropertyDefinition.PROPERTY_TITLE,
"Link to " + linkUrl,
"Link to " + linkUrl);
properties.add(property1);
try {
m_cmsObject.createResource(
m_linkGallery + filename,
CmsResourceTypePointer.getStaticTypeId(),
linkUrl.getBytes(),
properties);
} catch (CmsException e) {
// do nothing here, an exception will be thrown if this link already exisits
}
m_report.println(
org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0),
I_CmsReport.FORMAT_OK);
}
}
/**
* Creates a file in the VFS.<p>
*
* @param filename the complete filename in the real file system
* @param position the default nav pos of this folder
* @param content the html content of the file
* @param properties the file properties
*/
private void createFile(String filename, int position, String content, Hashtable properties) {
String vfsFileName = (String)m_fileIndex.get(filename.replace('\\', '/'));
if (vfsFileName != null) {
try {
m_report.print(Messages.get().container(Messages.RPT_CREATE_FILE_0), I_CmsReport.FORMAT_NOTE);
m_report.print(org.opencms.report.Messages.get().container(
org.opencms.report.Messages.RPT_ARGUMENT_1,
vfsFileName));
m_report.print(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0));
// check if we have to set the navpos property.
if ((properties.get(CmsPropertyDefinition.PROPERTY_NAVPOS) == null)
&& (properties.get(CmsPropertyDefinition.PROPERTY_NAVTEXT) != 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) + "");
}
// create new xml page
CmsXmlPage page = new CmsXmlPage(m_locale, OpenCms.getSystemInfo().getDefaultEncoding());
page.addValue(m_element, m_locale);
page.setStringValue(m_cmsObject, m_element, m_locale, content);
// check links
CmsLinkTable linkTable = page.getLinkTable(m_element, m_locale);
Iterator i = linkTable.iterator();
while (i.hasNext()) {
CmsLink link = (CmsLink)i.next();
String target = link.getTarget();
// do only update internal links
if (target.indexOf("://") == 0) {
//if (!target.startsWith("http") && !target.startsWith("mailto")) {
target = m_cmsObject.getRequestContext().getFileTranslator().translateResource(target);
// update link
link.updateLink(target, link.getAnchor(), link.getQuery());
}
}
// marshal xml page and get the content
byte[] contentByteArray = page.marshal();
List oldProperties = new ArrayList();
if (!m_overwriteMode) {
m_cmsObject.createResource(
vfsFileName,
CmsResourceTypeXmlPage.getStaticTypeId(),
contentByteArray,
new ArrayList());
} else {
try {
// try if the file is there
oldProperties = m_cmsObject.readPropertyObjects(vfsFileName, false);
CmsLock lock = m_cmsObject.getLock(vfsFileName);
if (lock.getType() != CmsLock.TYPE_EXCLUSIVE) {
m_cmsObject.lockResource(vfsFileName);
}
m_cmsObject.deleteResource(vfsFileName, CmsResource.DELETE_PRESERVE_SIBLINGS);
} catch (CmsException e) {
// the file did not exist, so we do not have to delete it
} finally {
// create the new resource
m_report.print(Messages.get().container(Messages.RPT_OVERWRITE_0), I_CmsReport.FORMAT_NOTE);
m_report.print(org.opencms.report.Messages.get().container(
org.opencms.report.Messages.RPT_DOTS_0));
m_cmsObject.createResource(
vfsFileName,
CmsResourceTypeXmlPage.getStaticTypeId(),
contentByteArray,
new ArrayList());
}
}
// create all properties and put them in an ArrayList
Enumeration en = properties.keys();
List propertyList = new ArrayList();
while (en.hasMoreElements()) {
// get property and value
String propertyKey = (String)en.nextElement();
String propertyVal = (String)properties.get(propertyKey);
// create new Property Object
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
try {
m_cmsObject.writePropertyObjects(vfsFileName, propertyList);
// write the old properties if available
m_cmsObject.writePropertyObjects(vfsFileName, oldProperties);
} 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);
}
}
}
/**
* Creates a folder in the VFS.<p>
*
* @param foldername the complete foldername in the real file system
* @param position the default nav pos of this folder
* @param properties the file properties
*/
private void createFolder(String foldername, int position, Hashtable properties) {
String vfsFolderName = (String)m_fileIndex.get(foldername.replace('\\', '/'));
m_report.print(Messages.get().container(Messages.RPT_CREATE_FOLDER_0), I_CmsReport.FORMAT_NOTE);
m_report.print(org.opencms.report.Messages.get().container(
org.opencms.report.Messages.RPT_ARGUMENT_1,
vfsFolderName));
m_report.print(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0));
if (vfsFolderName != null) {
String path = vfsFolderName.substring(
0,
vfsFolderName.substring(0, vfsFolderName.length() - 1).lastIndexOf("/"));
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) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -