001: /*
002:
003: This software is OSI Certified Open Source Software.
004: OSI Certified is a certification mark of the Open Source Initiative.
005:
006: The license (Mozilla version 1.0) can be read at the MMBase site.
007: See http://www.MMBase.org/license
008:
009: */
010: package org.mmbase.util.xml;
011:
012: import java.util.*;
013: import org.w3c.dom.Document;
014: import org.w3c.dom.Element;
015: import org.xml.sax.InputSource;
016:
017: import org.mmbase.util.LocalizedString;
018: import org.mmbase.util.XMLEntityResolver;
019:
020: /**
021: * @javadoc
022: * @since MMBase-1.8
023: * @author Daniel Ockeloen
024: * @author Pierre van Rooden
025: * @version $Id: ModuleReader.java,v 1.8 2007/06/19 14:00:54 michiel Exp $
026: */
027: public class ModuleReader extends DocumentReader {
028:
029: /** Public ID of the Module DTD version 1.0 */
030: public static final String PUBLIC_ID_MODULE_1_0 = "-//MMBase//DTD module config 1.0//EN";
031: private static final String PUBLIC_ID_MODULE_1_0_FAULT = "-//MMBase/DTD module config 1.0//EN";
032: private static final String PUBLIC_ID_MODULE_1_0_FAULT2 = "-//MMBase/ DTD module config 1.0//EN";
033: /** Public ID of the most recent Module DTD */
034: public static final String PUBLIC_ID_MODULE = PUBLIC_ID_MODULE_1_0;
035:
036: /** DTD resource filename of the most recent Module DTD */
037: public static final String DTD_MODULE_1_0 = "module_1_0.dtd";
038: /** DTD resource filename of the most recent Module DTD */
039: public static final String DTD_MODULE = DTD_MODULE_1_0;
040:
041: public static final String XSD_MODULE_2_0 = "module.xsd";
042: public static final String NAMESPACE_MODULE_2_0 = "http://www.mmbase.org/xmlns/module";
043: public static final String NAMESPACE_MODULE = NAMESPACE_MODULE_2_0;
044:
045: /**
046: * Register the namespace and XSD used by DataTypeConfigurer
047: * This method is called by XMLEntityResolver.
048: */
049: public static void registerSystemIDs() {
050: XMLEntityResolver.registerSystemID(NAMESPACE_MODULE_2_0
051: + ".xsd", XSD_MODULE_2_0, ModuleReader.class);
052: }
053:
054: /**
055: * Register the Public Ids for DTDs used by ModuleReader
056: * This method is called by XMLEntityResolver.
057: * @since MMBase-1.7
058: */
059: public static void registerPublicIDs() {
060: XMLEntityResolver.registerPublicID(PUBLIC_ID_MODULE_1_0,
061: DTD_MODULE_1_0, ModuleReader.class);
062: // legacy public IDs (wrong, don't use these)
063: XMLEntityResolver.registerPublicID(PUBLIC_ID_MODULE_1_0_FAULT,
064: DTD_MODULE_1_0, ModuleReader.class);
065: XMLEntityResolver.registerPublicID(PUBLIC_ID_MODULE_1_0_FAULT2,
066: DTD_MODULE_1_0, ModuleReader.class);
067: }
068:
069: public ModuleReader(InputSource is) {
070: super (is, ModuleReader.class);
071: }
072:
073: /**
074: * @since MMBase-1.8
075: */
076: public ModuleReader(Document doc) {
077: super (doc);
078: }
079:
080: /**
081: * Get the status of this module
082: */
083: public String getStatus() {
084: Element e = getElementByPath("module.status");
085: String s = getElementValue(e);
086: return s.equals("") ? "active" : s;
087: }
088:
089: /**
090: * Get the version of this module
091: */
092: public int getVersion() {
093: Element e = getElementByPath("module");
094: String version = getElementAttributeValue(e, "version");
095: int n = 0;
096: if (version == null) {
097: return n;
098: } else {
099: try {
100: n = Integer.parseInt(version);
101: } catch (Exception f) {
102: n = 0;
103: }
104: return n;
105: }
106: }
107:
108: /**
109: * Get the name of this module.
110: * Returns <code>null</code> if no name is found.
111: * @since MMBase-1.9
112: */
113: public String getName() {
114: Element e = getElementByPath("module");
115: String tmp = getElementAttributeValue(e, "name");
116: if (tmp != null && !tmp.equals("")) {
117: return tmp;
118: } else {
119: return null;
120: }
121: }
122:
123: /**
124: * Get the maintainer of this module
125: */
126: public String getMaintainer() {
127: Element e = getElementByPath("module");
128: String tmp = getElementAttributeValue(e, "maintainer");
129: if (tmp != null && !tmp.equals("")) {
130: return tmp;
131: } else {
132: return "mmbase.org";
133: }
134: }
135:
136: /**
137: * The name of the class which is implementing this Module.
138: */
139: public String getClassName() {
140: Element e = getElementByPath("module.class");
141: if (e != null)
142: return getElementValue(e);
143: // legacy fall back
144: e = getElementByPath("module.classfile");
145: return getElementValue(e);
146:
147: }
148:
149: /**
150: * get the optional resource url for the module
151: * @return the url of the resource or null if no url was defined
152: **/
153: public String getURLString() {
154: Element e = getElementByPath("module.url");
155: if (e != null) {
156: return getElementValue(e);
157: }
158: return null;
159: }
160:
161: /**
162: * Get the descriptions of this module.
163: * @return the descriptions as a LocalizedString
164: */
165: public LocalizedString getLocalizedDescription(
166: LocalizedString description) {
167: description.fillFromXml("description",
168: getElementByPath("module.descriptions"));
169: return description;
170: }
171:
172: /**
173: * Get the (gui) names of this module.
174: * @return the names as a LocalizedString
175: */
176: public LocalizedString getLocalizedGUIName(LocalizedString guiName) {
177: guiName.fillFromXml("name", getElementByPath("module.names"));
178: return guiName;
179: }
180:
181: /**
182: * Get the properties of this builder
183: */
184: public Map<String, String> getProperties() {
185: Map<String, String> results = new LinkedHashMap<String, String>();
186: for (Element el : getChildElements("module.properties",
187: "property")) {
188: String name = getElementAttributeValue(el, "name");
189: results.put(name, getElementValue(el));
190: }
191: return results;
192: }
193:
194: }
|