1 package org.mmbase.util.magicfile; 2 3 import java.io.*; 4 import java.util.*; 5 6 import org.mmbase.util.*; 7 import org.mmbase.util.logging.*; 8 import org.mmbase.util.xml.DocumentReader; 9 import org.w3c.dom.Element ; 10 import org.xml.sax.InputSource ; 11 import edu.emory.mathcs.backport.java.util.concurrent.CopyOnWriteArrayList; 12 13 16 public class MagicXMLReader extends DocumentReader implements DetectorProvider { 17 18 private static Logger log = Logging.getLoggerInstance(MagicXMLReader.class); 19 20 private static MagicXMLReader reader = null; 21 protected static final String MAGICXMLFILE = "magic.xml"; 22 24 private static void setReader(String config) throws IllegalArgumentException { 25 try { 26 InputSource is = ResourceLoader.getConfigurationRoot().getInputSource(config); 27 if (is != null) { 28 reader = new MagicXMLReader(is); 29 } 30 } catch (IOException ie) { 31 log.warn(ie); 32 } 33 } 34 35 39 40 public synchronized static MagicXMLReader getInstance() { 41 if (reader == null) { 43 setReader(MAGICXMLFILE); 44 45 if (reader != null) { 46 log.info("Magic XML file is: " + reader.getSystemId()); 47 } 48 49 ResourceWatcher watcher = new ResourceWatcher() { 50 public void onChange(String file) { 51 setReader(file); 53 } 54 }; 55 watcher.start(); 56 watcher.add(MAGICXMLFILE); 57 58 } 59 return reader; 60 } 61 private List detectors = null; 62 63 private MagicXMLReader(InputSource is) { 64 super(is, MagicXMLReader.class); 65 } 66 67 public String getVersion() { 68 Element e = getElementByPath("magic.info.version"); 69 return getElementValue(e); 70 } 71 public String getAuthor() { 72 Element e = getElementByPath("magic.info.author"); 73 return getElementValue(e); 74 } 75 public String getDescription() { 76 Element e = getElementByPath("magic.info.description"); 77 return getElementValue(e); 78 } 79 80 83 public List getDetectors() { 84 if (detectors == null) { 85 detectors = new CopyOnWriteArrayList(); 86 Element e = getElementByPath("magic.detectorlist"); 87 if (e == null) { 88 log.fatal("Could not find magic/detectorlist in magic.xml"); 89 return detectors; 91 } 92 for (Iterator iter = getChildElements(e); iter.hasNext();) { 93 Element element = (Element ) iter.next(); 94 Detector d = getOneDetector(element); 95 detectors.add(d); 96 } 97 } 98 return detectors; 99 } 100 101 104 private String convertOctals(String s) { 105 int p = 0; 106 int stoppedAt = 0; 107 ByteArrayOutputStream buf = new ByteArrayOutputStream(); 108 char c; 109 try { 110 while (p < s.length()) { 111 c = s.charAt(p); 112 if (c == '\\') { 113 if (p > s.length() - 4) { 114 break; 116 } else { 117 char c0; 118 boolean failed = false; 119 for (int p0 = p + 1; p0 < p + 4; p0++) { 120 c0 = s.charAt(p0); 121 if (!((int)c0 >= '0' && (int) c0 <= '7')) { 122 failed = true; 123 } 124 } 125 if (!failed) { 126 byte[] bytes = s.substring(stoppedAt, p).getBytes("US-ASCII"); 127 buf.write(bytes, 0, bytes.length); 128 buf.write(Integer.parseInt(s.substring(p + 1, p + 4), 8)); 129 stoppedAt = p + 4; 130 p = p + 4; 131 } else { 132 p++; 133 } 134 } 135 } else { 136 p++; 137 } 138 } 139 byte[] bytes = s.substring(stoppedAt, p).getBytes("US-ASCII"); 140 buf.write(bytes, 0, bytes.length); 141 return buf.toString("US-ASCII"); 142 } catch (java.io.UnsupportedEncodingException use) { return ""; 144 } 145 } 146 147 private Detector getOneDetector(Element e) { 148 Detector d = new Detector(); 149 Element e1; 150 151 e1 = getElementByPath(e, "detector.mimetype"); 152 d.setMimeType(getElementValue(e1)); 153 154 e1 = getElementByPath(e, "detector.extension"); 155 d.setExtension(getElementValue(e1)); 156 157 e1 = getElementByPath(e, "detector.designation"); 158 d.setDesignation(getElementValue(e1)); 159 160 e1 = getElementByPath(e, "detector.test"); 161 if (e1 != null) { 162 d.setTest(convertOctals(getElementValue(e1))); 163 d.setOffset(getElementAttributeValue(e1, "offset")); 164 d.setType(getElementAttributeValue(e1, "type")); 165 String comparator = getElementAttributeValue(e1, "comparator"); 166 if (comparator.equals(">")) { 167 d.setComparator('>'); 168 } else if (comparator.equals("<")) { 169 d.setComparator('<'); 170 } else if (comparator.equals("&")) { 171 d.setComparator('&'); 172 } else if (comparator.length() == 1) { 173 d.setComparator(comparator.charAt(0)); 174 } else { 175 d.setComparator('='); 176 } 177 } 178 179 e1 = getElementByPath(e, "detector.childlist"); 180 if (e1 != null) { 181 for (Iterator iter = getChildElements(e1); iter.hasNext();) { 182 Element element = (Element ) iter.next(); 183 Detector child = getOneDetector(element); 184 d.addChild(child, 1); } 186 } 187 return d; 188 } 189 } 190 | Popular Tags |