001: package org.objectweb.speedo.generation.parser;
002:
003: import java.io.File;
004: import java.util.Iterator;
005: import java.util.Properties;
006:
007: import org.apache.tools.ant.types.DTDLocation;
008: import org.objectweb.jorm.xml2mi.lib.SAXParserHelper;
009: import org.objectweb.speedo.api.SpeedoException;
010: import org.objectweb.speedo.api.SpeedoProperties;
011: import org.objectweb.speedo.generation.lib.AbstractGeneratorComponent;
012: import org.objectweb.speedo.lib.Personality;
013: import org.objectweb.speedo.metadata.SpeedoClass;
014: import org.objectweb.speedo.metadata.SpeedoPackage;
015: import org.objectweb.speedo.metadata.SpeedoXMLDescriptor;
016: import org.objectweb.util.monolog.api.BasicLevel;
017: import org.w3c.dom.Document;
018: import org.w3c.dom.Node;
019: import org.xml.sax.SAXException;
020:
021: public abstract class AbstractParser extends AbstractGeneratorComponent {
022: public static String LOGGER_NAME = SpeedoProperties.LOGGER_NAME
023: + ".generation.parser.";
024: SAXParserHelper parser;
025: int parsedfiles;
026:
027: abstract protected String getLoggerName();
028:
029: public AbstractParser(Personality p) {
030: super (p);
031: }
032:
033: /**
034: * Create the XML file descriptor.
035: * It throws an exception if the XML file doesn't follow the relevant dtd.
036: * @param node root node of the descriptor which will be return.
037: * @param o default descriptor to return.
038: * @return descriptor.
039: */
040: abstract protected Object treatDocument(Node node, Object o)
041: throws SpeedoException;
042:
043: /**
044: * The parser instance is reused at each process method call
045: */
046:
047: // IMPLEMENTATION OF THE GeneratorComponent INTERFACE //
048: //----------------------------------------------------//
049: public boolean init() throws SpeedoException {
050: logger = scp.loggerFactory.getLogger(LOGGER_NAME
051: + getLoggerName());
052: logger.log(BasicLevel.DEBUG, "- " + getLoggerName()
053: + " XML descriptor Parsing -");
054: if (scp.xml.isEmpty())
055: return false;
056: Properties p = new Properties();
057: for (Iterator iter = scp.dtdLocations.iterator(); iter
058: .hasNext();) {
059: DTDLocation element = (DTDLocation) iter.next();
060: p.setProperty(element.getPublicId(), element.getLocation());
061: }
062: try {
063: parser = new SAXParserHelper(p, logger, getClass()
064: .getClassLoader(), false);
065: } catch (SAXException e) {
066: throw new SpeedoException(e);
067: }
068: parsedfiles = 0;
069: return true;
070: }
071:
072: public void process() throws SpeedoException {
073: if (scp.xml.isEmpty())
074: return;
075: logger
076: .log(BasicLevel.DEBUG, scp.xml.size()
077: + " files to parse");
078: for (Iterator it = scp.xml.iterator(); it.hasNext();) {
079: String xmlFileName = (String) it.next();
080: logger.log(BasicLevel.DEBUG, "Parse " + xmlFileName);
081: scp.getXmldescriptor().put(xmlFileName,
082: createObjectModel(xmlFileName));
083: }
084: parsedfiles += scp.xml.size();
085: }
086:
087: public String getTitle() {
088: return "Loading " + getLoggerName() + " descriptor files...";
089: }
090:
091: public String getSummary() {
092: return parsedfiles + " descriptor(s) parsed";
093: }
094:
095: // PRIVATE METHODS //
096: //-----------------//
097:
098: /**
099: * This method constructs an object descriptor from a XML file.
100: * It returns the new object descriptor.
101: * If the file isn't well formed, a SpeedoXMLError is thrown.
102: * @param xmlFile file name.
103: * @return object descriptor for this file
104: * @exception SpeedoException if the XML file doesn't follow the
105: * relevant dtd.
106: */
107: private SpeedoXMLDescriptor createObjectModel(String xmlFile)
108: throws SpeedoException {
109: try {
110: // parse the document
111: Document document = parser.parse(scp.xmlDir
112: + File.separator + xmlFile);
113: //create a descriptor for the document
114: SpeedoXMLDescriptor desc = new SpeedoXMLDescriptor(scp.smi);
115: desc.xmlFile = xmlFile;
116: //fill and return the file descriptor
117: return (SpeedoXMLDescriptor) treatDocument(document, desc);
118: } catch (SpeedoException e) {
119: throw e;
120: } catch (Exception e) {
121: String msg = "Error with the construction of file "
122: + scp.xmlDir + File.separator + xmlFile;
123: logger.log(BasicLevel.ERROR, msg);
124: throw new SpeedoException(msg, e);
125: }
126: }
127:
128: }
|