1 package org.apache.fulcrum.intake.transform; 2 3 56 57 import java.io.BufferedReader ; 58 import java.io.FileReader ; 59 60 import org.apache.fulcrum.intake.xmlmodel.AppData; 61 import org.apache.fulcrum.intake.xmlmodel.Rule; 62 import org.apache.fulcrum.intake.xmlmodel.XmlField; 63 import org.apache.fulcrum.intake.xmlmodel.XmlGroup; 64 import org.apache.xerces.parsers.SAXParser; 65 import org.xml.sax.Attributes ; 66 import org.xml.sax.InputSource ; 67 import org.xml.sax.SAXParseException ; 68 import org.xml.sax.helpers.DefaultHandler ; 69 70 78 public class XmlToAppData extends DefaultHandler 79 { 80 private AppData app; 81 private XmlGroup currGroup; 82 private XmlField currField; 83 private Rule currRule; 84 private String currElement; 85 86 89 public XmlToAppData() 90 { 91 app = new AppData(); 92 } 93 94 95 99 public AppData parseFile(String xmlFile) 100 throws Exception 101 { 102 return parseFile(xmlFile, false); 103 } 104 105 109 public AppData parseFile(String xmlFile, boolean skipValidation) 110 throws Exception 111 { 112 SAXParser parser = new SAXParser(); 113 114 DTDResolver dtdResolver = new DTDResolver(); 116 parser.setEntityResolver(dtdResolver); 117 118 parser.setContentHandler(this); 120 parser.setErrorHandler(this); 121 122 parser.setFeature( 124 "http://apache.org/xml/features/validation/dynamic", true); 125 parser.setFeature("http://xml.org/sax/features/validation", true); 126 127 FileReader fr = new FileReader (xmlFile); 128 BufferedReader br = new BufferedReader (fr); 129 try 130 { 131 InputSource is = new InputSource (br); 132 parser.parse(is); 133 } 134 finally 135 { 136 br.close(); 137 } 138 139 return app; 140 } 141 142 143 144 147 public void startElement(String uri, String localName, 148 String rawName, Attributes attributes) 149 { 150 currElement = rawName; 151 if (rawName.equals("input-data")) 152 { 153 app.loadFromXML(attributes); 154 } 155 else if (rawName.equals("group")) 156 { 157 currGroup = app.addGroup(attributes); 158 } 159 else if (rawName.equals("field")) 160 { 161 currField = currGroup.addField(attributes); 162 } 163 else if (rawName.equals("rule")) 164 { 165 currRule = currField.addRule(attributes); 166 } 167 } 168 169 170 174 public void characters(char[] mesgArray, int start, int length) 175 { 176 String cdata = new String (mesgArray, start, length).trim(); 177 if ( "rule".equals(currElement) && cdata.length() > 0) 178 { 179 currRule.setMessage(cdata); 180 } 181 if ( "required-message".equals(currElement) && cdata.length() > 0) 182 { 183 currField.setIfRequiredMessage(cdata); 184 } 185 } 186 187 192 public void warning(SAXParseException spe) 193 { 194 System.out.println("Warning Line: " + spe.getLineNumber() + 195 " Row: " + spe.getColumnNumber() + 196 " Msg: " + spe.getMessage()); 197 } 198 199 204 public void error(SAXParseException spe) 205 { 206 System.out.println("Error Line: " + spe.getLineNumber() + 207 " Row: " + spe.getColumnNumber() + 208 " Msg: " + spe.getMessage()); 209 } 210 211 216 public void fatalError(SAXParseException spe) 217 { 218 System.out.println("Fatal Error Line: " + spe.getLineNumber() + 219 " Row: " + spe.getColumnNumber() + 220 " Msg: " + spe.getMessage()); 221 } 222 } 223 | Popular Tags |