1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 28 package org.openoffice.idesupport.xml; 29 30 import java.io.InputStream; 31 import java.io.OutputStream; 32 import java.io.ByteArrayInputStream; 33 import java.io.ByteArrayOutputStream; 34 import java.io.BufferedReader; 35 import java.io.InputStreamReader; 36 import java.io.IOException; 37 38 import java.util.Enumeration; 39 import java.util.ArrayList; 40 import java.util.Iterator; 41 42 import javax.xml.parsers.DocumentBuilder; 43 import javax.xml.parsers.DocumentBuilderFactory; 44 45 import org.w3c.dom.Document; 46 import org.w3c.dom.NodeList; 47 import org.w3c.dom.Element; 48 49 import com.sun.star.script.framework.container.XMLParserFactory; 50 51 public class Manifest { 52 53 private Document document = null; 54 private boolean baseElementsExist = false; 55 56 public Manifest(InputStream inputStream) throws IOException { 57 document = XMLParserFactory.getParser().parse(inputStream); 58 } 59 60 public void add(String entry) { 61 add(entry, ""); 62 } 63 64 private void add(String entry, String type) { 65 Element root, el; 66 67 ensureBaseElementsExist(); 68 69 try { 70 root = (Element) 71 document.getElementsByTagName("manifest:manifest").item(0); 72 73 el = document.createElement("manifest:file-entry"); 74 el.setAttribute("manifest:media-type", type); 75 el.setAttribute("manifest:full-path", entry); 76 // System.out.println("added: " + el.toString()); 77 root.appendChild(el); 78 } 79 catch (Exception e) { 80 System.err.println("Error adding entry: " + e.getMessage()); 81 } 82 } 83 84 private void ensureBaseElementsExist() { 85 if (baseElementsExist == false) { 86 baseElementsExist = true; 87 add("Scripts/", "application/script-parcel"); 88 } 89 } 90 91 public void remove(String entry) { 92 Element root, el; 93 int len; 94 95 try { 96 root = (Element) 97 document.getElementsByTagName("manifest:manifest").item(0); 98 99 NodeList nl = root.getElementsByTagName("manifest:file-entry"); 100 if (nl == null || (len = nl.getLength()) == 0) 101 return; 102 103 ArrayList list = new ArrayList(); 104 for (int i = 0; i < len; i++) { 105 el = (Element)nl.item(i); 106 if (el.getAttribute("manifest:full-path").startsWith(entry)) { 107 // System.out.println("found: " + el.toString()); 108 list.add(el); 109 } 110 } 111 112 Iterator iter = list.iterator(); 113 while (iter.hasNext()) 114 root.removeChild((Element)iter.next()); 115 116 // System.out.println("and after root is: " + root.toString()); 117 } 118 catch (Exception e) { 119 System.err.println("Error removing entry: " + e.getMessage()); 120 } 121 } 122 123 public InputStream getInputStream() throws IOException { 124 InputStream result = null; 125 ByteArrayOutputStream out = null; 126 127 try { 128 out = new ByteArrayOutputStream(); 129 write(out); 130 result = new ByteArrayInputStream(out.toByteArray()); 131 // result = replaceNewlines(out.toByteArray()); 132 } 133 finally { 134 if (out != null) 135 out.close(); 136 } 137 138 return result; 139 } 140 141 private InputStream replaceNewlines(byte[] bytes) throws IOException { 142 InputStream result; 143 ByteArrayOutputStream out; 144 BufferedReader reader; 145 146 reader = new BufferedReader(new InputStreamReader( 147 new ByteArrayInputStream(bytes))); 148 out = new ByteArrayOutputStream(); 149 150 int previous = reader.read(); 151 out.write(previous); 152 int current; 153 154 while ((current = reader.read()) != -1) { 155 if (((char)current == '\n' || (char)current == ' ') && 156 (char)previous == '\n') 157 continue; 158 else { 159 out.write(current); 160 previous = current; 161 } 162 } 163 result = new ByteArrayInputStream(out.toByteArray()); 164 165 return result; 166 } 167 168 public void write(OutputStream out) throws IOException { 169 XMLParserFactory.getParser().write(document, out); 170 } 171 } 172