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