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.Builder; 32 import nu.xom.Document; 33 import nu.xom.Element; 34 import nu.xom.Elements; 35 import nu.xom.ParsingException; 36 37 49 public class ExampleExtractor { 50 51 private static int chapter = 0; 52 53 public static void extract(Element element) 54 throws IOException { 55 56 Elements elements = element.getChildElements(); 57 for (int i = 0; i < elements.size(); i++) { 58 Element child = elements.get(i); 59 if (child.getQualifiedName().equals("chapter")) { 60 chapter++; 61 } 62 if (child.getQualifiedName().equals("example")) { 63 extractExample(child, chapter); 64 } 65 else { 66 extract(child); 67 } 68 } 70 } 71 72 73 private static void extractExample(Element example, int chapter) 74 throws IOException { 75 76 String fileName = example.getAttribute("id").getValue(); 77 Element programlisting 78 = example.getFirstChildElement("programlisting"); 79 80 if (programlisting == null) { 82 programlisting = example.getFirstChildElement("screen"); 83 } 84 if (programlisting == null) return; 86 String code = programlisting.getValue(); 87 88 File dir = new File ("examples2/" + chapter); 90 dir.mkdirs(); 91 File file = new File (dir, fileName); 92 System.out.println(file); 93 FileOutputStream fout = new FileOutputStream (file); 94 Writer out = new OutputStreamWriter (fout, "UTF-8"); 95 try { 96 out = new BufferedWriter (out); 98 out.write(code); 99 out.flush(); 101 } 102 finally { 103 fout.close(); 104 } 105 106 } 107 108 109 public static void main(String [] args) { 110 111 if (args.length <= 0) { 112 System.out.println( 113 "Usage: java nu.xom.samples.ExampleExtractor URL"); 114 return; 115 } 116 String url = args[0]; 117 118 try { 119 Builder builder = new Builder(); 120 Document document = builder.build(args[0]); 122 123 extract(document.getRootElement()); 125 126 } 127 catch (ParsingException ex) { 128 System.out.println(ex); 129 } 130 catch (IOException ex) { 131 System.out.println( 132 "Due to an IOException, the parser could not read " + url 133 ); 134 System.out.println(ex); 135 } 136 catch (OutOfMemoryError ex) { 137 System.out.println(ex); 138 ex.printStackTrace(); 139 } 140 141 } 143 144 } 145 146 | Popular Tags |