summaryrefslogtreecommitdiff
path: root/xmerge/source/xmerge/java/org/openoffice/xmerge/util/registry/ConverterInfoReader.java
blob: 411b5b005560df70d38ccd09f5d117f4ab1fbe47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

package org.openoffice.xmerge.util.registry;

import java.io.*;
import java.util.*;
import java.util.jar.*;
import org.xml.sax.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import java.net.URL;
import java.net.JarURLConnection;

/**
 * The {@code ConverterInfoReader} pulls a {@code META-INF/converter.xml} file
 * out of a jar file and parses it, providing access to this information in a
 * {@code Vector} of {@code ConverterInfo} objects.
 */
public class ConverterInfoReader {

    private final static String TAG_CONVERTER      = "converter";
    private final static String ATTRIB_OFFICE_TYPE = "type";
    private final static String ATTRIB_VERSION     = "version";
    private final static String TAG_NAME           = "converter-display-name";
    private final static String TAG_DESC           = "converter-description";
    private final static String TAG_VENDOR         = "converter-vendor";
    private final static String TAG_CLASS_IMPL     = "converter-class-impl";
    private final static String TAG_TARGET         = "converter-target";
    private final static String ATTRIB_DEVICE_TYPE = "type";
    private final static String TAG_XSLT_DESERIAL  = "converter-xslt-deserialize";
    private final static String TAG_XSLT_SERIAL    = "converter-xslt-serialize";
    private String   jarfilename;
    private Document document;
    private ArrayList<ConverterInfo>   converterInfoList;

    /**
     * Constructor.
     *
     * <p>A jar file is passed in. The jar file is parsed and the {@code Vector}
     * of {@code ConverterInfo} objects is built.</p>
     *
     * @param   jar  The URL of the jar file to process.
     * @param   shouldvalidate Boolean to enable or disable xml validation.
     *
     * @throws  IOException                   If the jar file cannot be read or
     *                                        if the META-INF/converter.xml can
     *                                        not be read in the jar file.
     * @throws  ParserConfigurationException  If the {@code DocumentBuilder}
     *                                        can not be built.
     * @throws  org.xml.sax.SAXException      If the converter.xml file can not
     *                                        be parsed.
     * @throws  RegistryException             If the {@code ConverterFactory}
     *                                        implementation of a plug-in cannot
     *                                        be loaded.
     */
    public ConverterInfoReader(String jar,boolean shouldvalidate) throws IOException,
        ParserConfigurationException, org.xml.sax.SAXException,
        RegistryException  {

        InputStream            istream;
        InputSource            isource;
        DocumentBuilderFactory builderFactory;
        DocumentBuilder        builder;
        JarURLConnection       jarConnection;
        JarEntry               jarentry;
        JarFile                jarfile;
        URL                    url;

        converterInfoList = new ArrayList<ConverterInfo>();
        jarfilename       = jar;

        // Get Jar via URL

        url               = new URL("jar:" + jar + "!/META-INF/converter.xml");
        jarConnection     = (JarURLConnection)url.openConnection();
        jarentry          = jarConnection.getJarEntry();
        jarfile           = jarConnection.getJarFile();

        // Build the InputSource

        istream           = jarfile.getInputStream(jarentry);
        isource           = new InputSource(istream);

        // Get the DOM builder and build the document.

        builderFactory    = DocumentBuilderFactory.newInstance();

        //DTD validation

        if (shouldvalidate) {
            System.out.println("Validating xml...");
            builderFactory.setValidating(true);
        }

        builder = builderFactory.newDocumentBuilder();
        document = builder.parse(isource);

        // Parse the document.

        parseDocument();
    }

    /**
     * Loops over the <i>converter</i> {@code Node} in the converter.xml file
     * and processes them.
     *
     * @throws  RegistryException  If the plug-in associated with a specific
     *                             <i>converter</i> {@code Node} cannot be
     *                             loaded.
     */
    private void parseDocument() throws RegistryException {

        Node     converterNode;
        NodeList converterNodes = document.getElementsByTagName(TAG_CONVERTER);

        for (int i=0; i < converterNodes.getLength(); i++) {
            converterNode = converterNodes.item(i);
            if (converterNode.getNodeType() == Node.ELEMENT_NODE) {
                parseConverterNode((Element)converterNode);
            }
        }
    }

    /**
     * Parses a <i>converter</i> node, pulling the information out of the
     * {@code Node} and placing it in a {@code ConverterInfo} object, and adds
     * that object to a {@code Vector} of {@code ConverterInfo} objects.
     *
     *  @param   e  The {@code Element} corresponding to the <i>converter</i>
     *              XML tag.
     *
     *  @throws  RegistryException  If the plug-in cannot be loaded.
     */
    private void parseConverterNode(Element e) throws RegistryException {

        Element detailElement;
        Node    detailNode;
        String  elementTagName;
        String  officeMime             = null;
        ArrayList<String>  deviceMime  = new ArrayList<String>();
        String  name                   = null;
        String  desc                   = null;
        String  version                = null;
        String  vendor                 = null;
        String  classImpl              = null;
        String  xsltSerial             = null;
        String  xsltDeserial           = null;
        String  temp;

        temp = e.getAttribute(ATTRIB_OFFICE_TYPE);
        if (temp.length() != 0) {
           officeMime = temp;
        }

        temp = e.getAttribute(ATTRIB_VERSION);
        if (temp.length() != 0) {
           version = temp;
        }

        NodeList detailNodes = e.getChildNodes();
        for (int i=0; i < detailNodes.getLength(); i++) {

            detailNode = detailNodes.item(i);
            if (detailNode.getNodeType() == Node.ELEMENT_NODE) {

                detailElement  = (Element)detailNode;
                elementTagName = detailElement.getTagName();

                if (TAG_NAME.equalsIgnoreCase(elementTagName)) {
                    name = getTextValue(detailElement);
                } else if (TAG_DESC.equalsIgnoreCase(elementTagName)) {
                    desc = getTextValue(detailElement);
                } else if (TAG_VENDOR.equalsIgnoreCase(elementTagName)) {
                    vendor = getTextValue(detailElement);
                } else if (TAG_XSLT_SERIAL.equalsIgnoreCase(elementTagName)) {
                    xsltSerial = getTextValue(detailElement);
                } else if (TAG_XSLT_DESERIAL.equalsIgnoreCase(elementTagName)) {
                    xsltDeserial = getTextValue(detailElement);
                } else if (TAG_CLASS_IMPL.equalsIgnoreCase(elementTagName)) {
                    classImpl = getTextValue(detailElement);
                } else if (TAG_TARGET.equalsIgnoreCase(elementTagName)) {
                    temp = detailElement.getAttribute(ATTRIB_DEVICE_TYPE);
                    if (temp.length() != 0) {
                        deviceMime.add(temp);
                    }
                }
            }
        }

        ConverterInfo converterInfo;
        if ((xsltSerial == null) || (xsltDeserial == null)) {
            converterInfo = new ConverterInfo(jarfilename,
                    officeMime, deviceMime, name,
                    desc, version, vendor, classImpl);
        } else {
            converterInfo = new ConverterInfo(jarfilename,
                    officeMime, deviceMime, name,
                    desc, version, vendor, classImpl,
                    xsltSerial, xsltDeserial);
        }
        converterInfoList.add(converterInfo);
    }

    /**
     * Helper function to get the text value of an {@code Element}.
     *
     * @param   e  The {@code Element} to process.
     *
     * @return  The text value of the {@code Element}.
     */
    private String getTextValue(Element e) {

        NodeList tempNodes = e.getChildNodes();
        String   text      = null;
        Node     tempNode;

        for (int j=0; j < tempNodes.getLength(); j++) {
            tempNode = tempNodes.item(j);
            if (tempNode.getNodeType() == Node.TEXT_NODE) {
               text = tempNode.getNodeValue().trim();
               break;
            }
        }

        return text;
    }

    /**
     * Returns an {@code Enumeration} of {@code ConverterInfo} objects.
     *
     * @return  An {@code Enumeration} of {@code ConverterInfo} objects.
     */
    public Iterator<ConverterInfo> getConverterInfoEnumeration() {
       return (converterInfoList.iterator());
    }
}