KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > util > magicfile > MagicXMLReader


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 JavaDoc;
10 import org.xml.sax.InputSource JavaDoc;
11 import edu.emory.mathcs.backport.java.util.concurrent.CopyOnWriteArrayList;
12
13 /**
14  * Reads <config>/magic.xml
15  */

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 JavaDoc MAGICXMLFILE = "magic.xml";
22     // Name of the XML magic file - should reside in top config dir
23

24     private static void setReader(String JavaDoc config) throws IllegalArgumentException JavaDoc {
25         try {
26             InputSource JavaDoc 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     /**
36      * Gets the one MagicXMLReader (there can only be one).
37      * @return MagicXMLReader if mmbase was staterd or null if mmbase was not started
38      */

39
40     public synchronized static MagicXMLReader getInstance() {
41         if (reader == null) { // can only occur once.
42

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 JavaDoc file) {
51                         // reader is replaced on every change of magic.xml
52
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 JavaDoc is) {
64         super(is, MagicXMLReader.class);
65     }
66
67     public String JavaDoc getVersion() {
68         Element JavaDoc e = getElementByPath("magic.info.version");
69         return getElementValue(e);
70     }
71     public String JavaDoc getAuthor() {
72         Element JavaDoc e = getElementByPath("magic.info.author");
73         return getElementValue(e);
74     }
75     public String JavaDoc getDescription() {
76         Element JavaDoc e = getElementByPath("magic.info.description");
77         return getElementValue(e);
78     }
79
80     /**
81      * Returns all 'Detectors'.
82      */

83     public List getDetectors() {
84         if (detectors == null) {
85             detectors = new CopyOnWriteArrayList();
86             Element JavaDoc e = getElementByPath("magic.detectorlist");
87             if (e == null) {
88                 log.fatal("Could not find magic/detectorlist in magic.xml");
89                 // aargh!
90
return detectors;
91             }
92             for (Iterator iter = getChildElements(e); iter.hasNext();) {
93                 Element JavaDoc element = (Element JavaDoc) iter.next();
94                 Detector d = getOneDetector(element);
95                 detectors.add(d);
96             }
97         }
98         return detectors;
99     }
100
101     /**
102      * Replaces octal representations of bytes, written as \ddd to actual byte values.
103      */

104     private String JavaDoc convertOctals(String JavaDoc 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                         // Can't be a full octal representation here, let's cut it off
115
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 JavaDoc use) { // could not happen US-ASCII is supported
143
return "";
144         }
145     }
146
147     private Detector getOneDetector(Element JavaDoc e) {
148         Detector d = new Detector();
149         Element JavaDoc 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 JavaDoc comparator = getElementAttributeValue(e1, "comparator");
166             if (comparator.equals("&gt;")) {
167                 d.setComparator('>');
168             } else if (comparator.equals("&lt;")) {
169                 d.setComparator('<');
170             } else if (comparator.equals("&amp;")) {
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 JavaDoc element = (Element JavaDoc) iter.next();
183                 Detector child = getOneDetector(element);
184                 d.addChild(child, 1); // Not sure if this is the right thing
185
}
186         }
187         return d;
188     }
189 }
190
Popular Tags