1 17 18 package com.finalist.jag.taglib; 19 20 import com.finalist.jag.util.Log; 21 import org.w3c.dom.Document ; 22 import org.w3c.dom.Element ; 23 import org.w3c.dom.NodeList ; 24 import org.xml.sax.InputSource ; 25 import org.xml.sax.SAXException ; 26 27 import javax.xml.parsers.DocumentBuilderFactory ; 28 import javax.xml.parsers.DocumentBuilder ; 29 import java.io.File ; 30 import java.io.FileInputStream ; 31 import java.io.IOException ; 32 import java.io.InputStreamReader ; 33 import java.util.ArrayList ; 34 import java.util.Collection ; 35 36 43 public class TagLibraryLoader { 44 45 46 private Document doc = null; 47 48 49 private Exception exception = null; 50 51 52 57 public TagLibraryLoader(File config) { 58 Log.log("loading: " + config.getPath()); 59 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 60 DocumentBuilder builder = null; 61 try { 62 builder = dbf.newDocumentBuilder(); 63 doc = builder.parse(config); 64 } catch (Exception e) { 65 e.printStackTrace(); 66 } 67 } 68 69 70 77 public TagLibrary getTagLibrary() { 78 79 if (doc == null) 80 return null; 81 Element root = doc.getDocumentElement(); 82 83 TagLibrary library = parseTagLibrary(root); 84 return library; 85 } 86 87 88 97 private TagLibrary parseTagLibrary(org.w3c.dom.Element root) { 98 TagLibrary tagLibrary = new TagLibrary(); 99 100 tagLibrary.setShortName(getAttribute(root, "shortname")); 101 tagLibrary.setLibVersion(getAttribute(root, "tlibversion")); 102 tagLibrary.setJagVersion(getAttribute(root, "jagversion")); 103 tagLibrary.setInfo(getAttribute(root, "info")); 104 tagLibrary.setTagDefs(getTagDefs(root, "tag")); 105 106 return tagLibrary; 107 } 108 109 110 120 private Collection getTagDefs(org.w3c.dom.Element root, String label) { 121 ArrayList defList = new ArrayList (); 122 NodeList list = root.getElementsByTagName(label); 123 124 for (int i = 0; i < list.getLength(); i++) { 125 org.w3c.dom.Element tagnode = (org.w3c.dom.Element ) list.item(i); 126 TagDef tagDef = new TagDef(); 127 128 tagDef.setName(getAttribute(tagnode, "name")); 129 tagDef.setTagClass(getAttribute(tagnode, "tagclass")); 130 tagDef.setBodyContent(getAttribute(tagnode, "bodycontent")); 131 tagDef.setAttributeDefs(getAttributeDefs(tagnode, "attribute")); 132 defList.add(tagDef); 133 } 134 135 return defList; 136 } 137 138 139 149 private Collection getAttributeDefs(org.w3c.dom.Element root, String label) { 150 ArrayList defList = new ArrayList (); 151 NodeList list = root.getElementsByTagName(label); 152 153 for (int i = 0; i < list.getLength(); i++) { 154 org.w3c.dom.Element tagnode = (org.w3c.dom.Element ) list.item(i); 155 AttributeDef attrDef = new AttributeDef(); 156 157 attrDef.setName(getAttribute(tagnode, "name")); 158 attrDef.setRequired(getAttribute(tagnode, "required")); 159 defList.add(attrDef); 160 } 161 162 return defList; 163 } 164 165 166 176 private String getAttribute(org.w3c.dom.Element root, String label) { 177 String sAttribute = root.getAttribute(label); 178 179 if ((sAttribute == null) || (sAttribute.length() < 1)) { 180 NodeList list = root.getElementsByTagName(label); 181 182 if (list.getLength() > 0) { 183 org.w3c.dom.Element node = (org.w3c.dom.Element ) list.item(0); 184 if (node.getFirstChild() != null) 185 sAttribute = node.getFirstChild().getNodeValue(); 186 } 187 } 188 return sAttribute; 189 } 190 191 192 199 public Exception getException() { 200 return exception; 201 } 202 } | Popular Tags |