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.pentaho.loader;
24 
25 import com.sun.star.report.InputRepository;
26 
27 import java.net.URL;
28 
29 import java.util.HashMap;
30 import java.util.Map;
31 
32 import org.pentaho.reporting.libraries.resourceloader.ResourceData;
33 import org.pentaho.reporting.libraries.resourceloader.ResourceException;
34 import org.pentaho.reporting.libraries.resourceloader.ResourceKey;
35 import org.pentaho.reporting.libraries.resourceloader.ResourceKeyCreationException;
36 import org.pentaho.reporting.libraries.resourceloader.ResourceLoader;
37 import org.pentaho.reporting.libraries.resourceloader.ResourceLoadingException;
38 import org.pentaho.reporting.libraries.resourceloader.ResourceManager;
39 import org.pentaho.reporting.libraries.resourceloader.loader.LoaderUtils;
40 
41 
42 public class InputRepositoryLoader implements ResourceLoader
43 {
44 
45     private final InputRepository inputRepository;
46     private ResourceManager resourceManager;
47 
InputRepositoryLoader(final InputRepository inputRepository)48     public InputRepositoryLoader(final InputRepository inputRepository)
49     {
50         if (inputRepository == null)
51         {
52             throw new NullPointerException();
53         }
54         this.inputRepository = inputRepository;
55     }
56 
57     /**
58      * Checks, whether this resource loader implementation was responsible for
59      * creating this key.
60      *
61      * @param key
62      * @return
63      */
isSupportedKey(final ResourceKey key)64     public boolean isSupportedKey(final ResourceKey key)
65     {
66         return InputRepositoryLoader.class.getName().equals(key.getSchema());
67     }
68 
69     /**
70      * Creates a new resource key from the given object and the factory keys.
71      *
72      * @param value
73      * @param factoryKeys
74      * @return the created key or null, if the format was not recognized.
75      * @throws org.jfree.resourceloader.ResourceKeyCreationException
76      *          if creating the key failed.
77      */
createKey(final Object value, final Map factoryKeys)78     public ResourceKey createKey(final Object value,
79             final Map factoryKeys)
80             throws ResourceKeyCreationException
81     {
82         if (value instanceof String)
83         {
84             final String strVal = (String) value;
85             if (strVal.startsWith("sun:oo://"))
86             {
87                 return new ResourceKey(InputRepositoryLoader.class.getName(),
88                         new InputResourceKey(inputRepository.getId(), strVal), factoryKeys);
89             }
90         }
91         return null;
92     }
93 
94     /**
95      * Derives a new resource key from the given key. If neither a path nor new
96      * factory-keys are given, the parent key is returned.
97      *
98      * @param parent      the parent
99      * @param path        the derived path (can be null).
100      * @param factoryKeys the optional factory keys (can be null).
101      * @return the derived key.
102      * @throws org.jfree.resourceloader.ResourceKeyCreationException
103      *          if the key cannot be derived for any reason.
104      */
deriveKey(final ResourceKey parent, final String path, final Map factoryKeys)105     public ResourceKey deriveKey(final ResourceKey parent,
106             final String path,
107             final Map factoryKeys)
108             throws ResourceKeyCreationException
109     {
110         if (!isSupportedKey(parent))
111         {
112             throw new ResourceKeyCreationException("Assertation: Unsupported parent key type");
113         }
114 
115         final InputResourceKey parentKey = (InputResourceKey) parent.getIdentifier();
116         final String resource;
117         if (path.startsWith("sun:oo://"))
118         {
119             resource = path;
120         }
121         else if (path.charAt(0) == '/')
122         {
123             resource = "sun:oo:/" + path;
124         }
125         else
126         {
127             resource = LoaderUtils.mergePaths(parentKey.getPath(), path);
128         }
129         final Map map;
130         if (factoryKeys != null)
131         {
132             map = new HashMap();
133             map.putAll(parent.getFactoryParameters());
134             map.putAll(factoryKeys);
135         }
136         else
137         {
138             map = parent.getFactoryParameters();
139         }
140         return new ResourceKey(parent.getSchema(),
141                 new InputResourceKey(parentKey.getInputRepositoryId(), resource), map);
142     }
143 
toURL(final ResourceKey key)144     public URL toURL(final ResourceKey key)
145     {
146         return null;
147     }
148 
load(final ResourceKey key)149     public ResourceData load(final ResourceKey key)
150             throws ResourceLoadingException
151     {
152         if (!isSupportedKey(key))
153         {
154             throw new ResourceLoadingException("None of my keys.");
155         }
156 
157         return new InputRepositoryResourceData(key, inputRepository);
158     }
159 
setResourceManager(final ResourceManager manager)160     public void setResourceManager(final ResourceManager manager)
161     {
162         this.resourceManager = manager;
163     }
164 
getResourceManager()165     public ResourceManager getResourceManager()
166     {
167         return resourceManager;
168     }
169 
isSupportedDeserializer(String string)170     public boolean isSupportedDeserializer(String string)
171     {
172         throw new UnsupportedOperationException("Not supported yet.");
173     }
174 
serialize(ResourceKey rk, ResourceKey rk1)175     public String serialize(ResourceKey rk, ResourceKey rk1) throws ResourceException
176     {
177         throw new UnsupportedOperationException("Not supported yet.");
178     }
179 
deserialize(ResourceKey rk, String string)180     public ResourceKey deserialize(ResourceKey rk, String string) throws ResourceKeyCreationException
181     {
182         throw new UnsupportedOperationException("Not supported yet.");
183     }
184 }
185