📄 cmshtmlconverter.java
字号:
* @return String with detected errors
*/
public String showErrors (String inString) {
InputStream in = new ByteArrayInputStream(inString.getBytes());
OutputStream out = new ByteArrayOutputStream();
this.showErrors(in,out);
return out.toString();
}
/**
* Checks if HTML code has errors and lists errors
* @param input InputStream with HTML code
* @param output OutputStream with detected errors
*/
public void showErrors (InputStream input, OutputStream output) {
/* initialise JTidy */
m_tidy.setOnlyErrors(true);
m_tidy.setQuiet(true);
m_tidy.setShowWarnings(false);
InputStream in = new BufferedInputStream(input);
PrintWriter errorLog = new PrintWriter(output);
m_tidy.setErrout(errorLog);
m_tidy.parse(in,null);
if (m_tidy.getParseErrors() == 0) {
errorLog.println("HTML code ok!\nNo errors detected.");
}
errorLog.close();
}
/**
* Transforms HTML code into user defined output
* @param inString String with HTML code
* @return String with transformed code
*/
public String convertHTML (String inString) {
InputStream in = new ByteArrayInputStream(inString.getBytes());
OutputStream out = new ByteArrayOutputStream();
this.convertHTML(in,out);
return out.toString();
}
/**
* Transforms HTML code into user defined output
* @param input InputStream with HTML code
* @param output OutputStream with transformed code
*/
public void convertHTML (InputStream input, OutputStream output) {
/* local variables */
int streamInput;
StringBuffer htmlString = new StringBuffer();
Node node;
String outString = "";
/* initialise JTidy */
m_tidy.setShowWarnings(false);
m_tidy.setQuiet(true);
if (m_tidyConfFileDefined == false) {
m_tidy.setOnlyErrors(false);
m_tidy.setTidyMark(false);
}
/* print errorlog in ByteArray */
PrintWriter errorLog = new PrintWriter(new ByteArrayOutputStream(),true);
m_tidy.setErrout(errorLog);
try {
/* write InputStream input in StringBuffer htmlString */
while ((streamInput = input.available()) > 0) {
byte[] b = new byte[streamInput];
int result = input.read(b);
if (result == -1) {
break;
}
htmlString.append(new String(b));
}
}
catch (IOException e) {
System.err.println("Conversion error: " + e.toString());
return;
}
outString = htmlString.toString();
/* first step: replace subStrings in htmlString run #1*/
outString = m_tools.scanContent(outString,m_configuration.getReplaceContent());
/* convert htmlString in InputStream for parseDOM */
InputStream in = new ByteArrayInputStream(outString.getBytes());
node = m_tidy.parseDOM(in,null);
/* check if html code has errors */
if (m_tidy.getParseErrors() != 0) {
System.err.println("Conversion error: HTML code has errors!");
}
/* second step: create transformed output with printDocument from DOM */
this.printDocument(node);
/* third step: replace Strings run #2 */
outString = m_tools.scanString(m_tempString.toString(),m_configuration.getReplaceStrings());
outString = this.cleanOutput(outString);
try {
output.write(outString.getBytes(),0,outString.length());
output.close();
}
catch (IOException e) {
System.err.println("Conversion error: " + e.toString());
return;
}
}
/**
* Private method to parse DOM and create user defined output
* @param node Node of DOM from HTML code
*/
private void printDocument(Node node) {
/* if node is empty do nothing... (Recursion) */
if ( node == null ) {
return;
}
/* initialise local variables */
int type = node.getNodeType();
int replaceTag = -1;
int replaceBlock = -1;
/* detect node type */
switch (type) {
case Node.DOCUMENT_NODE:
/* initialise m_tempString and add global prefix */
m_tempString = new StringBuffer(m_configuration.getGlobalPrefix());
this.printDocument(((Document)node).getDocumentElement());
break;
case Node.ELEMENT_NODE:
/* analyse element node and transform it */
replaceBlock = this.indexReplaceBlock(node);
replaceTag = this.indexReplaceTag(node);
/* scan element node; if a block has to be removed or replaced,
break and discard child nodes */
if (this.transformStartElement(node,replaceBlock,replaceTag)) {
break;
}
/* test if node has children */
NodeList children = node.getChildNodes();
if ( children != null ) {
int len = children.getLength();
for ( int i = 0; i < len; i++ )
/* recursively call printDocument with all child nodes */
this.printDocument(children.item(i));
}
break;
case Node.TEXT_NODE:
/* replace subStrings in text nodes */
this.transformTextNode(node);
break;
}
/* end of recursion, add eventual endtags and suffixes */
switch (type) {
case Node.ELEMENT_NODE:
/* analyse endtags and add them to output */
this.transformEndElement(node,replaceBlock,replaceTag);
break;
case Node.DOCUMENT_NODE:
/* add suffix to end of output */
this.transformEndDocument(node);
break;
}
}
/**
* Private method to transform element nodes and create start tags in output
* @param node actual element node
* @param replaceBlock index of object m_replaceBlocks
* @param replaceTag index of object m_replaceTags
* @return true if recursion has to be interrupted, otherwise false
*/
private boolean transformStartElement (Node node,int replaceBlock,int replaceTag) {
String tempReplaceString,valueParam;
/* remove complete block, interrupt recursion in printDocument */
if (m_tools.checkTag(node.getNodeName(),m_configuration.getRemoveBlocks())) {
return true;
}
/* if tag has to be removed return, otherwise test other cases */
if (!m_tools.checkTag(node.getNodeName(),m_configuration.getRemoveTags())) {
/* test if a block has to be replaced */
if (replaceBlock != -1) {
m_blockObject = (CmsHtmlConverterObjectReplaceBlocks)
m_configuration.getReplaceBlocks().get(replaceBlock);
/* if a parameter is used, get it from node attribute value,
insert it into replaceString */
tempReplaceString = m_blockObject.getReplaceString();
if (!m_blockObject.getParameter().equals("")) {
valueParam = m_tools.scanNodeAttrs(node,m_blockObject.getParameter());
tempReplaceString = m_tools.replaceString(tempReplaceString,"$parameter$",valueParam);
}
m_tempString.append(tempReplaceString);
/* remove temporary object from ArrayList replaceBlocks */
if (replaceBlock > (m_numberReplaceBlocks-1)) {
m_configuration.removeObjectReplaceBlock(replaceBlock);
}
/* ignore child elements in block, interrupt recursion in printDocument */
return true;
}
else {
/* test if actual element (start tag) has to be replaced */
if (replaceTag != -1) {
m_tagObject = (CmsHtmlConverterObjectReplaceTags)
m_configuration.getReplaceTags().get(replaceTag);
tempReplaceString = m_tagObject.getReplaceStartTag();
/* if a parameter is used, get it from node attribute value,
insert it into replaceString */
if (!m_tagObject.getParameter().equals("")) {
valueParam = m_tools.scanNodeAttrs(node,m_tagObject.getParameter());
// HACK: only replace attribute value of parameter attribute!
if (m_tagObject.getReplaceParamAttr()) {
if (!m_tools.shouldReplaceUrl(m_url,valueParam)) {
tempReplaceString = "$parameter$";
}
else {
valueParam = m_tools.modifyParameter(m_url, valueParam, m_servletPrefix);
}
tempReplaceString = m_tools.reconstructTag(tempReplaceString,node,m_tagObject.getParameter(),m_configuration.getQuotationmark());
}
tempReplaceString = m_tools.replaceString(tempReplaceString,"$parameter$",valueParam);
}
m_tempString.append(tempReplaceString);
}
/* no replacement needed: append original element to output */
else {
m_tempString.append("<");
m_tempString.append(node.getNodeName());
NamedNodeMap attrs = node.getAttributes();
for ( int i = attrs.getLength()-1; i >= 0 ; i-- ) {
m_tempString.append(" " + attrs.item(i).getNodeName()
+ "=" + m_configuration.getQuotationmark());
/* scan attribute values and replace subStrings */
String helpString = attrs.item(i).getNodeValue();
helpString = m_tools.scanString(helpString,m_configuration.getReplaceStrings());
m_tempString.append(helpString + m_configuration.getQuotationmark());
}
if (m_configuration.getXhtmlOutput()
&& m_tools.checkTag(node.getNodeName(),m_configuration.getInlineTags())) {
m_tempString.append("/");
}
m_tempString.append(">");
}
} /* OPTION: Here I can add a "\n" after every starttag */
}
return false;
}
/**
* Private method to transform element nodes and create end tags in output
* @param node actual element node
* @param replaceBlock index of object m_replaceBlocks
* @param replaceTag index of object m_replaceTags
*/
private void transformEndElement(Node node, int replaceBlock, int replaceTag) {
/* test if block has to be removed */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -