📄 cmshtmlimport.java
字号:
m_linkGallery), e);
}
// check the download gallery
if (!isExternal(m_downloadGallery)) {
try {
m_cmsObject.readFolder(m_downloadGallery);
} catch (CmsException e) {
// an excpetion is thrown if the folder does not exist
throw new CmsIllegalArgumentException(Messages.get().container(
Messages.GUI_HTMLIMPORT_DOWNGALLERY_1,
m_downloadGallery), e);
}
}
// check the template
try {
m_cmsObject.readResource(m_template, CmsResourceFilter.ALL);
} catch (CmsException e) {
// an excpetion is thrown if the template does not exist
if (!isValidElement()) {
throw new CmsIllegalArgumentException(Messages.get().container(
Messages.GUI_HTMLIMPORT_TEMPLATE_1,
m_template), e);
}
}
// check the element
if (!isValidElement()) {
throw new CmsIllegalArgumentException(Messages.get().container(
Messages.GUI_HTMLIMPORT_INVALID_ELEM_2,
m_element,
m_template));
}
// check if we are in an offline project
if (m_cmsObject.getRequestContext().currentProject().isOnlineProject()) {
throw new CmsIllegalArgumentException(
Messages.get().container(Messages.GUI_HTMLIMPORT_CONSTRAINT_OFFLINE_0));
}
}
/**
* Calculates an absolute uri from a relative "uri" and the given absolute "baseUri".<p>
*
* If "uri" is already absolute, it is returned unchanged.
* This method also returns "uri" unchanged if it is not well-formed.<p>
*
* @param relativeUri the relative uri to calculate an absolute uri for
* @param baseUri the base uri, this must be an absolute uri
* @return an absolute uri calculated from "uri" and "baseUri"
*/
public String getAbsoluteUri(String relativeUri, String baseUri) {
if ((relativeUri == null) || (relativeUri.charAt(0) == '/') || (relativeUri.startsWith("#"))) {
return relativeUri;
}
// if we are on a windows system, we must add a ":" in the uri later
String windowsAddition = "";
if (File.separator.equals("\\")) {
windowsAddition = ":";
}
try {
URL url = new URL(new URL(m_baseUrl, "file://" + baseUri), relativeUri);
if (url.getQuery() == null) {
return url.getHost() + windowsAddition + url.getPath();
} else {
return url.getHost() + windowsAddition + url.getPath() + "?" + url.getQuery();
}
} catch (MalformedURLException e) {
return relativeUri;
}
}
/**
* Returns the destinationDir.<p>
*
* @return the destinationDir
*/
public String getDestinationDir() {
return m_destinationDir.substring(0, m_destinationDir.length() - 1);
}
/**
* Returns the downloadGallery.<p>
*
* @return the downloadGallery
*/
public String getDownloadGallery() {
return m_downloadGallery;
}
/**
* Returns the element.<p>
*
* @return the element
*/
public String getElement() {
return m_element;
}
/**
* Returns the endPattern.<p>
*
* @return the endPattern
*/
public String getEndPattern() {
return m_endPattern;
}
/**
* Returns the imageGallery.<p>
*
* @return the imageGallery
*/
public String getImageGallery() {
return m_imageGallery;
}
/**
* Returns the inputDir.<p>
*
* @return the inputDir
*/
public String getInputDir() {
return m_inputDir;
}
/**
* Returns the inputEncoding.<p>
*
* @return the inputEncoding
*/
public String getInputEncoding() {
return m_inputEncoding;
}
/**
* Returns the linkGallery.<p>
*
* @return the linkGallery
*/
public String getLinkGallery() {
return m_linkGallery;
}
/**
* Returns the locale.<p>
*
* @return the locale
*/
public Locale getLocale() {
return m_locale;
}
/**
* Returns the overwrite flag.<p>
*
* @return the overwrite flag
*/
public String getOverwrite() {
return m_overwrite;
}
/**
* Returns the startPattern.<p>
*
* @return the startPattern
*/
public String getStartPattern() {
return m_startPattern;
}
/**
* Returns the template.<p>
*
* @return the template
*/
public String getTemplate() {
return m_template;
}
/**
* Returns the output of the HtmlImportThread.<p>
*
* @return log output of the import threat
*/
public String getThreadOutput() {
String output = "";
// check if we have a thread
if (m_htmlImportThread != null) {
// is it still alive?
if (m_htmlImportThread.isAlive()) {
output = m_htmlImportThread.getReportUpdate();
}
}
return output;
}
/**
* Imports all resources from the real filesystem, stores them into the correct locations
* in the OpenCms VFS and modifies all links. This method is called form the JSP to start the
* import process.<p>
* @param report StringBuffer for reporting
* @throws Exception if something goes wrong
*/
public void startImport(I_CmsReport report) throws Exception {
try {
m_report = report;
m_report.println(Messages.get().container(Messages.RPT_HTML_IMPORT_BEGIN_0), I_CmsReport.FORMAT_HEADLINE);
// first build the index of all resources
buildIndex(m_inputDir);
// copy and parse all html files first. during the copy process we will collect all
// required data for downloads and images
copyHtmlFiles(m_inputDir);
// now copy the other files
copyOtherFiles(m_inputDir);
// finally create all the external links
createExternalLinks();
m_report.println(Messages.get().container(Messages.RPT_HTML_IMPORT_END_0), I_CmsReport.FORMAT_HEADLINE);
} catch (Exception e) {
e.printStackTrace();
} finally {
// clear memory
clear();
}
}
/**
* Add a new external link to the storage of external links.<p>
*
* All links in this storage are later used to create entries in the external link gallery.
* @param externalLink link to an external resource
*/
public void storeExternalLink(String externalLink) {
m_externalLinks.add(externalLink);
}
/**
* Add a new image info to the storage of image infos.<p>
*
* The image infoes are later used to set the description properties of the images.
* @param image the name of the image
* @param altText the alt-text of the image
*/
public void storeImageInfo(String image, String altText) {
m_imageInfo.put(image, altText);
}
/**
* Checks if the HtmlImportThread is still alive.<p>
*
* @return true or false
*/
public boolean threadAlive() {
boolean alive = false;
if (m_htmlImportThread != null) {
alive = m_htmlImportThread.isAlive();
}
return alive;
}
/**
* Translated a link into the real filesystem to its new location in the OpenCms VFS.<p>
*
* This is needed by the HtmlConverter to get the correct links for link translation.
* @param link link to the reafl filesystem
* @return string containing absulute link into the OpenCms VFS
*/
public String translateLink(String link) {
String translatedLink = null;
translatedLink = (String)m_fileIndex.get(link.replace('\\', '/'));
if (translatedLink == null) {
// its an anchor link, so copy use it
if (link.startsWith("#")) {
translatedLink = link;
} else if (link.length() >= m_inputDir.length() + 1) {
// create a 'faked' link into the VFS. Original link was
// directing to a missing page, so let the link so to the
// same page inside of OpenCms.
String relativeFSName = link.substring(m_inputDir.length() + 1);
translatedLink = m_destinationDir + relativeFSName;
}
}
// if the link goes to a directory, lets link to the index page within
if ((translatedLink != null) && translatedLink.endsWith("/")) {
translatedLink += "index.html";
}
// final check: if the translated link is still null the original link found
// was broken
// lets link it to the same page, the link is found on
if (translatedLink == null) {
translatedLink = "#";
}
return translatedLink;
}
/**
* Builds an index of all files to be imported and determines their new names in the OpenCms.<p>
*
* @param startfolder the folder to start with
*
* @throws Exception if something goes wrong
*/
private void buildIndex(String startfolder) throws Exception {
File folder = new File(startfolder);
// get all subresources
File[] subresources = folder.listFiles();
// now loop through all subresources and add them to the index list
for (int i = 0; i < subresources.length; i++) {
try {
String relativeFSName = subresources[i].getAbsolutePath().substring(m_inputDir.length() + 1);
String absoluteVFSName = getVfsName(relativeFSName, subresources[i].getName(), subresources[i].isFile());
m_report.print(Messages.get().container(Messages.RPT_CREATE_INDEX_0), I_CmsReport.FORMAT_NOTE);
m_report.print(org.opencms.report.Messages.get().container(
org.opencms.report.Messages.RPT_ARGUMENT_1,
relativeFSName.replace('\\', '/')));
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.print(org.opencms.report.Messages.get().container(
org.opencms.report.Messages.RPT_ARGUMENT_1,
absoluteVFSName));
m_report.print(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0));
m_fileIndex.put(subresources[i].getAbsolutePath().replace('\\', '/'), absoluteVFSName);
// if the subresource is a folder, get all subresources of it as well
if (subresources[i].isDirectory()) {
buildIndex(subresources[i].getAbsolutePath());
}
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);
}
}
}
/**
* Clear all used indices and lists.<p>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -