KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jorm > mi2xml > lib > BasicDomWriter


1 /**
2  * JORM: an implementation of a generic mapping system for persistent Java
3  * objects. Two mapping are supported: to RDBMS and to binary files.
4  * Copyright (C) 2001-2003 France Telecom R&D - INRIA
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * Contact: jorm-team@objectweb.org
21  *
22  */

23
24 package org.objectweb.jorm.mi2xml.lib;
25
26 import org.objectweb.jorm.api.PException;
27 import org.objectweb.jorm.util.io.api.TargetHolder;
28 //import org.objectweb.jorm.util.io.api.PathExplorer;
29
import org.objectweb.jorm.util.api.Loggable;
30 import org.objectweb.jorm.api.JormConfigurator;
31 import org.objectweb.jorm.metainfo.api.*;
32 import org.objectweb.jorm.metainfo.api.Class;
33 import org.objectweb.jorm.metainfo.api.Package;
34 import org.objectweb.jorm.mi2xml.api.MappingDomtreeBuilder;
35 import org.objectweb.jorm.mi2xml.api.DomtreeBuilder;
36 import org.objectweb.jorm.mi2xml.api.Writer;
37 import org.objectweb.util.monolog.api.BasicLevel;
38 import org.objectweb.util.monolog.api.Logger;
39 import org.objectweb.util.monolog.api.LoggerFactory;
40
41 import java.util.Iterator JavaDoc;
42
43 import org.w3c.dom.Document JavaDoc;
44 import org.apache.xml.serialize.OutputFormat;
45 import org.apache.xml.serialize.XMLSerializer;
46
47 import java.io.*;
48
49 /**
50  * BasicDomWriter implements the Writer interface.
51  * BasicDomWriter generates the .pd file of a Class or a CompositeName object.
52  */

53 public class BasicDomWriter implements Writer, Loggable {
54
55     private static final String JavaDoc ENCODING = "ISO-8859-1";
56     private static final String JavaDoc DOCTYPE_PUBLIC =
57             "-//ObjectWeb Consortium//DTD JORM DEFINITIONS 2.0//EN";
58     private static final String JavaDoc JORM_DTD =
59             "http://www.objectweb.org/jorm/dtds/jorm2.dtd";
60
61     /**
62      * Default indentation for xml viewer.
63      */

64     public static final String JavaDoc XML_INDENT = "4";
65
66     //private PathExplorer pathExplorer = null;
67

68     private Logger logger = null;
69     private LoggerFactory loggerFactory = null;
70
71     private DomtreeBuilder domtreeBuilder;
72
73     /**
74      * Initializes a BasicDomWriter object.
75      * @param compilerConfigurator a JormConfiguratorImpl object.
76      */

77     public void init(JormConfigurator compilerConfigurator)
78             throws PException {
79         domtreeBuilder = new BasicDomtreeBuilder();
80         ((Loggable) domtreeBuilder).setLoggerFactory(loggerFactory);
81         ((Loggable) domtreeBuilder).setLogger(loggerFactory.getLogger("org.objectweb.jorm.mi2xml."));
82         Iterator JavaDoc it = compilerConfigurator.knownMappers();
83         while (it.hasNext()) {
84             String JavaDoc mappername = (String JavaDoc) it.next();
85             logger.log(BasicLevel.DEBUG,
86                        "Register specific mapping domtree builder for mapper [" + mappername + "]");
87             MappingDomtreeBuilder mdb = compilerConfigurator.getMappingDomtreeBuilder(mappername);
88             ((Loggable) mdb).setLoggerFactory(loggerFactory);
89             ((Loggable) mdb).setLogger(loggerFactory.getLogger("org.objectweb.jorm.mi2xml." + mappername));
90             domtreeBuilder.addMappingDomtreeBuilder(mappername, mdb);
91         }
92     }
93
94     /**
95      * This method first builds the DOMTree of a Class or a CompositeName object and
96      * then generates the .pd file by delegation to the XMLSerializer.
97      * @param mo a Class or a CompositeName object.
98      * @param holder The target holder which allows to create files.
99      * @exception PException if an error occurs during the generation.
100      */

101     public void write(MetaObject mo, TargetHolder holder)
102             throws PException {
103         String JavaDoc moFQName = null;
104         String JavaDoc moFileName = null;
105         String JavaDoc name = null;
106         if (mo instanceof Class JavaDoc) {
107             moFQName = ((Class JavaDoc) mo).getFQName();
108             moFileName = ((Class JavaDoc) mo).getFileName();
109             name = ((Class JavaDoc) mo).getName();
110         } else if (mo instanceof CompositeName) {
111             moFQName = ((CompositeName) mo).getFQName();
112             //fileName = ((CompositeName) mo).getFileName();
113
name = ((CompositeName) mo).getName();
114         }
115         logger.log(BasicLevel.DEBUG, "FQName():" + moFQName
116                                      + " / filename=[" + moFileName + "]");
117         logger.log(BasicLevel.DEBUG, "let's build the domtree !");
118         // generate the DOMTree for a Class or a CompositeName object.
119
Document JavaDoc domTree = domtreeBuilder.build(mo);
120         logger.log(BasicLevel.DEBUG, name + "DOMTree generated");
121         // File name
122
String JavaDoc fileName = File.separatorChar + name;
123         String JavaDoc packName = ((Package JavaDoc) mo.getParent()).getName();
124         if (packName != null && packName.length() > 0) {
125             fileName = packName + File.separatorChar + fileName;
126             fileName = fileName.replace('.', File.separatorChar);
127         }
128         logger.log(BasicLevel.DEBUG, "Generate " + fileName + ".pd");
129         write(domTree, holder, fileName);
130     }
131
132     /**
133      * This method generates a .pd file from a DOMTree object.
134      * @param domTree the DOMTree of a Class or a CompositeName object.
135      * @param holder The target holder which allows to create files.
136      * @exception PException if an error occurs during the generation.
137      */

138     private void write(Document JavaDoc domTree, TargetHolder holder, String JavaDoc fileName)
139             throws PException {
140         try {
141             // outputformat properties
142
OutputFormat outf = new OutputFormat(domTree);
143             outf.setIndenting(true);
144             outf.setIndent(Integer.parseInt(XML_INDENT));
145             outf.setOmitXMLDeclaration(false);
146             outf.setEncoding(ENCODING);
147             outf.setDoctype(DOCTYPE_PUBLIC, JORM_DTD);
148             FileWriter fw = holder.getFileWriter(fileName + ".pd");
149             BufferedWriter buf = new BufferedWriter(fw);
150             // serialize
151
XMLSerializer xmls = new XMLSerializer(buf, outf);
152             xmls.asDOMSerializer();
153             xmls.serialize(domTree);
154             fw.flush();
155             fw.close();
156             logger.log(BasicLevel.DEBUG, fileName + ".pd generated");
157         } catch (Exception JavaDoc e) {
158             throw new PException(e, "Problem while writing .pd file.");
159         }
160     }
161
162     ///////////////////////////////////////////////////////////////////
163
// from Loggable interface
164
///////////////////////////////////////////////////////////////////
165

166     /**
167      * Defines a logger object.
168      *
169      * @param logger the logger object
170      */

171     public void setLogger(Logger logger) {
172         this.logger = logger;
173     }
174
175     /**
176      * Defines the logger factory to obtain new loggers.
177      *
178      * @param loggerfactory The LoggerFactory object to obtain a logger object
179      */

180     public void setLoggerFactory(LoggerFactory loggerfactory) {
181         this.loggerFactory = loggerfactory;
182         if (logger == null && loggerFactory != null) {
183             logger = loggerFactory.getLogger("org.objectweb.jorm.mi2xml.rdb");
184         }
185     }
186
187     public Logger getLogger() {
188         return logger;
189     }
190
191     public LoggerFactory getLoggerFactory() {
192         return loggerFactory;
193     }
194 }
195
Popular Tags