Debian Codegraph @

scripting/java/com/sun/star/script/framework/container/DeployedUnoPackagesDB.java

raw
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 com.sun.star.script.framework.container;
20
21 import java.io.ByteArrayInputStream;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.OutputStream;
25
26 import java.util.ArrayList;
27
28 import org.w3c.dom.Document;
29 import org.w3c.dom.Element;
30 import org.w3c.dom.NodeList;
31
32 public class DeployedUnoPackagesDB {
33
34 // This is the default contents of a parcel descriptor to be used when
35 // creating empty descriptors
36 private static final byte[] EMPTY_DOCUMENT =
37 ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
38 "<unopackages xmlns:unopackages=\"unopackages.dtd\">\n" +
39 "</unopackages>").getBytes();
40
41 private Document document = null;
42
43 public DeployedUnoPackagesDB() throws IOException {
44
45 ByteArrayInputStream bis = null;
46
47 try {
48 bis = new ByteArrayInputStream(EMPTY_DOCUMENT);
49 this.document = XMLParserFactory.getParser().parse(bis);
50 } finally {
51 if (bis != null)
52 bis.close();
53 }
54 }
55
56 private DeployedUnoPackagesDB(Document document) {
57 this.document = document;
58 }
59
60 public DeployedUnoPackagesDB(InputStream is) throws IOException {
61 this(XMLParserFactory.getParser().parse(is));
62 }
63
64 public String[] getDeployedPackages(String language) {
65
66 ArrayList<String> packageUrls = new ArrayList<String>(4);
67 Element main = document.getDocumentElement();
68 Element root = null;
69 int len = 0;
70 NodeList langNodes = null;
71
72 if ((langNodes = main.getElementsByTagName("language")) != null &&
73 (len = langNodes.getLength()) != 0) {
74 for (int i = 0; i < len; i++) {
75 Element e = (Element)langNodes.item(i);
76
77 if (e.getAttribute("value").equals(language)) {
78 root = e;
79 break;
80 }
81 }
82 }
83
84 if (root != null) {
85 len = 0;
86 NodeList packages = null;
87
88 if ((packages = root.getElementsByTagName("package")) != null &&
89 (len = packages.getLength()) != 0) {
90
91 for (int i = 0; i < len; i++) {
92 Element e = (Element)packages.item(i);
93 packageUrls.add(e.getAttribute("value"));
94 }
95
96 }
97 }
98
99 if (!packageUrls.isEmpty()) {
100 return packageUrls.toArray(new String[packageUrls.size()]);
101 }
102
103 return new String[0];
104 }
105
106 public void write(OutputStream out) throws IOException {
107 XMLParserFactory.getParser().write(document, out);
108 }
109
110 public Document getDocument() {
111 return document;
112 }
113
114
115 public boolean removePackage(String language, String url) {
116
117 Element main = document.getDocumentElement();
118 Element langNode = null;
119 int len = 0;
120 NodeList langNodes = null;
121
122 if ((langNodes = main.getElementsByTagName("language")) != null &&
123 (len = langNodes.getLength()) != 0) {
124 for (int i = 0; i < len; i++) {
125 Element e = (Element)langNodes.item(i);
126
127 if (e.getAttribute("value").equals(language)) {
128 langNode = e;
129 break;
130 }
131 }
132 }
133
134 if (langNode == null) {
135 return false;
136 }
137 len = 0;
138 NodeList packages = null;
139 boolean result = false;
140
141 if ((packages = langNode.getElementsByTagName("package")) != null &&
142 (len = packages.getLength()) != 0) {
143 for (int i = 0; i < len; i++) {
144
145 Element e = (Element)packages.item(i);
146 String value = e.getAttribute("value");
147
148 if (value.equals(url)) {
149 langNode.removeChild(e);
150 result = true;
151 break;
152 }
153 }
154 }
155
156 return result;
157 }
158
159 public void addPackage(String language, String url) {
160
161 Element main = document.getDocumentElement();
162 Element langNode = null;
163 Element pkgNode = null;
164
165 int len = 0;
166 NodeList langNodes = null;
167
168 if ((langNodes = document.getElementsByTagName("language")) != null &&
169 (len = langNodes.getLength()) != 0) {
170 for (int i = 0; i < len; i++) {
171 Element e = (Element)langNodes.item(i);
172
173 if (e.getAttribute("value").equals(language)) {
174 langNode = e;
175 break;
176 }
177 }
178 }
179
180 if (langNode == null) {
181 langNode = document.createElement("language");
182 langNode.setAttribute("value", language);
183 }
184
185 pkgNode = document.createElement("package");
186 pkgNode.setAttribute("value", url);
187
188 langNode.appendChild(pkgNode);
189 //add to the Top Element
190 main.appendChild(langNode);
191 }
192 }