1 22 23 package org.gjt.sp.jedit.pluginmgr; 24 25 import java.util.*; 26 27 import org.xml.sax.Attributes ; 28 import org.xml.sax.InputSource ; 29 import org.xml.sax.helpers.DefaultHandler ; 30 31 import org.gjt.sp.util.XMLUtilities; 32 import org.gjt.sp.util.Log; 33 import org.gjt.sp.jedit.options.PluginOptions; 34 35 38 class MirrorListHandler extends DefaultHandler 39 { 40 MirrorListHandler(MirrorList mirrors, String path) 42 { 43 this.mirrors = mirrors; 44 this.path = path; 45 } 47 public InputSource resolveEntity(String publicId, String systemId) 49 { 50 return XMLUtilities.findEntity(systemId, "mirrors.dtd", 51 PluginOptions.class); 52 } 54 public void characters(char[] c, int off, int len) 56 { 57 String tag = peekElement(); 58 59 if(tag == "DESCRIPTION") 60 description.append(c, off, len); 61 else if(tag == "LOCATION") 62 location.append(c, off, len); 63 else if(tag == "COUNTRY") 64 country.append(c, off, len); 65 else if(tag == "CONTINENT") 66 continent.append(c, off, len); 67 } 69 public void startElement(String uri, String localName, 71 String tag, Attributes attrs) 72 { 73 tag = pushElement(tag); 74 75 if (tag.equals("MIRROR")) 76 { 77 mirror = new MirrorList.Mirror(); 78 id = attrs.getValue("ID"); 79 } 80 } 82 public void endElement(String uri, String localName, String tag) 84 { 85 popElement(); 86 87 if(tag.equals("MIRROR")) 88 { 89 mirror.id = id; 90 mirror.description = description.toString(); 91 mirror.location = location.toString(); 92 mirror.country = country.toString(); 93 mirror.continent = continent.toString(); 94 mirrors.add(mirror); 95 description.setLength(0); 96 location.setLength(0); 97 country.setLength(0); 98 continent.setLength(0); 99 } 100 } 102 public void startDocument() 104 { 105 try 106 { 107 pushElement(null); 108 } 109 catch (Exception e) 110 { 111 Log.log(Log.ERROR, this, e); 112 } 113 } 115 public void endDocument() 117 { 118 mirrors.finished(); 119 } 121 123 private String id; 125 private final StringBuilder description = new StringBuilder (); 126 private final StringBuilder location = new StringBuilder (); 127 private final StringBuilder country = new StringBuilder (); 128 private final StringBuilder continent = new StringBuilder (); 129 130 private final MirrorList mirrors; 131 private MirrorList.Mirror mirror; 132 133 private final Stack<String > stateStack = new Stack<String >(); 134 private final String path; 135 137 private String pushElement(String name) 138 { 139 name = name == null ? null : name.intern(); 140 stateStack.push(name); 141 return name; 142 } 143 144 private String peekElement() 145 { 146 return stateStack.peek(); 147 } 148 149 private void popElement() 150 { 151 stateStack.pop(); 152 } 153 154 } 156 | Popular Tags |