OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libjava/] [classpath/] [tools/] [gnu/] [classpath/] [tools/] [doclets/] [xmldoclet/] [doctranslet/] [DocTranslet.java] - Blame information for rev 779

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 779 jeremybenn
/* gnu.classpath.tools.doclets.xmldoclet.doctranslet.DocTranslet
2
   Copyright (C) 2001 Free Software Foundation, Inc.
3
 
4
This file is part of GNU Classpath.
5
 
6
GNU Classpath is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 2, or (at your option)
9
any later version.
10
 
11
GNU Classpath is distributed in the hope that it will be useful, but
12
WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
General Public License for more details.
15
 
16
You should have received a copy of the GNU General Public License
17
along with GNU Classpath; see the file COPYING.  If not, write to the
18
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19
02111-1307 USA.
20
 
21
Linking this library statically or dynamically with other modules is
22
making a combined work based on this library.  Thus, the terms and
23
conditions of the GNU General Public License cover the whole
24
combination.
25
 
26
As a special exception, the copyright holders of this library give you
27
permission to link this library with independent modules to produce an
28
executable, regardless of the license terms of these independent
29
modules, and to copy and distribute the resulting executable under
30
terms of your choice, provided that you also meet, for each linked
31
independent module, the terms and conditions of the license of that
32
module.  An independent module is a module which is not derived from
33
or based on this library.  If you modify this library, you may extend
34
this exception to your version of the library, but you are not
35
obligated to do so.  If you do not wish to do so, delete this
36
exception statement from your version. */
37
 
38
package gnu.classpath.tools.doclets.xmldoclet.doctranslet;
39
 
40
import java.io.ByteArrayInputStream;
41
import java.io.ByteArrayOutputStream;
42
import java.io.File;
43
import java.io.FileOutputStream;
44
import java.io.InputStream;
45
import java.io.IOException;
46
import java.io.OutputStream;
47
import java.io.PrintStream;
48
 
49
import java.net.MalformedURLException;
50
import java.net.URL;
51
 
52
import java.util.ArrayList;
53
import java.util.Iterator;
54
import java.util.List;
55
import java.util.HashMap;
56
import java.util.Map;
57
 
58
import java.util.jar.JarFile;
59
import java.util.jar.Manifest;
60
import java.util.jar.Attributes;
61
 
62
import javax.xml.transform.ErrorListener;
63
import javax.xml.transform.Source;
64
import javax.xml.transform.Transformer;
65
import javax.xml.transform.TransformerException;
66
import javax.xml.transform.TransformerFactory;
67
import javax.xml.transform.TransformerFactoryConfigurationError;
68
import javax.xml.transform.URIResolver;
69
 
70
import javax.xml.transform.dom.DOMResult;
71
 
72
import javax.xml.transform.stream.StreamResult;
73
import javax.xml.transform.stream.StreamSource;
74
 
75
import javax.xml.parsers.DocumentBuilderFactory;
76
import javax.xml.parsers.DocumentBuilder;
77
import javax.xml.parsers.ParserConfigurationException;
78
 
79
import org.w3c.dom.Document;
80
import org.w3c.dom.Element;
81
import org.w3c.dom.Node;
82
import org.w3c.dom.NodeList;
83
 
84
import org.xml.sax.SAXException;
85
 
86
import gnu.classpath.tools.IOToolkit;
87
import gnu.classpath.tools.doclets.xmldoclet.Driver;
88
 
89
import com.sun.javadoc.DocErrorReporter;
90
 
91
public class DocTranslet implements ErrorListener {
92
 
93
   private static class DocErrorReporterOutputStream
94
      extends OutputStream
95
   {
96
      private ByteArrayOutputStream out = new ByteArrayOutputStream();
97
      private DocErrorReporter reporter;
98
 
99
      public DocErrorReporterOutputStream(DocErrorReporter reporter) {
100
         this.reporter = reporter;
101
      }
102
 
103
      public void write(int ch) {
104
         out.write(ch);
105
         if (ch == 10) {
106
            reporter.printNotice(out.toString());
107
            out.reset();
108
         }
109
      }
110
   }
111
 
112
   private String mainResourceFilename;
113
   private ClassLoader classLoader;
114
   private Map transformerMap = new java.util.HashMap(); //WeakHashMap();
115
   private DocTransletOptions options;
116
 
117
   protected DocTranslet(String mainResourceFilename,
118
                         ClassLoader classLoader)
119
      throws DocTransletConfigurationException {
120
 
121
      if (mainResourceFilename.length() > 0 && mainResourceFilename.charAt(0) == '/') {
122
         mainResourceFilename = mainResourceFilename.substring(1);
123
      }
124
      this.mainResourceFilename = mainResourceFilename;
125
      this.classLoader = classLoader;
126
   }
127
 
128
   private static boolean equalsFile(File file1, File file2) {
129
      return file1.getAbsolutePath().equals(file2.getAbsolutePath());
130
   }
131
 
132
   private static File getParentFile(File file) {
133
      String filename = file.getAbsolutePath();
134
      if (filename.endsWith(File.separator)) {
135
         filename = filename.substring(0, filename.length() - 1);
136
      }
137
      int lastSlash = filename.lastIndexOf(File.separatorChar);
138
      if (lastSlash > 0) {
139
         filename = filename.substring(0, lastSlash);
140
      }
141
      else {
142
         filename = File.separator;
143
      }
144
 
145
      return new File(filename);
146
   }
147
 
148
   private static boolean cacheXSLTSheets = true;
149
 
150
   public void apply(File xmlSourceDirectory, File targetDirectory,
151
                     DocErrorReporter reporter)
152
      throws DocTransletException {
153
 
154
      PrintStream err = System.err;
155
 
156
      try{
157
         URL mainResourceURL = classLoader == null ?
158
             ClassLoader.getSystemResource(mainResourceFilename):
159
             classLoader.getResource(mainResourceFilename);
160
 
161
         if (null == mainResourceURL) {
162
            throw new DocTransletException("Cannot find resource '" + mainResourceFilename + "'");
163
         }
164
 
165
 
166
         Map parameters = new HashMap();
167
         parameters.put("gjdoc.xmldoclet.version", Driver.XMLDOCLET_VERSION);
168
 
169
         parameters.put("gjdoc.option.nonavbar", xsltBoolean(options.nonavbar));
170
         parameters.put("gjdoc.option.noindex", xsltBoolean(options.noindex));
171
         parameters.put("gjdoc.option.notree", xsltBoolean(options.notree));
172
         parameters.put("gjdoc.option.nocomment", xsltBoolean(options.nocomment));
173
         parameters.put("gjdoc.option.nohelp", xsltBoolean(options.nohelp));
174
         parameters.put("gjdoc.option.splitindex", xsltBoolean(options.splitindex));
175
         parameters.put("gjdoc.option.linksource", xsltBoolean(options.linksource));
176
         parameters.put("gjdoc.option.nodeprecatedlist", xsltBoolean(options.nodeprecatedlist));
177
         parameters.put("gjdoc.option.uses", xsltBoolean(options.uses));
178
         parameters.put("gjdoc.option.windowtitle", options.windowtitle);
179
         parameters.put("gjdoc.option.helpfile", options.helpfile);
180
         parameters.put("gjdoc.option.stylesheetfile", options.stylesheetfile);
181
         parameters.put("gjdoc.option.header", options.header);
182
         parameters.put("gjdoc.option.footer", options.footer);
183
         parameters.put("gjdoc.option.bottom", options.bottom);
184
         parameters.put("gjdoc.option.doctitle", options.doctitle);
185
 
186
         List outputFileList = getOutputFileList(mainResourceURL,
187
                                                 xmlSourceDirectory,
188
                                                 parameters);
189
 
190
         reporter.printNotice("Running DocTranslet...");
191
 
192
         TransformerFactory transformerFactory
193
            = TransformerFactory.newInstance();
194
 
195
         transformerFactory.setErrorListener(this);
196
 
197
         boolean isLibxmlJ
198
            = transformerFactory.getClass().getName().equals("gnu.xml.libxmlj.transform.TransformerFactoryImpl");
199
 
200
         for (Iterator it = outputFileList.iterator(); it.hasNext(); ) {
201
 
202
            if (isLibxmlJ) {
203
               System.gc();
204
               Runtime.getRuntime().runFinalization();
205
            }
206
 
207
            OutputFileInfo fileInfo = (OutputFileInfo)it.next();
208
 
209
            File targetFile = new File(targetDirectory, fileInfo.getName());
210
            File packageTargetDir = getParentFile(targetFile);
211
 
212
            if (!packageTargetDir.exists() && !packageTargetDir.mkdirs()) {
213
               throw new DocTransletException("Target directory " + packageTargetDir + " does not exist and cannot be created.");
214
            }
215
 
216
            if (options.linksource) {
217
               File sourceTargetDirectory = new File(targetDirectory, "src-html");
218
               File sourceTargetFile = new File(sourceTargetDirectory, fileInfo.getName());
219
               File sourcePackageTargetDir = getParentFile(sourceTargetFile);
220
 
221
               if (!sourcePackageTargetDir.exists() && !sourcePackageTargetDir.mkdirs()) {
222
                  throw new DocTransletException("Target directory " + packageTargetDir + " does not exist and cannot be created.");
223
               }
224
            }
225
 
226
            if (options.uses) {
227
               File usesTargetDirectory = new File(targetDirectory, "class-use");
228
               File usesTargetFile = new File(usesTargetDirectory, fileInfo.getName());
229
               File usesPackageTargetDir = getParentFile(usesTargetFile);
230
 
231
               if (!usesPackageTargetDir.exists() && !usesPackageTargetDir.mkdirs()) {
232
                  throw new DocTransletException("Target directory " + packageTargetDir + " does not exist and cannot be created.");
233
               }
234
            }
235
 
236
            if (null != fileInfo.getSource()) {
237
 
238
               reporter.printNotice("Copying " + fileInfo.getComment() + "...");
239
               InputStream in = new URL(mainResourceURL, fileInfo.getSource()).openStream();
240
               FileOutputStream out = new FileOutputStream(targetFile.getAbsolutePath());
241
               IOToolkit.copyStream(in, out);
242
               in.close();
243
               out.close();
244
            }
245
            else {
246
 
247
               reporter.printNotice("Generating " + fileInfo.getComment() + "...");
248
 
249
               String pathToRoot = "";
250
               for (File file = getParentFile(targetFile); !equalsFile(file, targetDirectory); file = getParentFile(file)) {
251
                  pathToRoot += "../";
252
               }
253
 
254
               StreamResult out = new StreamResult(targetFile.getAbsolutePath());
255
 
256
               StreamSource in = new StreamSource(new File(xmlSourceDirectory, "index.xml").getAbsolutePath());
257
               URL resource = new URL(mainResourceURL, fileInfo.getSheet());
258
 
259
 
260
               StreamSource xsltSource = new StreamSource(resource.toExternalForm());
261
 
262
               if (null != fileInfo.getInfo()) {
263
                  parameters.put("gjdoc.outputfile.info", fileInfo.getInfo());
264
               }
265
               parameters.put("gjdoc.pathtoroot", pathToRoot);
266
 
267
               Transformer transformer;
268
               transformer = (Transformer)transformerMap.get(xsltSource.getSystemId());
269
               if (null == transformer) {
270
                  transformer = transformerFactory.newTransformer(xsltSource);
271
                  if (cacheXSLTSheets) {
272
                     transformerMap.put(xsltSource.getSystemId(), transformer);
273
                  }
274
               }
275
 
276
               transformer.clearParameters();
277
               for (Iterator pit = parameters.keySet().iterator(); pit.hasNext(); ) {
278
                  String key = (String)pit.next();
279
                  String value = (String)parameters.get(key);
280
                  transformer.setParameter(key, value);
281
               }
282
 
283
               transformer.setErrorListener(this);
284
               DocErrorReporterOutputStream errorReporterOut
285
                  = new DocErrorReporterOutputStream(reporter);
286
               System.setErr(new PrintStream(errorReporterOut));
287
 
288
               transformer.transform(in, out);
289
               errorReporterOut.flush();
290
            }
291
         }
292
      }
293
      catch (MalformedURLException e) {
294
         throw new DocTransletException(e);
295
      }
296
      catch (TransformerFactoryConfigurationError e) {
297
         throw new DocTransletException(e);
298
      }
299
      catch (TransformerException e) {
300
         throw new DocTransletException(e.getMessageAndLocation(), e);
301
      }
302
      catch (IOException e) {
303
         throw new DocTransletException(e);
304
      }
305
      finally {
306
         System.setErr(err);
307
      }
308
   }
309
 
310
   private List getOutputFileList(URL resource, File xmlSourceDirectory, Map parameters)
311
      throws DocTransletException {
312
 
313
      try {
314
         List result;
315
 
316
         OutputStream out = new ByteArrayOutputStream();
317
 
318
         DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
319
         DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
320
         Document document = documentBuilder.newDocument();
321
         DOMResult domResult = new DOMResult(document);
322
         {
323
            StreamSource source = new StreamSource(resource.toExternalForm());
324
 
325
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
326
            Transformer transformer = (Transformer)transformerFactory.newTransformer(source);
327
 
328
            transformer.clearParameters();
329
            for (Iterator pit = parameters.keySet().iterator(); pit.hasNext(); ) {
330
               String key = (String)pit.next();
331
               String value = (String)parameters.get(key);
332
               transformer.setParameter(key, value);
333
            }
334
 
335
            transformer.transform(new StreamSource(new File(xmlSourceDirectory,
336
                                                            "index.xml").getAbsolutePath()),
337
                                  domResult);
338
         }
339
 
340
         {
341
            NodeList nodeList = document.getElementsByTagName("outputfile");
342
            result = new ArrayList(nodeList.getLength());
343
 
344
            for (int i=0; i<nodeList.getLength(); ++i) {
345
               Element elem = (Element)nodeList.item(i);
346
               String name    = getTextContent(elem.getElementsByTagName("name").item(0));
347
               String source
348
                  = (null != elem.getElementsByTagName("source").item(0))
349
                  ? getTextContent(elem.getElementsByTagName("source").item(0))
350
                  : null;
351
               String sheet
352
                  = (null != elem.getElementsByTagName("sheet").item(0))
353
                  ? getTextContent(elem.getElementsByTagName("sheet").item(0))
354
                  : null;
355
               String comment = getTextContent(elem.getElementsByTagName("comment").item(0));
356
               String info    = null;
357
               if (elem.getElementsByTagName("info").getLength() > 0) {
358
                  if (null != elem.getElementsByTagName("info").item(0).getFirstChild()) {
359
                     info = getTextContent(elem.getElementsByTagName("info").item(0));
360
                  }
361
                  else {
362
                     info = "";
363
                  }
364
               }
365
               result.add(new OutputFileInfo(name, source, sheet, comment, info));
366
            }
367
         }
368
         return result;
369
      }
370
      catch (TransformerFactoryConfigurationError e) {
371
         throw new DocTransletException(e);
372
      }
373
      catch (TransformerException e) {
374
         throw new DocTransletException(e.getMessageAndLocation(), e);
375
      }
376
      catch (ParserConfigurationException e) {
377
         throw new DocTransletException(e);
378
      }
379
   }
380
 
381
   private String getTextContent(Node elem)
382
   {
383
      StringBuffer result = new StringBuffer();
384
      NodeList children = elem.getChildNodes();
385
      for (int i=0; i<children.getLength(); ++i) {
386
         Node item = children.item(i);
387
         if (null != item) {
388
            String value = item.getNodeValue();
389
            if (null != value) {
390
               result.append(value);
391
            }
392
         }
393
      }
394
      return result.toString();
395
   }
396
 
397
   public void setOptions(DocTransletOptions options) {
398
      this.options = options;
399
   }
400
 
401
 
402
   public static DocTranslet fromClasspath(String resourceName)
403
      throws DocTransletConfigurationException {
404
 
405
      return new DocTranslet(resourceName,
406
                             DocTranslet.class.getClassLoader());
407
   }
408
 
409
   public static DocTranslet fromJarFile(File jarFile)
410
      throws DocTransletConfigurationException {
411
 
412
      try {
413
         JarFile inputJarFile = new JarFile(jarFile, false, JarFile.OPEN_READ);
414
 
415
         Manifest manifest = inputJarFile.getManifest();
416
 
417
         if (null == manifest) {
418
 
419
            throw new DocTransletConfigurationException("Jar file '" + jarFile + "' doesn't contain a manifest.");
420
         }
421
 
422
         Attributes mainAttributes = manifest.getMainAttributes();
423
 
424
         String docTransletMainEntry = mainAttributes.getValue("doctranslet-main-entry");
425
 
426
         if (null == docTransletMainEntry) {
427
 
428
            throw new DocTransletConfigurationException("Manifest in Jar file '" + jarFile + "' doesn't contain a doctranslet-main-entry specification.");
429
         }
430
 
431
         return new DocTranslet(docTransletMainEntry,
432
                                new JarClassLoader(inputJarFile));
433
      }
434
      catch (IOException e) {
435
         throw new DocTransletConfigurationException(e);
436
      }
437
   }
438
 
439
   private static String xsltBoolean(boolean b) {
440
      return b ? "1" : "";
441
   }
442
 
443
  public void error (TransformerException exception)
444
    throws TransformerException {
445
 
446
     throw exception;
447
  }
448
 
449
  public void fatalError (TransformerException exception)
450
    throws TransformerException {
451
 
452
     throw exception;
453
  }
454
 
455
  public void warning (TransformerException exception)
456
    throws TransformerException {
457
 
458
     System.err.println("WWW: " + exception.getMessage());
459
  }
460
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.