1 21 22 package nu.xom.samples; 23 24 import java.io.BufferedWriter ; 25 import java.io.File ; 26 import java.io.FileOutputStream ; 27 import java.io.IOException ; 28 import java.io.OutputStreamWriter ; 29 import java.io.Writer ; 30 31 import nu.xom.Attribute; 32 import nu.xom.Builder; 33 import nu.xom.Element; 34 import nu.xom.NodeFactory; 35 import nu.xom.Nodes; 36 import nu.xom.ParsingException; 37 38 50 public class StreamingExampleExtractor extends NodeFactory { 51 52 private int chapter = 0; 53 private boolean inExample = false; 54 private Nodes empty = new Nodes(); 55 56 57 public Nodes makeComment(String data) { 59 return empty; 60 } 61 62 63 public Nodes makeText(String data) { 65 if (inExample) return super.makeText(data); 66 return empty; 67 } 68 69 70 public Element makeRootElement(String name, String namespace) { 71 if ("example".equals(name)) { 72 inExample = true; 73 } 74 if ("chapter".equals(name)) { 75 chapter++; 76 } 77 return super.startMakingElement(name, namespace); 78 } 79 80 81 public Element startMakingElement(String name, String namespace) { 82 if ("example".equals(name)) { 83 inExample = true; 84 } 85 if ("chapter".equals(name)) { 86 chapter++; 87 } 88 if (inExample) return super.startMakingElement(name, namespace); 89 else return null; 90 } 91 92 93 public Nodes finishMakingElement(Element element) { 94 if (element.getQualifiedName().equals("example")) { 95 try { 96 extractExample(element, chapter); 97 } 98 catch (IOException ex) { 99 System.err.println( 100 "Problem writing " + element.getAttributeValue("id") 101 + " in chapter " + chapter 102 ); 103 } 104 inExample = false; 105 } 106 return new Nodes(element); 107 } 108 109 110 public Nodes makeAttribute(String name, String URI, 111 String value, Attribute.Type type) { 112 if (inExample && name.equals("id")) { 113 return super.makeAttribute(name, URI, value, type); 114 } 115 return empty; 116 } 117 118 119 public Nodes makeDocType(String rootElementName, 120 String publicID, String systemID) { 121 return empty; 122 } 123 124 125 public Nodes makeProcessingInstruction( 126 String target, String data) { 127 return empty; 128 } 129 130 131 private static void extractExample(Element example, int chapter) 132 throws IOException { 133 134 String fileName = example.getAttribute("id").getValue(); 135 System.out.println(fileName); 136 Element programlisting 137 = example.getFirstChildElement("programlisting"); 138 139 if (programlisting == null) { 141 programlisting = example.getFirstChildElement("screen"); 142 } 143 if (programlisting == null) return; 145 String code = programlisting.getValue(); 146 147 File dir = new File ("examples2/" + chapter); 149 dir.mkdirs(); 150 File file = new File (dir, fileName); 151 System.out.println(file); 152 FileOutputStream fout = new FileOutputStream (file); 153 try { 154 Writer out = new OutputStreamWriter (fout, "UTF-8"); 155 out = new BufferedWriter (out); 157 out.write(code); 158 out.flush(); 160 } 161 finally { 162 fout.close(); 163 } 164 165 } 166 167 168 public static void main(String [] args) { 169 170 if (args.length <= 0) { 171 System.out.println( 172 "Usage: java nu.xom.samples.StreamingExampleExtractor URL"); 173 return; 174 } 175 String url = args[0]; 176 177 try { 178 Builder builder = new Builder(new StreamingExampleExtractor()); 179 builder.build(args[0]); 181 } 182 catch (ParsingException ex) { 183 System.out.println(ex); 184 } 185 catch (IOException ex) { 186 System.out.println( 187 "Due to an IOException, the parser could not read " + url 188 ); 189 System.out.println(ex); 190 } 191 192 } 194 195 } | Popular Tags |