001: /*
002: * Copyright 2006 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
007: * BI Platform. The Initial Developer is Pentaho Corporation.
008: *
009: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
011: * the license for the specific language governing your rights and limitations.
012: */
013: package org.pentaho.plugin.jfreereport.helper;
014:
015: import java.net.URL;
016: import java.util.HashMap;
017: import java.util.Map;
018:
019: import org.jfree.resourceloader.ResourceData;
020: import org.jfree.resourceloader.ResourceKey;
021: import org.jfree.resourceloader.ResourceKeyCreationException;
022: import org.jfree.resourceloader.ResourceLoader;
023: import org.jfree.resourceloader.ResourceLoadingException;
024: import org.jfree.resourceloader.ResourceManager;
025:
026: /**
027: * This class is implemented to support loading solution files
028: * from the pentaho repository into JFreeReport
029: *
030: * @author Will Gorman
031: */
032: public class PentahoResourceLoader implements ResourceLoader {
033:
034: public static final String SOLUTION_SCHEMA_NAME = "solution"; //$NON-NLS-1$
035:
036: public static final String SCHEMA_SEPARATOR = ":/"; //$NON-NLS-1$
037:
038: public static final String PATH_SEPARATOR = "/"; //$NON-NLS-1$
039:
040: public static final String WIN_PATH_SEPARATOR = "\\"; //$NON-NLS-1$
041:
042: /** keep track of the resource manager */
043: private ResourceManager manager;
044:
045: /**
046: * default constructor
047: */
048: public PentahoResourceLoader() {
049: }
050:
051: /**
052: * set the resource manager
053: *
054: * @param manager resource manager
055: */
056: public void setResourceManager(ResourceManager manager) {
057: this .manager = manager;
058: }
059:
060: /**
061: * get the resource manager
062: *
063: * @return resource manager
064: */
065: public ResourceManager getManager() {
066: return manager;
067: }
068:
069: /**
070: * get the schema name, in this case it's always "solution"
071: *
072: * @return the schema name
073: */
074: public String getSchema() {
075: return SOLUTION_SCHEMA_NAME;
076: }
077:
078: /**
079: * create a resource data object
080: *
081: * @param key resource key
082: * @return resource data
083: * @throws ResourceLoadingException
084: */
085: public ResourceData load(ResourceKey key)
086: throws ResourceLoadingException {
087: return new PentahoResourceData(key);
088: }
089:
090: /**
091: * see if the pentaho resource loader can support the content key path
092: *
093: * @param values map of values to look in
094: * @return true if class supports the content key.
095: */
096: public boolean isSupportedKey(ResourceKey key) {
097: if (key.getSchema().equals(getSchema())) {
098: return true;
099: }
100: return false;
101: }
102:
103: /**
104: * create a new key based on the values provided
105: *
106: * @param values map of values
107: * @return new resource key
108: * @throws ResourceKeyCreationException
109: */
110: public ResourceKey createKey(Object value, Map factoryKeys)
111: throws ResourceKeyCreationException {
112: if (value instanceof String) {
113: String valueString = (String) value;
114: if (valueString.startsWith(getSchema() + SCHEMA_SEPARATOR)) {
115: String path = valueString.substring(getSchema()
116: .length()
117: + SCHEMA_SEPARATOR.length());
118: return new ResourceKey(getSchema(), path, factoryKeys);
119: }
120: }
121: return null;
122: }
123:
124: /**
125: * derive a key from an existing key, used when a relative path is given.
126: *
127: * @param parent the parent key
128: * @param data the new data to be keyed
129: * @return derived key
130: * @throws ResourceKeyCreationException
131: */
132: public ResourceKey deriveKey(ResourceKey parent, String path,
133: Map data) throws ResourceKeyCreationException {
134:
135: // update url to absolute path if currently a relative path
136: if (!path.startsWith(getSchema() + SCHEMA_SEPARATOR)) {
137: // we are looking for the current directory specified by the parent. currently
138: // the pentaho system uses the native File.separator, so we need to support it
139: // we're simply looking for the last "/" or "\" in the parent's url.
140: int winindex = ((String) parent.getIdentifier())
141: .lastIndexOf(WIN_PATH_SEPARATOR);
142: int regindex = ((String) parent.getIdentifier())
143: .lastIndexOf(PATH_SEPARATOR);
144: int dirindex = 0;
145: if (winindex > regindex) {
146: dirindex = winindex + WIN_PATH_SEPARATOR.length();
147: } else {
148: dirindex = regindex + PATH_SEPARATOR.length();
149: }
150: path = ((String) parent.getIdentifier()).substring(0,
151: dirindex)
152: + path;
153: }
154: final Map derivedValues = new HashMap(parent
155: .getFactoryParameters());
156: derivedValues.putAll(data);
157: return new ResourceKey(getSchema(), path, derivedValues);
158: }
159:
160: public URL toURL(ResourceKey key) {
161: // not supported ..
162: return null;
163: }
164:
165: }
|