xref: /trunk/main/scripting/java/org/openoffice/idesupport/xml/Manifest.java (revision 5226f0b8dea4ef117ddd2bc4948a8601d0ac403b)
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 org.openoffice.idesupport.xml;
25 
26 import java.io.InputStream;
27 import java.io.OutputStream;
28 import java.io.ByteArrayInputStream;
29 import java.io.ByteArrayOutputStream;
30 import java.io.BufferedReader;
31 import java.io.InputStreamReader;
32 import java.io.IOException;
33 
34 import java.util.ArrayList;
35 import java.util.Iterator;
36 
37 import javax.xml.parsers.DocumentBuilder;
38 import javax.xml.parsers.DocumentBuilderFactory;
39 
40 import org.w3c.dom.Document;
41 import org.w3c.dom.NodeList;
42 import org.w3c.dom.Element;
43 
44 import com.sun.star.script.framework.container.XMLParserFactory;
45 
46 public class Manifest {
47 
48     private Document document = null;
49     private boolean baseElementsExist = false;
50 
Manifest(InputStream inputStream)51     public Manifest(InputStream inputStream) throws IOException {
52         document = XMLParserFactory.getParser().parse(inputStream);
53     }
54 
add(String entry)55     public void add(String entry) {
56         add(entry, "");
57     }
58 
add(String entry, String type)59     private void add(String entry, String type) {
60         Element root, el;
61 
62         ensureBaseElementsExist();
63 
64         try {
65             root = (Element)
66                 document.getElementsByTagName("manifest:manifest").item(0);
67 
68             el = document.createElement("manifest:file-entry");
69             el.setAttribute("manifest:media-type", type);
70             el.setAttribute("manifest:full-path", entry);
71             // System.out.println("added: " + el.toString());
72             root.appendChild(el);
73         }
74         catch (Exception e) {
75             System.err.println("Error adding entry: " + e.getMessage());
76         }
77     }
78 
ensureBaseElementsExist()79     private void ensureBaseElementsExist() {
80         if (baseElementsExist == false) {
81             baseElementsExist = true;
82             add("Scripts/", "application/script-parcel");
83         }
84     }
85 
remove(String entry)86     public void remove(String entry) {
87         Element root, el;
88         int len;
89 
90         try {
91             root = (Element)
92                 document.getElementsByTagName("manifest:manifest").item(0);
93 
94             NodeList nl = root.getElementsByTagName("manifest:file-entry");
95             if (nl == null || (len = nl.getLength()) == 0)
96                 return;
97 
98             ArrayList list = new ArrayList();
99             for (int i = 0; i < len; i++) {
100                 el = (Element)nl.item(i);
101                 if (el.getAttribute("manifest:full-path").startsWith(entry)) {
102                     // System.out.println("found: " + el.toString());
103                     list.add(el);
104                 }
105             }
106 
107             Iterator iter = list.iterator();
108             while (iter.hasNext())
109                 root.removeChild((Element)iter.next());
110 
111             // System.out.println("and after root is: " + root.toString());
112         }
113         catch (Exception e) {
114             System.err.println("Error removing entry: " + e.getMessage());
115         }
116     }
117 
getInputStream()118     public InputStream getInputStream() throws IOException {
119         InputStream result = null;
120         ByteArrayOutputStream out = null;
121 
122         try {
123             out = new ByteArrayOutputStream();
124             write(out);
125             result = new ByteArrayInputStream(out.toByteArray());
126             // result = replaceNewlines(out.toByteArray());
127         }
128         finally {
129             if (out != null)
130                 out.close();
131         }
132 
133         return result;
134     }
135 
replaceNewlines(byte[] bytes)136     private InputStream replaceNewlines(byte[] bytes) throws IOException {
137         InputStream result;
138         ByteArrayOutputStream out;
139         BufferedReader reader;
140 
141         reader = new BufferedReader(new InputStreamReader(
142             new ByteArrayInputStream(bytes)));
143         out = new ByteArrayOutputStream();
144 
145         int previous = reader.read();
146         out.write(previous);
147         int current;
148 
149         while ((current = reader.read()) != -1) {
150             if (((char)current == '\n' || (char)current == ' ') &&
151                 (char)previous == '\n')
152                 continue;
153             else {
154                 out.write(current);
155                 previous = current;
156             }
157         }
158         result = new ByteArrayInputStream(out.toByteArray());
159 
160         return result;
161     }
162 
write(OutputStream out)163     public void write(OutputStream out) throws IOException {
164         XMLParserFactory.getParser().write(document, out);
165     }
166 }
167