LibreOffice Module xmerge (master)  1
ConverterInfoReader.java
Go to the documentation of this file.
1 /*
2  * This file is part of the LibreOffice project.
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  *
8  * This file incorporates work covered by the following license notice:
9  *
10  * Licensed to the Apache Software Foundation (ASF) under one or more
11  * contributor license agreements. See the NOTICE file distributed
12  * with this work for additional information regarding copyright
13  * ownership. The ASF licenses this file to you under the Apache
14  * License, Version 2.0 (the "License"); you may not use this file
15  * except in compliance with the License. You may obtain a copy of
16  * the License at http://www.apache.org/licenses/LICENSE-2.0 .
17  */
18 
19 package org.openoffice.xmerge.util.registry;
20 
21 import java.io.*;
22 import java.util.*;
23 import java.util.jar.*;
24 import org.xml.sax.*;
25 import org.w3c.dom.*;
26 import javax.xml.parsers.*;
27 import java.net.URL;
28 import java.net.JarURLConnection;
29 
35 public class ConverterInfoReader {
36 
37  private static final String TAG_CONVERTER = "converter";
38  private static final String ATTRIB_OFFICE_TYPE = "type";
39  private static final String ATTRIB_VERSION = "version";
40  private static final String TAG_NAME = "converter-display-name";
41  private static final String TAG_DESC = "converter-description";
42  private static final String TAG_VENDOR = "converter-vendor";
43  private static final String TAG_CLASS_IMPL = "converter-class-impl";
44  private static final String TAG_TARGET = "converter-target";
45  private static final String ATTRIB_DEVICE_TYPE = "type";
46  private static final String TAG_XSLT_DESERIAL = "converter-xslt-deserialize";
47  private static final String TAG_XSLT_SERIAL = "converter-xslt-serialize";
48  private final String jarfilename;
49  private final Document document;
50  private final ArrayList<ConverterInfo> converterInfoList;
51 
72  public ConverterInfoReader(String jar,boolean shouldvalidate) throws IOException,
73  ParserConfigurationException, org.xml.sax.SAXException,
75 
76  converterInfoList = new ArrayList<ConverterInfo>();
77  jarfilename = jar;
78 
79  // Get Jar via URL
80  URL url = new URL("jar:" + jar + "!/META-INF/converter.xml");
81  JarURLConnection jarConnection = (JarURLConnection)url.openConnection();
82  JarEntry jarentry = jarConnection.getJarEntry();
83  JarFile jarfile = jarConnection.getJarFile();
84 
85  if (jarfile == null || jarentry == null) {
86  throw new IOException("Missing jar entry");
87  }
88 
89  // Build the InputSource
90  InputStream istream = jarfile.getInputStream(jarentry);
91  InputSource isource = new InputSource(istream);
92 
93  // Get the DOM builder and build the document.
94 
95  DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
96 
97  //DTD validation
98 
99  if (shouldvalidate) {
100  System.out.println("Validating xml...");
101  builderFactory.setValidating(true);
102  }
103 
104  DocumentBuilder builder = builderFactory.newDocumentBuilder();
105  document = builder.parse(isource);
106 
107  // Parse the document.
108 
109  parseDocument();
110  }
111 
120  private void parseDocument() throws RegistryException {
121 
122  Node converterNode;
123  NodeList converterNodes = document.getElementsByTagName(TAG_CONVERTER);
124 
125  for (int i=0; i < converterNodes.getLength(); i++) {
126  converterNode = converterNodes.item(i);
127  if (converterNode.getNodeType() == Node.ELEMENT_NODE) {
128  parseConverterNode((Element)converterNode);
129  }
130  }
131  }
132 
143  private void parseConverterNode(Element e) throws RegistryException {
144 
145  Element detailElement;
146  Node detailNode;
147  String elementTagName;
148  String officeMime = null;
149  ArrayList<String> deviceMime = new ArrayList<String>();
150  String name = null;
151  String desc = null;
152  String version = null;
153  String vendor = null;
154  String classImpl = null;
155  String xsltSerial = null;
156  String xsltDeserial = null;
157  String temp;
158 
159  temp = e.getAttribute(ATTRIB_OFFICE_TYPE);
160  if (temp.length() != 0) {
161  officeMime = temp;
162  }
163 
164  temp = e.getAttribute(ATTRIB_VERSION);
165  if (temp.length() != 0) {
166  version = temp;
167  }
168 
169  NodeList detailNodes = e.getChildNodes();
170  for (int i=0; i < detailNodes.getLength(); i++) {
171 
172  detailNode = detailNodes.item(i);
173  if (detailNode.getNodeType() == Node.ELEMENT_NODE) {
174 
175  detailElement = (Element)detailNode;
176  elementTagName = detailElement.getTagName();
177 
178  if (TAG_NAME.equalsIgnoreCase(elementTagName)) {
179  name = getTextValue(detailElement);
180  } else if (TAG_DESC.equalsIgnoreCase(elementTagName)) {
181  desc = getTextValue(detailElement);
182  } else if (TAG_VENDOR.equalsIgnoreCase(elementTagName)) {
183  vendor = getTextValue(detailElement);
184  } else if (TAG_XSLT_SERIAL.equalsIgnoreCase(elementTagName)) {
185  xsltSerial = getTextValue(detailElement);
186  } else if (TAG_XSLT_DESERIAL.equalsIgnoreCase(elementTagName)) {
187  xsltDeserial = getTextValue(detailElement);
188  } else if (TAG_CLASS_IMPL.equalsIgnoreCase(elementTagName)) {
189  classImpl = getTextValue(detailElement);
190  } else if (TAG_TARGET.equalsIgnoreCase(elementTagName)) {
191  temp = detailElement.getAttribute(ATTRIB_DEVICE_TYPE);
192  if (temp.length() != 0) {
193  deviceMime.add(temp);
194  }
195  }
196  }
197  }
198 
199  ConverterInfo converterInfo;
200  if ((xsltSerial == null) || (xsltDeserial == null)) {
201  converterInfo = new ConverterInfo(jarfilename,
202  officeMime, deviceMime, name,
203  desc, version, vendor, classImpl);
204  } else {
205  converterInfo = new ConverterInfo(jarfilename,
206  officeMime, deviceMime, name,
207  desc, version, vendor, classImpl,
208  xsltSerial, xsltDeserial);
209  }
210  converterInfoList.add(converterInfo);
211  }
212 
220  private String getTextValue(Element e) {
221 
222  NodeList tempNodes = e.getChildNodes();
223  String text = null;
224  Node tempNode;
225 
226  for (int j=0; j < tempNodes.getLength(); j++) {
227  tempNode = tempNodes.item(j);
228  if (tempNode.getNodeType() == Node.TEXT_NODE) {
229  text = tempNode.getNodeValue().trim();
230  break;
231  }
232  }
233 
234  return text;
235  }
236 
242  public Iterator<ConverterInfo> getConverterInfoEnumeration() {
243  return converterInfoList.iterator();
244  }
245 }
void parseConverterNode(Element e)
Parses a converter node, pulling the information out of the.
exports com.sun.star. java
exports com.sun.star. text
ConverterInfoReader(String jar, boolean shouldvalidate)
Constructor.
int i
String getTextValue(Element e)
Helper function to get the text value of an.
const char * name
Class for storing the information about a converter plug-in.
Iterator< ConverterInfo > getConverterInfoEnumeration()
Returns an.