1 package org.apache.ecs.html2ecs; 2 3 60 61 import java.util.Hashtable ; 62 import java.io.InputStream ; 63 import java.io.IOException ; 64 import java.io.FileInputStream ; 65 import java.io.FileNotFoundException ; 66 import java.lang.reflect.Method ; 67 import org.apache.xerces.parsers.DOMParser; 68 import org.xml.sax.InputSource ; 69 import org.w3c.dom.Node ; 70 import org.w3c.dom.NodeList ; 71 import org.w3c.dom.NamedNodeMap ; 72 import org.w3c.dom.Attr ; 73 import org.apache.ecs.filter.CharacterFilter; 74 import org.apache.ecs.Entities; 75 import org.xml.sax.SAXException ; 76 77 89 public class Html2Ecs extends Hashtable 90 { 91 private static org.w3c.dom.Document doc; 92 private static DOMParser parser; 93 private static InputStream input; 94 95 { 98 put("A","org.apache.ecs.html.A"); 99 put("ABBR","org.apache.ecs.html.Abbr"); 100 put("ACRONYM","org.apache.ecs.html.Acronym"); 101 put("ADDRESS","org.apache.ecs.html.Address"); 102 put("APPLET","org.apache.ecs.html.Applet"); 103 put("AREA","org.apache.ecs.html.Area"); 104 put("B","org.apache.ecs.B"); 105 put("BASE","org.apache.ecs.html.Base"); 106 put("BASEFONT","org.apache.ecs.html.BaseFont"); 107 put("BDO","org.apache.ecs.html.Bdo"); 108 put("BIG","org.apache.ecs.html.Big"); 109 put("BLINK","org.apache.ecs.html.Blink"); 110 put("BLOCKQUOTE","org.apache.ecs.html.BlockQuote"); 111 put("BODY","org.apache.ecs.html.Body"); 112 put("BR","org.apache.ecs.html.BR"); 113 put("BUTTON","org.apache.ecs.html.Button"); 114 put("CAPTION","org.apache.ecs.html.Caption"); 115 put("CENTER","org.apache.ecs.html.Center"); 116 put("CITE","org.apache.ecs.html.Cite"); 117 put("CODE","org.apache.ecs.html.Code"); 118 put("COL","org.apache.ecs.html.Col"); 119 put("COLGROUP","org.apache.ecs.html.ColGroup"); 120 put("COMMENT","org.apache.ecs.html.Comment"); 121 put("DD","org.apache.ecs.html.DD"); 122 put("DEL","org.apache.ecs.html.Del"); 123 put("DFN","org.apache.ecs.html.Dfn"); 124 put("DIV","org.apache.ecs.html.Div"); 125 put("DL","org.apache.ecs.html.DL"); 126 put("EM","org.apache.ecs.html.EM"); 127 put("FILEDSET","org.apache.ecs.html.FieldSet"); 128 put("FONT","org.apache.ecs.html.Font"); 129 put("FORM","org.apache.ecs.html.Form"); 130 put("FRAME","org.apache.ecs.html.Frame"); 131 put("FRAMESET","org.apache.ecs.html.FrameSet"); 132 put("H1","org.apache.ecs.html.H1"); 133 put("H2","org.apache.ecs.html.H2"); 134 put("H3","org.apache.ecs.html.H3"); 135 put("H4","org.apache.ecs.html.H4"); 136 put("H5","org.apache.ecs.html.H5"); 137 put("H6","org.apache.ecs.html.H6"); 138 put("HEAD","org.apache.ecs.html.Head"); 139 put("HR","org.apache.ecs.html.HR"); 140 put("HTML","org.apache.ecs.html.Html"); 141 put("I","org.apache.ecs.html.I"); 142 put("IFRAME","org.apache.ecs.html.IFrame"); 143 put("IMG","org.apache.ecs.html.IMG"); 144 put("INPUT","org.apache.ecs.html.Input"); 145 put("INS","org.apache.ecs.html.Ins"); 146 put("KBD","org.apache.ecs.html.Kbd"); 147 put("LABEL","org.apache.ecs.html.Label"); 148 put("LEGEND","org.apache.ecs.html.Legend"); 149 put("LI","org.apache.ecs.html.LI"); 150 put("LINK","org.apache.ecs.html.Link"); 151 put("MAP","org.apache.ecs.html.Map"); 152 put("META","org.apache.ecs.html.Meta"); 153 put("NOBR","org.apache.ecs.html.NOBR"); 154 put("NOFRAMES","org.apache.ecs.html.NoFrames"); 155 put("NOSCRIPT","org.apache.ecs.html.NoScript"); 156 put("OBJECT","org.apache.ecs.html.ObjectElement"); 157 put("OL","org.apache.ecs.html.OL"); 158 put("OPTGROUP","org.apache.ecs.html.OptGroup"); 159 put("OPTION","org.apache.ecs.html.Option"); 160 put("P","org.apache.ecs.html.P"); 161 put("PARAM","org.apache.ecs.html.Param"); 162 put("PRE","org.apache.ecs.html.PRE"); 163 put("Q","org.apache.ecs.html.Q"); 164 put("S","org.apache.ecs.html.S"); 165 put("SAMP","org.apache.ecs.html.Samp"); 166 put("SCRIPT","org.apache.ecs.html.Script"); 167 put("SELECT","org.apache.ecs.html.Select"); 168 put("SMALL","org.apache.ecs.html.Small"); 169 put("SPAN","org.apache.ecs.html.Span"); 170 put("STRIKE","org.apache.ecs.html.Strike"); 171 put("STRONG","org.apache.ecs.html.Strong"); 172 put("STYLE","org.apache.ecs.html.Style"); 173 put("SUB","org.apache.ecs.html.Sub"); 174 put("SUP","org.apache.ecs.html.Sup"); 175 put("TABLE","org.apache.ecs.html.Table"); 176 put("TBODY","org.apache.ecs.html.TBody"); 177 put("TD","org.apache.ecs.html.TD"); 178 put("THEAD","org.apache.ecs.html.THead"); 179 put("TITLE","org.apache.ecs.html.Title"); 180 put("TR","org.apache.ecs.html.TR"); 181 put("TT","org.apache.ecs.html.TT"); 182 put("U","org.apache.ecs.html.U"); 183 put("UL","org.apache.ecs.html.UL"); 184 put("VAR","org.apache.ecs.html.Var"); 185 186 } 187 188 public Html2Ecs() 189 { 190 super(4); 191 } 192 193 private void process( Node node ) 194 { 195 while( node != null) 196 { 197 constructElement(node); 198 199 if ( node.hasChildNodes() && 201 !node.getNodeName().equals("#document") && 202 get(node.getNodeName()) == null ) 203 { 204 System.out.print(".addElement("); 205 process ( node.getFirstChild() ); 206 System.out.println(")"); 207 } 208 else 209 { 210 process ( node.getFirstChild() ); 211 } 212 node = node.getNextSibling(); 214 System.out.print("\n"); 215 } 216 } 217 218 private void constructElement( Node node ) 219 { 220 if( get(node.getNodeName().toUpperCase()) != null ) 221 { 222 System.out.print("new "); 223 System.out.print(get(node.getNodeName().toUpperCase())); 224 System.out.print("()"); 225 } 226 constructAttributes( node ); 227 constructValue( node ); 228 } 229 230 private void constructValue( Node node ) 231 { 232 CharacterFilter cf = new CharacterFilter(); 233 cf.addAttribute("\"",Entities.QUOT); 234 235 if( node.getNodeName().equals("#text") 236 && node.getNodeValue().length() > 1 ) 237 { 238 System.out.print(".addElement(\""); 239 System.out.print(node.getNodeValue()); 240 System.out.print("\")"); 241 } 242 } 243 244 private void constructAttributes( Node node ) 245 { 246 NamedNodeMap attrList = node.getAttributes(); 247 Attr attr; 248 Class c = null; 249 250 try 251 { 252 c = Class.forName( (String ) get(node.getNodeName().toUpperCase()) ); 253 } 254 catch(Exception e) 255 { 256 } 257 258 if( attrList != null && c != null) 259 { 260 for(int x = 0; x < attrList.getLength(); x++) 261 { 262 attr = (Attr ) attrList.item(x); 263 264 Method [] m = c.getMethods(); 265 for(int y = 0; y < m.length; ++y) 266 { 267 if(m[y].getName().toLowerCase().endsWith(attr.getName().toLowerCase())) 268 { 269 System.out.print("."); 270 System.out.print(m[y].getName()); 271 System.out.print("(\""); 272 System.out.print(attr.getValue()); 273 System.out.print("\")"); 274 break; 275 } 276 } 277 } 278 } 279 } 280 281 public static void main(String [] args) 282 { 283 Html2Ecs html2ecs = new Html2Ecs(); 284 try 285 { 286 input = new FileInputStream ( args[0] ); 287 parser = new DOMParser(); 288 parser.parse(new InputSource (input)); 289 doc = parser.getDocument(); 290 } 291 catch(SAXException se) 292 { 293 System.out.println(se.toString()); 294 } 295 catch(FileNotFoundException fnfe) 296 { 297 System.out.println(fnfe.toString()); 298 } 299 catch(IOException ioe) 300 { 301 System.out.println(ioe.toString()); 302 } 303 html2ecs.process( doc ); 304 } 305 } 306 | Popular Tags |