1 23 24 import org.xml.sax.InputSource ; 25 import java.util.Properties ; 26 import org.xml.sax.helpers.DefaultHandler ; 27 import org.xml.sax.SAXException ; 28 import org.xml.sax.Attributes ; 29 import java.io.IOException ; 30 import javax.xml.parsers.SAXParserFactory ; 31 import javax.xml.parsers.ParserConfigurationException ; 32 import org.xml.sax.SAXNotRecognizedException ; 33 import javax.xml.parsers.SAXParser ; 34 import java.io.File ; 35 import java.io.FileReader ; 36 import java.io.FileOutputStream ; 37 38 class MakeCatalog extends DefaultHandler 39 { 40 final String prefix; 41 42 MakeCatalog(){ 43 prefix = ""; 44 } 45 46 MakeCatalog(final String prefix){ 47 this.prefix = (prefix != null ? prefix +"." : ""); 48 } 49 50 51 public static void main(String [] args) throws Exception { 52 if (args.length < 2 || 3 < args.length ){ 53 useage(); 54 System.exit(1); 55 } 56 57 final String prefix = (args.length == 2 ? null : args[0]); 58 final String in = (args.length == 2 ? args[0] : args[1]); 59 final String out = (args.length == 2 ? args[1] : args[2]); 60 61 final MakeCatalog mc = new MakeCatalog(prefix); 62 final InputSource is = new InputSource (new FileReader (new File (in))); 63 is.setSystemId(in); 64 mc.makeCatalog(is); 65 mc.getProperties().save(new FileOutputStream (new File (out)), null); 66 System.exit(0); 67 } 68 69 private static void useage(){ 70 System.err.println("Useage: MakeCatalog [prefix] in out"); 71 } 72 73 74 private static final String NAMESPACES="http://xml.org/sax/features/namespaces"; 75 private static final String PREFIXES="http://xml.org/sax/features/namespace-prefixes"; 76 private static final File SCHEMA=new File ("./message-catalog.rng"); 77 78 void makeCatalog(InputSource is) throws ParserConfigurationException , SAXNotRecognizedException , SAXException , IOException { 79 final SAXParserFactory spf = new com.sun.msv.verifier.jaxp.SAXParserFactoryImpl(); 81 82 spf.setNamespaceAware(true); 83 spf.setValidating(false); 84 spf.setFeature(NAMESPACES, true); 85 spf.setFeature(PREFIXES, false); 86 87 final SAXParser p = spf.newSAXParser(); 88 p.setProperty("http://www.sun.com/xml/msv/schema", SCHEMA); 89 p.parse(is, this); 91 } 92 93 Properties getProperties(){ 94 return properties; 95 } 96 97 Properties properties; 98 99 public void startDocument() throws SAXException { 100 properties = new Properties (); 101 } 102 103 public void startElement(final String uri, 104 final String localName, 105 final String qName, 106 final Attributes attributes) 107 throws SAXException { 108 if (null == localName){ 109 throw new SAXException ("localName is null"); 110 } 111 112 if (localName.equals("messages")){ } else if (localName.equals("message")) { 114 startMessage(attributes); 115 } else { 116 throw new SAXException ("Unknown element: "+uri+":"+localName); 117 } 118 } 119 120 public void characters(char[] ch, 121 int start, 122 int length) 123 throws SAXException { 124 if (null != sb) { 125 sb.append(ch, start, length); 126 } 127 } 128 129 public void endElement(final String uri, 130 final String localName, 131 final String qName) throws SAXException { 132 if (localName.equals("messages")){ } else if (localName.equals("message")) { 134 endMessage(); 135 } else { 136 throw new SAXException ("Unknown element: "+uri+":"+localName); 137 } 138 } 139 140 141 private String key; 142 private StringBuffer sb; 143 144 private void startMessage(final Attributes attributes) throws SAXException { 145 key = (prefix != null ? prefix : "") + attributes.getValue("", "id"); 146 if (null == key){ 147 throw new SAXException ("message element had no id attribute"); 148 } 149 sb = new StringBuffer (); 150 } 151 152 private void endMessage() throws SAXException { 153 if (null != sb){ 154 properties.setProperty(key, sb.toString().trim()); 155 } 156 sb = null; 157 } 158 } 159 | Popular Tags |