01: /*
02: * Copyright 2006-2007 Pentaho Corporation. All rights reserved.
03: * This software was developed by Pentaho Corporation and is provided under the terms
04: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
05: * this file except in compliance with the license. If you need a copy of the license,
06: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt.
07: *
08: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
09: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
10: * the license for the specific language governing your rights and limitations.
11: *
12: * Additional Contributor(s): Martin Schmid gridvision engineering GmbH
13: */
14: package org.pentaho.reportdesigner.lib.common.xml;
15:
16: import org.jetbrains.annotations.NonNls;
17: import org.jetbrains.annotations.NotNull;
18: import org.jetbrains.annotations.Nullable;
19: import org.pentaho.reportdesigner.crm.report.util.FileRelativator;
20:
21: import java.lang.reflect.Field;
22: import java.net.MalformedURLException;
23: import java.net.URL;
24: import java.util.logging.Logger;
25:
26: /**
27: * User: Martin
28: * Date: 06.02.2006
29: * Time: 13:30:43
30: */
31: public class URLConverter implements ObjectConverter {
32: @NotNull
33: @NonNls
34: private static final Logger LOG = Logger
35: .getLogger(URLConverter.class.getName());
36:
37: @Nullable
38: private XMLContext xmlContext;
39:
40: @NotNull
41: public URL getObject(@NotNull
42: String s) {
43: try {
44: //noinspection ConstantConditions
45: if (s == null) {
46: throw new IllegalArgumentException("s must not be null");
47: }
48:
49: try {
50: return FileRelativator.getAbsoluteURL(xmlContext, s);
51: } catch (MalformedURLException e) {
52: throw new RuntimeException("URL can not be read", e);
53: }
54: } finally {
55: this .xmlContext = null;
56: }
57: }
58:
59: @NotNull
60: public String getString(@NotNull
61: Object obj) {
62: try {
63: return FileRelativator.getRelativePathFromURL(xmlContext,
64: obj.toString());
65: } finally {
66: this .xmlContext = null;
67: }
68: }
69:
70: public void configure(@Nullable
71: XMLContext xmlContext, @Nullable
72: Field field) {
73: this.xmlContext = xmlContext;
74: }
75: }
|