1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 package com.sun.star.script.framework.container; 25 26 import java.io.File; 27 import java.io.InputStream; 28 import java.io.OutputStream; 29 import java.io.IOException; 30 import java.io.ByteArrayInputStream; 31 32 import java.util.ArrayList; 33 34 // import javax.xml.parsers.DocumentBuilderFactory; 35 // import javax.xml.parsers.DocumentBuilder; 36 // import javax.xml.parsers.ParserConfigurationException; 37 38 import org.w3c.dom.*; 39 40 public class DeployedUnoPackagesDB { 41 42 // File name to be used for parcel descriptor files 43 private static final String 44 PARCEL_DESCRIPTOR_NAME = "unopkg-desc.xml"; 45 46 // This is the default contents of a parcel descriptor to be used when 47 // creating empty descriptors 48 private static final byte[] EMPTY_DOCUMENT = 49 ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + 50 "<unopackages xmlns:unopackages=\"unopackages.dtd\">\n" + 51 "</unopackages>").getBytes(); 52 53 private File file = null; 54 private Document document = null; 55 DeployedUnoPackagesDB()56 public DeployedUnoPackagesDB() throws IOException { 57 ByteArrayInputStream bis = null; 58 try { 59 bis = new ByteArrayInputStream(EMPTY_DOCUMENT); 60 this.document = XMLParserFactory.getParser().parse(bis); 61 } 62 finally { 63 if (bis != null) 64 bis.close(); 65 } 66 } 67 DeployedUnoPackagesDB(Document document)68 public DeployedUnoPackagesDB(Document document) { 69 this.document = document; 70 } 71 DeployedUnoPackagesDB(InputStream is)72 public DeployedUnoPackagesDB(InputStream is) throws IOException { 73 this(XMLParserFactory.getParser().parse(is)); 74 } 75 getDeployedPackages( String language )76 public String[] getDeployedPackages( String language ) 77 { 78 ArrayList packageUrls = new ArrayList(4); 79 Element main = document.getDocumentElement(); 80 Element root = null; 81 Element item; 82 int len = 0; 83 NodeList langNodes = null; 84 85 if ((langNodes = main.getElementsByTagName("language")) != null && 86 (len = langNodes.getLength()) != 0) 87 { 88 for ( int i=0; i<len; i++ ) 89 { 90 Element e = (Element)langNodes.item( i ); 91 if ( e.getAttribute("value").equals(language) ) 92 { 93 root = e; 94 break; 95 } 96 } 97 } 98 if ( root != null ) 99 { 100 len = 0; 101 NodeList packages = null; 102 if ((packages = root.getElementsByTagName("package")) != null && 103 (len = packages.getLength()) != 0) 104 { 105 106 for ( int i=0; i<len; i++ ) 107 { 108 109 Element e = (Element)packages.item( i ); 110 packageUrls.add( e.getAttribute("value") ); 111 } 112 } 113 } 114 if ( !packageUrls.isEmpty() ) 115 { 116 return (String[])packageUrls.toArray( new String[0] ); 117 } 118 return new String[0]; 119 } 120 write(OutputStream out)121 public void write(OutputStream out) throws IOException { 122 XMLParserFactory.getParser().write(document, out); 123 } 124 getDocument()125 public Document getDocument() { 126 return document; 127 } 128 129 clearEntries()130 private void clearEntries() { 131 NodeList langNodes; 132 Element main = document.getDocumentElement(); 133 int len; 134 135 if ((langNodes = document.getElementsByTagName("language")) != null && 136 (len = langNodes.getLength()) != 0) 137 { 138 for (int i = len - 1; i >= 0; i--) { 139 try { 140 main.removeChild(langNodes.item(i)); 141 } 142 catch (DOMException de) { 143 // ignore 144 } 145 } 146 } 147 } 148 removePackage( String language, String url )149 public boolean removePackage( String language, String url ) 150 { 151 Element main = document.getDocumentElement(); 152 Element langNode = null; 153 int len = 0; 154 NodeList langNodes = null; 155 boolean result = false; 156 if ((langNodes = main.getElementsByTagName("language")) != null && 157 (len = langNodes.getLength()) != 0) 158 { 159 for ( int i=0; i<len; i++ ) 160 { 161 Element e = (Element)langNodes.item( i ); 162 if ( e.getAttribute("value").equals(language) ) 163 { 164 langNode = e; 165 break; 166 } 167 } 168 } 169 if ( langNode != null ) 170 { 171 len = 0; 172 NodeList packages = null; 173 if ((packages = langNode.getElementsByTagName("package")) != null && 174 (len = packages.getLength()) != 0) 175 { 176 for ( int i=0; i<len; i++ ) 177 { 178 179 Element e = (Element)packages.item( i ); 180 String value = e.getAttribute("value"); 181 182 if ( value.equals(url) ) 183 { 184 langNode.removeChild( e ); 185 result = true; 186 break; 187 } 188 } 189 } 190 } 191 return result; 192 } 193 addPackage(String language, String url )194 public void addPackage(String language, String url ) { 195 Element main = document.getDocumentElement(); 196 Element langNode = null; 197 Element pkgNode = null; 198 199 int len = 0; 200 NodeList langNodes = null; 201 202 if ((langNodes = document.getElementsByTagName("language")) != null && 203 (len = langNodes.getLength()) != 0) 204 { 205 for ( int i=0; i<len; i++ ) 206 { 207 Element e = (Element)langNodes.item( i ); 208 if ( e.getAttribute("value").equals(language) ) 209 { 210 langNode = e; 211 break; 212 } 213 } 214 } 215 if ( langNode == null ) 216 { 217 langNode = document.createElement("language"); 218 langNode.setAttribute( "value", language ); 219 } 220 pkgNode = document.createElement("package"); 221 pkgNode.setAttribute( "value", url ); 222 223 langNode.appendChild(pkgNode); 224 //add to the Top Element 225 main.appendChild(langNode); 226 } 227 } 228