1 6 7 package org.contineo.core.util; 8 9 import java.io.File ; 10 import java.io.FileInputStream ; 11 import java.io.IOException ; 12 import java.io.InputStream ; 13 import java.io.StringBufferInputStream ; 14 15 import org.apache.log4j.Level; 16 import org.apache.log4j.Logger; 17 import org.contineo.core.LoggingManager; 18 22 public class HTMLReviser { 23 private StringBuffer sb = new StringBuffer (); 24 25 public HTMLReviser() { 26 } 27 28 public InputStream revise(File file, String path, String menuid) throws IOException 29 { 30 try 31 { 32 sb = new StringBuffer (); 33 FileInputStream reader = new FileInputStream (file); 34 int ichar = 0; 35 boolean istag = false , wtag = false; 36 StringBuffer tagb = new StringBuffer (); 37 StringBuffer tag = new StringBuffer (); 38 39 while ((ichar = reader.read()) != -1) 40 { 41 if (ichar == 60) 42 { 43 istag = true; 44 wtag = true; 45 } 46 if (ichar == 62) 47 { 48 if (tag.toString().toLowerCase().equals("<a") || tag.toString().toLowerCase().equals("<link")) 50 { 51 String temp = tagb.toString(); 53 if (temp.indexOf("href") != -1) 54 { 55 String prehref = temp.substring(0,temp.indexOf("href") -1); 57 String href = temp.substring(temp.indexOf("href")); 58 String posthref = ""; 59 if (href.indexOf((char)32) != -1) 61 { 62 posthref = href.substring(href.indexOf((char)32)); 64 href = href.substring(0,href.indexOf((char)32)); 66 } 67 StringBuffer sbtemp = new StringBuffer (); 68 for (int i=0;i<href.length();i++) 69 { 70 int a = (int)href.charAt(i); 71 if (a != 34 && a != 39) 73 sbtemp.append((char)a); 74 } 75 href = sbtemp.toString(); 76 href.trim(); 77 String value = href.substring(href.indexOf("=") +1); 79 while (path.endsWith("/")) 81 path = path.substring(0,path.length()-1); 82 value = "ShowDocument.do?menuid=" + menuid + "&reference=" + path + "/" + value; 84 temp = prehref + " HREF='" + value + "'" + posthref; 86 } 87 sb.append(temp); 88 } 89 else 90 { 91 if (tag.toString().toLowerCase().equals("<frame") || tag.toString().toLowerCase().equals("<img") || tag.toString().toLowerCase().equals("<script")) 92 { 93 String temp = tagb.toString(); 95 if (temp.indexOf("src") != -1) 96 { 97 String prehref = temp.substring(0,temp.indexOf("src") -1); 98 String href = temp.substring(temp.indexOf("src")); 99 String posthref = ""; 100 if (href.indexOf((char)32) != -1) 101 { 102 posthref = href.substring(href.indexOf((char)32)); 103 href = href.substring(0,href.indexOf((char)32)); 104 } 105 StringBuffer sbtemp = new StringBuffer (); 106 for (int i=0;i<href.length();i++) 107 { 108 int a = (int)href.charAt(i); 109 if (a != 34 && a != 39) 110 sbtemp.append((char)a); 111 } 112 href = sbtemp.toString(); 113 href.trim(); 114 String value = href.substring(href.indexOf("=") +1); 115 while (path.endsWith("/")) 116 path = path.substring(0,path.length()-1); 117 value = "ShowDocument.do?menuid=" + menuid + "&reference=" + path + "/" + value; 118 temp = prehref + " SRC='" + value + "'" + posthref; 119 } 120 sb.append(temp); 121 } 122 else 123 { 124 if (tag.toString().toLowerCase().equals("<applet")) 125 { 126 String temp = tagb.toString(); 128 if (temp.indexOf("archive") != -1) 129 { 130 String prehref = temp.substring(0,temp.indexOf("archive") -1); 131 String href = temp.substring(temp.indexOf("archive")); 132 String posthref = ""; 133 if (href.indexOf((char)32) != -1) 134 { 135 posthref = href.substring(href.indexOf((char)32)); 136 href = href.substring(0,href.indexOf((char)32)); 137 } 138 StringBuffer sbtemp = new StringBuffer (); 139 for (int i=0;i<href.length();i++) 140 { 141 int a = (int)href.charAt(i); 142 if (a != 34 && a != 39) 143 sbtemp.append((char)a); 144 } 145 href = sbtemp.toString(); 146 href.trim(); 147 String value = href.substring(href.indexOf("=") +1); 148 while (path.endsWith("/")) 149 path = path.substring(0,path.length()-1); 150 value = "ShowDocument.do?menuid=" + menuid + "&reference=" + path + "/" + value; 151 temp = prehref + " archive='" + value + "'" + posthref; 152 } 153 sb.append(temp); 154 } 155 else 156 { 157 sb.append(tagb); 158 } 159 } 160 } 161 tag = new StringBuffer (); 162 tagb = new StringBuffer (); 163 istag = false; 164 wtag = false; 165 } 166 if (!istag) 167 sb.append((char)ichar); 168 else 169 { 170 tagb.append((char)ichar); 171 if (ichar == 32) 172 { 173 wtag = false; 174 } 175 if (wtag) 176 { 177 tag.append((char)ichar); 178 } 179 } 180 } 181 reader.close(); 182 } catch (Exception ex) { 183 Logger logger = LoggingManager.getLogger(this.getClass()); 184 if (logger.isEnabledFor(Level.ERROR)) 185 logger.error(ex.getMessage()); 186 } 187 StringBufferInputStream sbis = new StringBufferInputStream (sb.toString()); 188 InputStream is = sbis; 189 sbis.close(); 190 return is; 191 } 192 } 193 | Popular Tags |