📄 cmsimport.java
字号:
String recoveryPassword, String firstname, String lastname,
String email, String address, String section, String defaultGroup,
String type, Hashtable userInfo, Vector userGroups)
throws CmsException{
try{
try{
m_report.print(m_report.key("report.importing_user"), I_CmsReport.C_FORMAT_NOTE);
m_report.print(name);
m_report.print(m_report.key("report.dots"), I_CmsReport.C_FORMAT_NOTE);
m_cms.addImportUser(name, password, recoveryPassword, description, firstname,
lastname, email, Integer.parseInt(flags), userInfo, defaultGroup, address,
section, Integer.parseInt(type));
// add user to all groups vector
for (int i = 0; i < userGroups.size(); i++) {
try {
m_cms.addUserToGroup(name, (String) userGroups.elementAt(i));
} catch (CmsException exc) {}
}
m_report.println(m_report.key("report.ok"), I_CmsReport.C_FORMAT_OK);
} catch (CmsException exc){
m_report.println(m_report.key("report.not_created"), I_CmsReport.C_FORMAT_OK);
}
} catch (Exception exc){
throw new CmsException(CmsException.C_UNKNOWN_EXCEPTION, exc);
}
}
/**
* Checks all new imported pages and create or updates the entrys in the
* database for the linkmanagement.<p>
*/
private void updatePageLinks(){
int importPagesSize = m_importedPages.size();
for(int i=0; i<importPagesSize; i++){
m_report.print(" ( " + (i+1) + " / " + importPagesSize + " ) ");
try{
// first parse the page
CmsPageLinks links = m_cms.getPageLinks((String)m_importedPages.elementAt(i));
m_report.print(m_report.key("report.checking_page"), I_CmsReport.C_FORMAT_NOTE);
m_report.println((String)m_importedPages.elementAt(i));
// now save the result in the database
m_cms.createLinkEntrys(links.getResourceId(), links.getLinkTargets());
} catch(CmsException e){
m_report.println(e);
// m_report.println(m_report.key("report.problems_with") + m_importedPages.elementAt(i) + ": " + e.getMessage());
}
}
}
/**
* Converts the content of a file from OpenCms 4.x versions.<p>
*
* @param filename the name of the file to convert
* @param byteContent the content of the file
* @param type the type of the file
* @return the converted filecontent
*/
private byte[] convertFile(String filename, byte[] byteContent) {
byte[] returnValue = byteContent;
if (!filename.startsWith("/")) {
filename = "/" + filename;
}
String fileContent = new String(byteContent);
String encoding = getEncoding(fileContent);
if (!"".equals(encoding)) {
// encoding found, ensure that the String is correct
try {
// get content of the file and store it in String with the correct encoding
fileContent = new String(byteContent, encoding);
} catch (UnsupportedEncodingException e) {
// encoding not supported, we use the default and hope we are lucky
if (DEBUG > 0){
System.err.println("["+this.getClass().getName()+".convertFile()]: Encoding not supported, using default encoding.");
}
}
} else {
// encoding not found, set encoding of xml files to default
if (DEBUG > 0){
System.err.println("["+this.getClass().getName()+".convertFile()]: Encoding not set, using default encoding and setting it in <?xml...?>.");
}
encoding = OpenCms.getDefaultEncoding();
fileContent = setEncoding(fileContent, encoding);
}
// check the frametemplates
if (filename.indexOf("frametemplates") != -1) {
fileContent = scanFrameTemplate(fileContent);
}
// scan content/bodys
if (filename.indexOf(C_VFS_PATH_OLD_BODIES) != -1
|| filename.indexOf(I_CmsWpConstants.C_VFS_PATH_BODIES) != -1) {
if (DEBUG > 0){
System.err.println("["+this.getClass().getName()+".convertFile()]: Starting scan of body page.");
}
fileContent = convertPageBody(fileContent, filename);
}
// translate OpenCms 4.x paths to the new directory structure
fileContent = setDirectories(fileContent, m_cms.getRequestContext().getDirectoryTranslator().getTranslations());
// create output ByteArray
try {
returnValue = fileContent.getBytes(encoding);
} catch (UnsupportedEncodingException e) {
// encoding not supported, we use the default and hope we are lucky
returnValue = fileContent.getBytes();
}
return returnValue;
}
/**
* Gets the encoding from the <?XML ...> tag if present.<p>
*
* @param content the file content
* @return String the found encoding
*/
private String getEncoding(String content) {
String encoding = content;
int index = encoding.toLowerCase().indexOf("encoding=\"");
// encoding attribute found, get the value
if (index != -1) {
encoding = encoding.substring(index + 10);
if ((index = encoding.indexOf("\"")) != -1) {
encoding = encoding.substring(0, index);
return encoding.toUpperCase();
}
}
// no encoding attribute found
return "";
}
/**
* Scans the given content of a frametemplate and returns the result.<p>
*
* @param content the filecontent
* @return modified content
*/
private String scanFrameTemplate(String content) {
// no Meta-Tag present, insert it!
if (content.toLowerCase().indexOf("http-equiv=\"content-type\"") == -1) {
content = CmsStringSubstitution.substitute(content, "</head>", "<meta http-equiv=\"content-type\" content=\"text/html; charset=]]><method name=\"getEncoding\"/><![CDATA[\">\n</head>");
}
// Meta-Tag present
else {
if(content.toLowerCase().indexOf("charset=]]><method name=\"getencoding\"/>") == -1){
String fileStart = content.substring(0,content.toLowerCase().indexOf("charset=")+8);
String editContent = content.substring(content.toLowerCase().indexOf("charset="));
editContent = editContent.substring(editContent.indexOf("\""));
String newEncoding = "]]><method name=\"getEncoding\"/><![CDATA[";
content = fileStart + newEncoding + editContent;
}
}
return content;
}
/**
* Sets the right encoding and returns the result.<p>
*
* @param content the filecontent
* @param encoding the encoding to use
* @return modified content
*/
private String setEncoding(String content, String encoding) {
if (content.toLowerCase().indexOf("<?xml") == -1) {
return content;
}
// XML information present, replace encoding
else {
// set the encoding only if it does not exist
String xmlTag = content.substring(0, content.indexOf(">") + 1);
if (xmlTag.toLowerCase().indexOf("encoding") == -1) {
content = content.substring(content.indexOf(">") + 1);
content = "<?xml version=\"1.0\" encoding=\"" + encoding + "\"?>" + content;
}
}
return content;
}
/**
* Translates directory Strings from OpenCms 4.x structure to new 5.0 structure.<p>
*
* @param content the filecontent
* @param type the file type
* @return String the manipulated file content
*/
public static String setDirectories(String content, String[] rules) {
// get translation rules
String root = I_CmsConstants.C_DEFAULT_SITE + I_CmsConstants.C_ROOTNAME_VFS;
for (int i=0; i<rules.length; i++) {
String actRule = rules[i];
// cut String "/default/vfs/" from rule
actRule = CmsStringSubstitution.substitute(actRule, root, "");
// divide rule into search and replace parts and delete regular expressions
StringTokenizer ruleT = new StringTokenizer(actRule, "#");
ruleT.nextToken();
String search = ruleT.nextToken();
search = search.substring(0, search.lastIndexOf("(.*)"));
String replace = ruleT.nextToken();
replace = replace.substring(0, replace.lastIndexOf("$1"));
// scan content for paths if the replace String is not present
if (content.indexOf(replace) == -1 && content.indexOf(search) != -1) {
// ensure subdirectories of the same name are not replaced
search = "([}>\"'\\[]\\s*)" + search;
replace = "$1" + replace;
content = CmsStringSubstitution.substitutePerl(content, search, replace, "g");
}
}
return content;
}
/**
* Searches for the webapps String and replaces it with a macro which is needed for the WYSIWYG editor,
* also creates missing <edittemplate> tags for exports of older OpenCms 4.x versions.<p>
*
* @param content the filecontent
* @return String the modified filecontent
*/
private String convertPageBody(String content, String fileName) {
// variables needed for the creation of <template> elements
boolean createTemplateTags = false;
Hashtable templateElements = new Hashtable();
// first check if any contextpaths are in the content String
boolean found = false;
for (int i = 0; i < m_webAppNames.size(); i++) {
if (content.indexOf((String)m_webAppNames.get(i)) != -1) {
found = true;
}
}
// check if edittemplates are in the content string
if (content.indexOf("<edittemplate>") != -1) {
found = true;
}
// only build document when some paths were found or <edittemplate> is missing!
if (found == true) {
InputStream in = new ByteArrayInputStream(content.getBytes());
String editString, templateString;
try {
// create DOM document
Document contentXml = A_CmsXmlContent.getXmlParser().parse(in);
// get all <edittemplate> nodes to check their content
NodeList editNodes = contentXml.getElementsByTagName("edittemplate");
// no <edittemplate> tags present, create them!
if (editNodes.getLength() < 1) {
if (DEBUG > 0){
System.err.println("["+this.getClass().getName()+".convertPageBody()]: No <edittemplate> found, creating it.");
}
createTemplateTags = true;
NodeList templateNodes = contentXml.getElementsByTagName("TEMPLATE");
// create an <edittemplate> tag for each <template> tag
for (int i = 0; i < templateNodes.getLength(); i++) {
// get the CDATA content of the <template> tags
editString = templateNodes.item(i).getFirstChild().getNodeValue();
templateString = editString;
// substitute the links in the <template> tag String
try {
LinkSubstitution sub = new LinkSubstitution();
templateString = sub.substituteContentBody(templateString, m_webappUrl, fileName);
} catch (CmsException e) {
throw new CmsException("[" + this.getClass().getName() + ".convertPageBody()] can't parse the content: ", e);
}
// look for the "name" attribute of the <template> tag
NamedNodeMap attrs = templateNodes.item(i).getAttributes();
String templateName = "";
if (attrs.getLength() > 0) {
templateName = attrs.item(0).getNodeValue();
}
// create the new <edittemplate> node
Element newNode = contentXml.createElement("edittemplate");
CDATASection newText = contentXml.createCDATASection(editString);
newNode.appendChild(newText);
// set the "name" attribute, if necessary
attrs = newNode.getAttributes();
if (!templateName.equals("")) {
newNode.setAttribute("name", templateName);
}
// append the new edittemplate node to the document
contentXml.getElementsByTagName("XMLTEMPLATE").item(0).appendChild(newNode);
// store modified <template> node Strings in Hashtable
if (templateName.equals("")) {
templateName = "noNameKey";
}
templateElements.put(templateName, templateString);
}
// finally, delete old <TEMPLATE> tags from document
while (templateNodes.getLength() > 0) {
contentXml.getElementsByTagName("XMLTEMPLATE").item(0).removeChild(templateNodes.item(0));
}
}
// check the content of the <edittemplate> nodes
for (int i = 0; i < editNodes.getLength(); i++) {
editString = editNodes.item(i).getFirstChild().getNodeValue();
for (int k = 0; k < m_webAppNames.size(); k++) {
editString =
CmsStringSubstitution.substitute(editString, (String)m_webAppNames.get(k), C_MACRO_OPENCMS_CONTEXT + "/");
}
editNodes.item(i).getFirstChild().setNodeValue(editString);
}
// convert XML document back to String
CmsXmlXercesParser parser = new CmsXmlXercesParser();
Writer out = new StringWriter();
parser.getXmlText(contentXml, out);
content = out.toString();
// rebuild the template tags in the document!
if (createTemplateTags) {
content = content.substring(0, content.lastIndexOf("</XMLTEMPLATE>"));
// get the keys
Enumeration enum = templateElements.keys();
while (enum.hasMoreElements()) {
String key = (String)enum.nextElement();
String value = (String)templateElements.get(key);
// create the default template
if (key.equals("noNameKey")) {
content += "\n<TEMPLATE><![CDATA[" + value;
}
// create template with "name" attribute
else {
content += "\n<TEMPLATE name=\"" + key + "\"><![CDATA[" + value;
}
content += "]]></TEMPLATE>\n";
}
content += "\n</XMLTEMPLATE>";
}
} catch (Exception exc) {}
}
return content;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -