1 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.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 ; 42 43 import org.w3c.dom.Document ; 44 import org.apache.xml.serialize.OutputFormat; 45 import org.apache.xml.serialize.XMLSerializer; 46 47 import java.io.*; 48 49 53 public class BasicDomWriter implements Writer, Loggable { 54 55 private static final String ENCODING = "ISO-8859-1"; 56 private static final String DOCTYPE_PUBLIC = 57 "-//ObjectWeb Consortium//DTD JORM DEFINITIONS 2.0//EN"; 58 private static final String JORM_DTD = 59 "http://www.objectweb.org/jorm/dtds/jorm2.dtd"; 60 61 64 public static final String XML_INDENT = "4"; 65 66 68 private Logger logger = null; 69 private LoggerFactory loggerFactory = null; 70 71 private DomtreeBuilder domtreeBuilder; 72 73 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 it = compilerConfigurator.knownMappers(); 83 while (it.hasNext()) { 84 String mappername = (String ) 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 101 public void write(MetaObject mo, TargetHolder holder) 102 throws PException { 103 String moFQName = null; 104 String moFileName = null; 105 String name = null; 106 if (mo instanceof Class ) { 107 moFQName = ((Class ) mo).getFQName(); 108 moFileName = ((Class ) mo).getFileName(); 109 name = ((Class ) mo).getName(); 110 } else if (mo instanceof CompositeName) { 111 moFQName = ((CompositeName) mo).getFQName(); 112 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 Document domTree = domtreeBuilder.build(mo); 120 logger.log(BasicLevel.DEBUG, name + "DOMTree generated"); 121 String fileName = File.separatorChar + name; 123 String packName = ((Package ) 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 138 private void write(Document domTree, TargetHolder holder, String fileName) 139 throws PException { 140 try { 141 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 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 e) { 158 throw new PException(e, "Problem while writing .pd file."); 159 } 160 } 161 162 166 171 public void setLogger(Logger logger) { 172 this.logger = logger; 173 } 174 175 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 |