11a37d047SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 31a37d047SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 41a37d047SAndrew Rist * or more contributor license agreements. See the NOTICE file 51a37d047SAndrew Rist * distributed with this work for additional information 61a37d047SAndrew Rist * regarding copyright ownership. The ASF licenses this file 71a37d047SAndrew Rist * to you under the Apache License, Version 2.0 (the 81a37d047SAndrew Rist * "License"); you may not use this file except in compliance 91a37d047SAndrew Rist * with the License. You may obtain a copy of the License at 101a37d047SAndrew Rist * 111a37d047SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 121a37d047SAndrew Rist * 131a37d047SAndrew Rist * Unless required by applicable law or agreed to in writing, 141a37d047SAndrew Rist * software distributed under the License is distributed on an 151a37d047SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 161a37d047SAndrew Rist * KIND, either express or implied. See the License for the 171a37d047SAndrew Rist * specific language governing permissions and limitations 181a37d047SAndrew Rist * under the License. 191a37d047SAndrew Rist * 201a37d047SAndrew Rist *************************************************************/ 211a37d047SAndrew Rist 221a37d047SAndrew Rist 23cdf0e10cSrcweir package com.sun.star.report.pentaho.loader; 24cdf0e10cSrcweir 25cdf0e10cSrcweir import com.sun.star.report.InputRepository; 26cdf0e10cSrcweir 27cdf0e10cSrcweir import java.net.URL; 28cdf0e10cSrcweir 29cdf0e10cSrcweir import java.util.HashMap; 30cdf0e10cSrcweir import java.util.Map; 31cdf0e10cSrcweir 32cdf0e10cSrcweir import org.pentaho.reporting.libraries.resourceloader.ResourceData; 33cdf0e10cSrcweir import org.pentaho.reporting.libraries.resourceloader.ResourceException; 34cdf0e10cSrcweir import org.pentaho.reporting.libraries.resourceloader.ResourceKey; 35cdf0e10cSrcweir import org.pentaho.reporting.libraries.resourceloader.ResourceKeyCreationException; 36cdf0e10cSrcweir import org.pentaho.reporting.libraries.resourceloader.ResourceLoader; 37cdf0e10cSrcweir import org.pentaho.reporting.libraries.resourceloader.ResourceLoadingException; 38cdf0e10cSrcweir import org.pentaho.reporting.libraries.resourceloader.ResourceManager; 39cdf0e10cSrcweir import org.pentaho.reporting.libraries.resourceloader.loader.LoaderUtils; 40cdf0e10cSrcweir 41cdf0e10cSrcweir 42cdf0e10cSrcweir public class InputRepositoryLoader implements ResourceLoader 43cdf0e10cSrcweir { 44cdf0e10cSrcweir 45cdf0e10cSrcweir private final InputRepository inputRepository; 46cdf0e10cSrcweir private ResourceManager resourceManager; 47cdf0e10cSrcweir InputRepositoryLoader(final InputRepository inputRepository)48cdf0e10cSrcweir public InputRepositoryLoader(final InputRepository inputRepository) 49cdf0e10cSrcweir { 50cdf0e10cSrcweir if (inputRepository == null) 51cdf0e10cSrcweir { 52cdf0e10cSrcweir throw new NullPointerException(); 53cdf0e10cSrcweir } 54cdf0e10cSrcweir this.inputRepository = inputRepository; 55cdf0e10cSrcweir } 56cdf0e10cSrcweir 57cdf0e10cSrcweir /** 58cdf0e10cSrcweir * Checks, whether this resource loader implementation was responsible for 59cdf0e10cSrcweir * creating this key. 60cdf0e10cSrcweir * 61cdf0e10cSrcweir * @param key 62cdf0e10cSrcweir * @return 63cdf0e10cSrcweir */ isSupportedKey(final ResourceKey key)64cdf0e10cSrcweir public boolean isSupportedKey(final ResourceKey key) 65cdf0e10cSrcweir { 66cdf0e10cSrcweir return InputRepositoryLoader.class.getName().equals(key.getSchema()); 67cdf0e10cSrcweir } 68cdf0e10cSrcweir 69cdf0e10cSrcweir /** 70cdf0e10cSrcweir * Creates a new resource key from the given object and the factory keys. 71cdf0e10cSrcweir * 72cdf0e10cSrcweir * @param value 73cdf0e10cSrcweir * @param factoryKeys 74cdf0e10cSrcweir * @return the created key or null, if the format was not recognized. 75cdf0e10cSrcweir * @throws org.jfree.resourceloader.ResourceKeyCreationException 76cdf0e10cSrcweir * if creating the key failed. 77cdf0e10cSrcweir */ createKey(final Object value, final Map factoryKeys)78cdf0e10cSrcweir public ResourceKey createKey(final Object value, 79cdf0e10cSrcweir final Map factoryKeys) 80cdf0e10cSrcweir throws ResourceKeyCreationException 81cdf0e10cSrcweir { 82cdf0e10cSrcweir if (value instanceof String) 83cdf0e10cSrcweir { 84cdf0e10cSrcweir final String strVal = (String) value; 85cdf0e10cSrcweir if (strVal.startsWith("sun:oo://")) 86cdf0e10cSrcweir { 87cdf0e10cSrcweir return new ResourceKey(InputRepositoryLoader.class.getName(), 88cdf0e10cSrcweir new InputResourceKey(inputRepository.getId(), strVal), factoryKeys); 89cdf0e10cSrcweir } 90cdf0e10cSrcweir } 91cdf0e10cSrcweir return null; 92cdf0e10cSrcweir } 93cdf0e10cSrcweir 94cdf0e10cSrcweir /** 95cdf0e10cSrcweir * Derives a new resource key from the given key. If neither a path nor new 96cdf0e10cSrcweir * factory-keys are given, the parent key is returned. 97cdf0e10cSrcweir * 98cdf0e10cSrcweir * @param parent the parent 99cdf0e10cSrcweir * @param path the derived path (can be null). 100cdf0e10cSrcweir * @param factoryKeys the optional factory keys (can be null). 101cdf0e10cSrcweir * @return the derived key. 102cdf0e10cSrcweir * @throws org.jfree.resourceloader.ResourceKeyCreationException 103cdf0e10cSrcweir * if the key cannot be derived for any reason. 104cdf0e10cSrcweir */ deriveKey(final ResourceKey parent, final String path, final Map factoryKeys)105cdf0e10cSrcweir public ResourceKey deriveKey(final ResourceKey parent, 106cdf0e10cSrcweir final String path, 107cdf0e10cSrcweir final Map factoryKeys) 108cdf0e10cSrcweir throws ResourceKeyCreationException 109cdf0e10cSrcweir { 110cdf0e10cSrcweir if (!isSupportedKey(parent)) 111cdf0e10cSrcweir { 112*30acf5e8Spfg throw new ResourceKeyCreationException("Assertion: Unsupported parent key type"); 113cdf0e10cSrcweir } 114cdf0e10cSrcweir 115cdf0e10cSrcweir final InputResourceKey parentKey = (InputResourceKey) parent.getIdentifier(); 116cdf0e10cSrcweir final String resource; 117cdf0e10cSrcweir if (path.startsWith("sun:oo://")) 118cdf0e10cSrcweir { 119cdf0e10cSrcweir resource = path; 120cdf0e10cSrcweir } 121cdf0e10cSrcweir else if (path.charAt(0) == '/') 122cdf0e10cSrcweir { 123cdf0e10cSrcweir resource = "sun:oo:/" + path; 124cdf0e10cSrcweir } 125cdf0e10cSrcweir else 126cdf0e10cSrcweir { 127cdf0e10cSrcweir resource = LoaderUtils.mergePaths(parentKey.getPath(), path); 128cdf0e10cSrcweir } 129cdf0e10cSrcweir final Map map; 130cdf0e10cSrcweir if (factoryKeys != null) 131cdf0e10cSrcweir { 132cdf0e10cSrcweir map = new HashMap(); 133cdf0e10cSrcweir map.putAll(parent.getFactoryParameters()); 134cdf0e10cSrcweir map.putAll(factoryKeys); 135cdf0e10cSrcweir } 136cdf0e10cSrcweir else 137cdf0e10cSrcweir { 138cdf0e10cSrcweir map = parent.getFactoryParameters(); 139cdf0e10cSrcweir } 140cdf0e10cSrcweir return new ResourceKey(parent.getSchema(), 141cdf0e10cSrcweir new InputResourceKey(parentKey.getInputRepositoryId(), resource), map); 142cdf0e10cSrcweir } 143cdf0e10cSrcweir toURL(final ResourceKey key)144cdf0e10cSrcweir public URL toURL(final ResourceKey key) 145cdf0e10cSrcweir { 146cdf0e10cSrcweir return null; 147cdf0e10cSrcweir } 148cdf0e10cSrcweir load(final ResourceKey key)149cdf0e10cSrcweir public ResourceData load(final ResourceKey key) 150cdf0e10cSrcweir throws ResourceLoadingException 151cdf0e10cSrcweir { 152cdf0e10cSrcweir if (!isSupportedKey(key)) 153cdf0e10cSrcweir { 154cdf0e10cSrcweir throw new ResourceLoadingException("None of my keys."); 155cdf0e10cSrcweir } 156cdf0e10cSrcweir 157cdf0e10cSrcweir return new InputRepositoryResourceData(key, inputRepository); 158cdf0e10cSrcweir } 159cdf0e10cSrcweir setResourceManager(final ResourceManager manager)160cdf0e10cSrcweir public void setResourceManager(final ResourceManager manager) 161cdf0e10cSrcweir { 162cdf0e10cSrcweir this.resourceManager = manager; 163cdf0e10cSrcweir } 164cdf0e10cSrcweir getResourceManager()165cdf0e10cSrcweir public ResourceManager getResourceManager() 166cdf0e10cSrcweir { 167cdf0e10cSrcweir return resourceManager; 168cdf0e10cSrcweir } 169cdf0e10cSrcweir isSupportedDeserializer(String string)170cdf0e10cSrcweir public boolean isSupportedDeserializer(String string) 171cdf0e10cSrcweir { 172cdf0e10cSrcweir throw new UnsupportedOperationException("Not supported yet."); 173cdf0e10cSrcweir } 174cdf0e10cSrcweir serialize(ResourceKey rk, ResourceKey rk1)175cdf0e10cSrcweir public String serialize(ResourceKey rk, ResourceKey rk1) throws ResourceException 176cdf0e10cSrcweir { 177cdf0e10cSrcweir throw new UnsupportedOperationException("Not supported yet."); 178cdf0e10cSrcweir } 179cdf0e10cSrcweir deserialize(ResourceKey rk, String string)180cdf0e10cSrcweir public ResourceKey deserialize(ResourceKey rk, String string) throws ResourceKeyCreationException 181cdf0e10cSrcweir { 182cdf0e10cSrcweir throw new UnsupportedOperationException("Not supported yet."); 183cdf0e10cSrcweir } 184cdf0e10cSrcweir } 185