Project

General

Profile

Download (4.62 KB) Statistics
| Branch: | Tag: | Revision:
1
package eu.etaxonomy.cdm.common;
2

    
3
import java.io.ByteArrayOutputStream;
4
import java.io.File;
5
import java.io.FileNotFoundException;
6
import java.io.IOException;
7
import java.io.InputStream;
8
import java.io.Reader;
9
import java.io.UnsupportedEncodingException;
10

    
11
import org.apache.maven.doxia.module.apt.AptParser;
12
import org.apache.maven.doxia.module.xhtml.XhtmlSinkFactory;
13
import org.apache.maven.doxia.parser.ParseException;
14
import org.apache.maven.doxia.parser.Parser;
15
import org.apache.maven.doxia.sink.Sink;
16
import org.apache.maven.doxia.sink.SinkFactory;
17
import org.codehaus.plexus.DefaultPlexusContainer;
18
import org.codehaus.plexus.PlexusContainer;
19
import org.codehaus.plexus.PlexusContainerException;
20
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
21
import org.codehaus.plexus.util.ReaderFactory;
22

    
23
/**
24
 * The utility class which provides methods relating to documentation.
25
 *
26
 * @author c.mathew
27
 * @since 01-Aug-2012
28
 */
29

    
30
public class DocUtils {
31

    
32
    /**
33
     * Converts an apt file into html.
34
     *
35
     * @param aptFile apt file
36
     *
37
     * @return html as string or error message if exception
38
     *
39
     */
40
    public static String convertAptToHtml(File aptFile) {
41
        PlexusContainer container;
42
        try {
43
            container = new DefaultPlexusContainer();
44
        } catch (PlexusContainerException e) {
45
            return "Error in generating documentation : " + e.getMessage();
46
        }
47
        //FIXME : Plexus does not seem to work for looking up Sink Factory, so XhtmlSinkFactory is called directory
48
        //SinkFactory sinkFactory = (SinkFactory) container.lookup( SinkFactory.ROLE, "html" ); // Plexus lookup
49

    
50
        SinkFactory sinkFactory = new XhtmlSinkFactory();
51

    
52
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
53
        Sink sink;
54
        try {
55
            sink = sinkFactory.createSink(baos);
56
        } catch (IOException e) {
57
            return "Error in generating documentation : " + e.getMessage();
58
        }
59

    
60
        AptParser parser;
61
        try {
62
            parser = (AptParser)container.lookup(Parser.ROLE, "apt");
63
        } catch (ComponentLookupException e) {
64
            return "Error in generating documentation : " + e.getMessage();
65
        }
66
        Reader reader;
67
        try {
68
            reader = ReaderFactory.newReader( aptFile, "UTF-8" );
69
        } catch (FileNotFoundException e) {
70
            return "Error in generating documentation : " + e.getMessage();
71
        } catch (UnsupportedEncodingException e) {
72
            return "Error in generating documentation : " + e.getMessage();
73
        }
74

    
75
        try {
76
            parser.parse( reader, sink );
77
        } catch (ParseException e) {
78
            return "Error in generating documentation : " + e.getMessage();
79
        }
80

    
81
        return baos.toString();
82
    }
83

    
84
    /**
85
     * Converts an apt input stream into html.
86
     *
87
     * @param aptInputStream apt input stream
88
     *
89
     * @return html as string or error message if exception
90
     *
91
     */
92
    public static String convertAptToHtml(InputStream aptInputStream) {
93
        PlexusContainer container;
94
        try {
95
            container = new DefaultPlexusContainer();
96
        } catch (PlexusContainerException e) {
97
            return "Error in generating documentation : " + e.getMessage();
98
        }
99
        //FIXME : Plexus does not seem to work for looking up Sink Factory, so XhtmlSinkFactory is called directory
100
        //SinkFactory sinkFactory = (SinkFactory) container.lookup( SinkFactory.ROLE, "html" ); // Plexus lookup
101

    
102
        SinkFactory sinkFactory = new XhtmlSinkFactory();
103

    
104
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
105
        Sink sink;
106
        try {
107
            sink = sinkFactory.createSink(baos);
108
        } catch (IOException e) {
109
            return "Error in generating documentation : " + e.getMessage();
110
        }
111

    
112
        AptParser parser;
113
        try {
114
            parser = (AptParser)container.lookup(Parser.ROLE, "apt");
115
        } catch (ComponentLookupException e) {
116
            return "Error in generating documentation : " + e.getMessage();
117
        }
118
        Reader reader;
119
        try {
120
            reader = ReaderFactory.newReader( aptInputStream, "UTF-8" );
121
        } catch (UnsupportedEncodingException e) {
122
            return "Error in generating documentation : " + e.getMessage();
123
        }
124

    
125
        try {
126
            parser.parse( reader, sink );
127
        } catch (ParseException e) {
128
            return "Error in generating documentation : " + e.getMessage();
129
        }
130

    
131
        return baos.toString();
132
    }
133

    
134
}
(6-6/21)