Project

General

Profile

Download (4.44 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.IOException;
6
import java.io.InputStream;
7
import java.io.Reader;
8
import java.io.UnsupportedEncodingException;
9

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

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

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

    
48
        SinkFactory sinkFactory = new XhtmlSinkFactory();
49

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

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

    
71
        try {
72
            parser.parse( reader, sink );
73
        } catch (ParseException e) {
74
            return "Error in generating documentation : " + e.getMessage();
75
        }
76

    
77
        return baos.toString();
78
    }
79

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

    
98
        SinkFactory sinkFactory = new XhtmlSinkFactory();
99

    
100
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
101
        Sink sink;
102
        try {
103
            sink = sinkFactory.createSink(baos);
104
        } catch (IOException e) {
105
            return "Error in generating documentation : " + e.getMessage();
106
        }
107

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

    
121
        try {
122
            parser.parse( reader, sink );
123
        } catch (ParseException e) {
124
            return "Error in generating documentation : " + e.getMessage();
125
        }
126

    
127
        return baos.toString();
128
    }
129

    
130
}
(6-6/25)