1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package net.jot.xml; import java.io.File; import java.util.Hashtable; import java.util.Vector; import java.util.regex.Matcher; import java.util.regex.Pattern; import net.jot.utils.JOTPair; import net.jot.web.view.JOTViewParser; /** * http://en.wikipedia.org/wiki/XML * http://en.wikipedia.org/wiki/CDATA#CDATA_sections_in_XML * TODO deal with <blah>toto<blah> * TODO deal with <blah name="5">toto</blah> * TODO deal with special chars: & etc... * TODO deal with numbers: &8 * TODO deal with CDATA (<![CDATA[ ... ]]>) * etc... * * * Used by JOTXML to parse xml text into XML text/elements * Kept separated for clarity. * We completely ignore declarative stuff (and DTD etc...) * http://en.wikipedia.org/wiki/XML * @author thibautc */ public class JOTXMLParser { protected static final int PATTERN_FLAGS = Pattern.CASE_INSENSITIVE | Pattern.DOTALL | Pattern.MULTILINE | Pattern.CANON_EQ; protected static final Pattern PATTERN_TAG = Pattern.compile("(<([^> ]*)[^>]*>)(.*)", PATTERN_FLAGS); protected static final Pattern OPEN_TAG_PATTERN = Pattern.compile("[^> ]*[^>]*>", PATTERN_FLAGS); protected static final Pattern PATTERN_COMMENT = Pattern.compile("<\\!--.*?-->", PATTERN_FLAGS); protected static final Pattern PATTERN_DECLARATION = Pattern.compile("<\\?.*?\\?>", PATTERN_FLAGS); private final static Pattern PATTERN_PIECES = Pattern.compile("__JOT_XML_PIECE_\\d+__"); private final static Pattern PATTERN_ATTRIB = Pattern.compile("(\\S+)\\s*=['\"]+(.*?)['\"]+"); private Hashtable pieces = new Hashtable(); private Vector elements = new Vector(); private boolean rootFound=false; private JOTXMLParser() { } protected static Vector parse(StringBuffer xmlBuf) throws JOTXMLException { JOTXMLParser parser = new JOTXMLParser(); // remove comments xmlBuf = parser.doTextItem(PATTERN_COMMENT, xmlBuf); // and declarations (we ignore declarative stuff (and DTD etc...)) xmlBuf = parser.doTextItem(PATTERN_DECLARATION, xmlBuf); String xml = xmlBuf.toString(); parser.doTags(xml, null); // TODO: put back the comments etc... // TODO parser.dumpElements(parser.elements, ""); return parser.elements; } private void addElement(JOTXMLElement parent, JOTXMLElement element) { rootFound=true; if (parent != null) { parent.addItem(element); } else { elements.add(element); } } private void addText(JOTXMLElement parent,String text) { if(text.length()>0) { JOTXMLTextElement element=new JOTXMLTextElement(reinsertPieces(text)); if (parent != null) { parent.addItem(element); } else { elements.add(element); } } } private void doAttributes(JOTXMLElement parent, String openingTag) { Matcher m=PATTERN_ATTRIB.matcher(openingTag); while(m.find()) { String name=m.group(1); String val=m.group(2); //TODO: numbers ? JOTXMLElement el=new JOTXMLElement(name); el.setValue(val); parent.addItem(el); } } private StringBuffer doTextItem(Pattern pattern, StringBuffer xml) { StringBuffer newBuf = new StringBuffer(); Matcher m = pattern.matcher(xml); while (m.find()) { String key = getNextKey(); pieces.put(key, m.group(0)); JOTViewParser.safeAppendReplacement(m, newBuf, key); } m.appendTail(newBuf); return newBuf; } private String getNextKey() { String key = "__JOT_XML_PIECE_" + pieces.size() + "__"; return key; } /** * * @param buffer * @param curElem null = first - root * @return * @throws java.lang.Exception */ private int doTags(String xml, JOTXMLElement parent) throws JOTXMLException { Matcher m = PATTERN_TAG.matcher(xml); int lastElemIndex=0; int index = 0; while (m.find(index)) { index=m.start(); String openingTag = m.group(1); String tagName = m.group(2).trim(); String restOfTemplate = m.group(3); String closeTagString = "</\\s*" + /*Pattern.quote(*/ tagName/*)*/ + "\\s*>"; Pattern closeTag = Pattern.compile(closeTagString, PATTERN_FLAGS); Pattern open = Pattern.compile(/*Pattern.quote(*/"<" + tagName/*)*/ + "\\s+"); // a closing tag if (openingTag.startsWith("</")) { index += openingTag.length(); lastElemIndex=index; continue; } if (parent==null && rootFound) { throw (new JOTXMLException("There should be only one ROOT element in the xml file. Found second one: " + openingTag)); } addText(parent,xml.substring(lastElemIndex,index)); boolean oneLiner = openingTag.endsWith("/>"); int i = 0; if (!oneLiner) { JOTPair pair=JOTViewParser.findMatchingClosingTag(0, restOfTemplate, open, closeTag); i=pair.getX(); if (i == -1) { throw new JOTXMLException("Couldn't find closing tag (" + closeTag + ") for:" + open + " in " + restOfTemplate); } index += pair.getY(); } else { index += openingTag.length(); } JOTXMLElement element = new JOTXMLElement(tagName); //String desc = openingTag.length() > 40 ? openingTag.substring(0, 40) : openingTag; //System.out.println((oneLiner ? "1" : "") + "TAG: " + desc); if (!oneLiner) { String tagContent = restOfTemplate.substring(0, i); doTags(tagContent, element); } lastElemIndex=index; addElement(parent, element); doAttributes(element,openingTag); } addText(parent,xml.substring(lastElemIndex)); return index; } private void dumpElements(Vector elements, String curPath) { for (int i = 0; i != elements.size(); i++) { Object o = elements.get(i); if(o instanceof JOTXMLTextElement) { JOTXMLTextElement el=(JOTXMLTextElement)o; System.out.println(el.getText()); } else if(o instanceof JOTXMLElement) { JOTXMLElement el = (JOTXMLElement) elements.get(i); String newPath = curPath + (curPath.length() > 0 ? "." : "") + el.getName(); String val=el.getValue()!=null?" = '"+el.getValue()+"'":""; System.out.println(newPath+val); dumpElements(el.getAllItems(), newPath); } } } private String reinsertPieces(String buf) { StringBuffer newBuf = new StringBuffer(); Matcher m = PATTERN_PIECES.matcher(buf); while (m.find()) { String value = (String) pieces.get(m.group()); JOTViewParser.safeAppendReplacement(m, newBuf, value); } m.appendTail(newBuf); return newBuf.toString(); } /** * For testing * @param args */ public static void main(String[] args) { String[] files = { "/tmp/xml/config.xml" }; for (int i = 0; i != files.length; i++) { File f = new File(files[i]); try { StringBuffer xml = JOTXML.readXmlFrom(f); JOTXMLParser.parse(xml); } catch (Exception e) { e.printStackTrace(); System.exit(-1); } } } }