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 package com.sun.star.report.util;
24 
25 import com.sun.star.report.OutputRepository;
26 
27 import java.io.IOException;
28 import java.io.OutputStream;
29 import java.io.OutputStreamWriter;
30 
31 import java.util.HashMap;
32 import java.util.Iterator;
33 import java.util.Map;
34 
35 import org.jfree.report.JFreeReportBoot;
36 
37 import org.pentaho.reporting.libraries.xmlns.common.AttributeList;
38 import org.pentaho.reporting.libraries.xmlns.writer.DefaultTagDescription;
39 import org.pentaho.reporting.libraries.xmlns.writer.XmlWriter;
40 import org.pentaho.reporting.libraries.xmlns.writer.XmlWriterSupport;
41 
42 
43 /**
44  * Creation-Date: 24.07.2007, 18:16:52
45  *
46  * @author Thomas Morgner
47  */
48 public class ManifestWriter
49 {
50     // need this two strings other it breaks the ooo build :-(
51 
52     public static final String MANIFEST_NS = "urn:oasis:names:tc:opendocument:xmlns:manifest:1.0";
53     public static final String TAG_DEF_PREFIX = "com.sun.star.report.pentaho.output.";
54     private final Map entries;
55 
ManifestWriter()56     public ManifestWriter()
57     {
58         entries = new HashMap();
59     }
60 
addEntry(final String fullPath, final String mimeType)61     public void addEntry(final String fullPath, final String mimeType)
62     {
63         if (fullPath == null)
64         {
65             throw new NullPointerException();
66         }
67         if (mimeType == null)
68         {
69             throw new NullPointerException();
70         }
71         if ("META-INF/manifest.xml".equals(fullPath))
72         {
73             return;
74         }
75         entries.put(fullPath, mimeType);
76     }
77 
isEmpty()78     public boolean isEmpty()
79     {
80         return entries.isEmpty();
81     }
82 
write(final OutputRepository outputRepository)83     public void write(final OutputRepository outputRepository) throws IOException
84     {
85         if (isEmpty())
86         {
87             return;
88         }
89 
90         final DefaultTagDescription tagDescription = new DefaultTagDescription();
91         tagDescription.configure(JFreeReportBoot.getInstance().getGlobalConfig(),
92                 TAG_DEF_PREFIX);
93 
94         final OutputStream manifestOutputStream =
95                 outputRepository.createOutputStream("META-INF/manifest.xml", "text/xml");
96 
97         final OutputStreamWriter writer = new OutputStreamWriter(manifestOutputStream, "UTF-8");
98         final XmlWriter xmlWriter = new XmlWriter(writer, tagDescription);
99         xmlWriter.setAlwaysAddNamespace(true);
100         xmlWriter.writeXmlDeclaration("UTF-8");
101 
102         final AttributeList rootAttributes = new AttributeList();
103         rootAttributes.addNamespaceDeclaration("manifest", MANIFEST_NS);
104         xmlWriter.writeTag(MANIFEST_NS, "manifest", rootAttributes, XmlWriterSupport.OPEN);
105 
106         final Iterator iterator = entries.entrySet().iterator();
107         while (iterator.hasNext())
108         {
109             final Map.Entry entry = (Map.Entry) iterator.next();
110             final AttributeList entryAttrs = new AttributeList();
111             entryAttrs.setAttribute(MANIFEST_NS, "media-type", (String) entry.getValue());
112             entryAttrs.setAttribute(MANIFEST_NS, "full-path", (String) entry.getKey());
113             xmlWriter.writeTag(MANIFEST_NS, "file-entry", entryAttrs, XmlWriterSupport.CLOSE);
114         }
115 
116         xmlWriter.writeCloseTag();
117         xmlWriter.close();
118     }
119 }
120