Coverage Report - net.sf.jmatchparser.template.engine.formatter.JTidyFormatter
 
Classes in this File Line Coverage Branch Coverage Complexity
JTidyFormatter
71%
27/38
80%
8/10
3,667
 
 1  
 /*
 2  
  * Copyright (c) 2006 - 2011 Michael Schierl
 3  
  * All rights reserved.
 4  
  * 
 5  
  * This program is free software: you can redistribute it and/or modify
 6  
  * it under the terms of the GNU General Public License as published by
 7  
  * the Free Software Foundation, either version 2 of the License, or
 8  
  * (at your option) any later version.
 9  
  * 
 10  
  * This program is distributed in the hope that it will be useful,
 11  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13  
  * GNU General Public License for more details.
 14  
  * 
 15  
  * You should have received a copy of the GNU General Public License
 16  
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 17  
  */
 18  
 package net.sf.jmatchparser.template.engine.formatter;
 19  
 
 20  
 import java.io.IOException;
 21  
 
 22  
 import net.sf.jmatchparser.template.Formatter;
 23  
 
 24  
 import org.w3c.tidy.Tidy;
 25  
 import java.io.*;
 26  
 import java.util.regex.Pattern;
 27  
 
 28  
 public class JTidyFormatter implements Formatter {
 29  
 
 30  
         private final boolean cleanSpace;
 31  
 
 32  406
         public JTidyFormatter(boolean cleanSpace) {
 33  406
                 this.cleanSpace = cleanSpace;
 34  406
         }
 35  
 
 36  
         @Override
 37  
         public String getName() {
 38  406
                 return cleanSpace ? "jtidy+cleanspace" : "jtidy";
 39  
         }
 40  
 
 41  
         @Override
 42  
         public String format(String content, PrintStream debugStream) throws IOException {
 43  4
                 if (content.length() == 0)
 44  0
                         return content;
 45  4
                 Tidy tidy = new Tidy();
 46  4
                 tidy.setXmlOut(true);
 47  4
                 tidy.setWraplen(0);
 48  4
                 tidy.setShowWarnings(false);
 49  4
                 tidy.setQuiet(true);
 50  4
                 tidy.setShowErrors(0);
 51  4
                 tidy.setForceOutput(true);
 52  4
                 tidy.setTidyMark(false);
 53  4
                 tidy.setNumEntities(true);
 54  4
                 tidy.setRawOut(true);
 55  4
                 StringWriter sw = new StringWriter();
 56  
                 try {
 57  4
                         tidy.parse(new StringReader(content), sw);
 58  0
                 } catch (StringIndexOutOfBoundsException ex) {
 59  
                         // jTidy bug #2922337
 60  0
                         String bugAvoider = "<script>BugAvoider</script>";
 61  0
                         String bugAvoider2 = Pattern.quote("<script type=\"text/javascript\">\r\n//<![CDATA[\r\nBugAvoider\r\n//]]>\r\n</script>\r\n");
 62  0
                         debugStream.println(" * I caught the bug * ");
 63  0
                         content = content.replaceFirst("<title", bugAvoider + "<title");
 64  0
                         tidy.parse(new StringReader(content), sw);
 65  0
                         String tmp = sw.toString();
 66  0
                         sw = new StringWriter();
 67  0
                         sw.write(tmp.replaceFirst(bugAvoider2, ""));
 68  4
                 }
 69  4
                 sw.close();
 70  4
                 if (sw.toString().length() == 0) {
 71  0
                         throw new RuntimeException("Content is empty after preprocessing");
 72  
                 }
 73  4
                 content = sw.toString();
 74  4
                 if (cleanSpace)
 75  3
                         content = content.replaceAll("[\r\n][ \t]+<", "<").replaceAll("[\r\n]+", "").replace('\t', ' ').replaceAll("  +", " ");
 76  4
                 content = content.replace("\r\n", "\n").replace('\r', '\n');
 77  4
                 if (!content.endsWith("\n"))
 78  3
                         content = content + "\n";
 79  4
                 return content;
 80  
         }
 81  
 }