01: package org.pentaho.plugin.jfreereport.helper;
02:
03: import java.util.ArrayList;
04: import java.text.MessageFormat;
05:
06: import org.jfree.report.modules.output.table.html.URLRewriteException;
07: import org.jfree.report.modules.output.table.html.URLRewriter;
08: import org.jfree.repository.ContentEntity;
09: import org.jfree.repository.ContentIOException;
10: import org.jfree.repository.ContentLocation;
11:
12: /**
13: * Creation-Date: 05.07.2007, 19:16:13
14: *
15: * @author Thomas Morgner
16: */
17: public class PentahoURLRewriter implements URLRewriter {
18: private String pattern;
19:
20: public PentahoURLRewriter(final String pattern) {
21: this .pattern = pattern;
22: }
23:
24: public String rewrite(final ContentEntity contentEntry,
25: final ContentEntity dataEntity) throws URLRewriteException {
26: try {
27: final ArrayList entityNames = new ArrayList();
28: entityNames.add(dataEntity.getName());
29:
30: ContentLocation location = dataEntity.getParent();
31: while (location != null) {
32: entityNames.add(location.getName());
33: location = location.getParent();
34: }
35:
36: final ArrayList contentNames = new ArrayList();
37: location = dataEntity.getRepository().getRoot();
38:
39: while (location != null) {
40: contentNames.add(location.getName());
41: location = location.getParent();
42: }
43:
44: // now remove all path elements that are equal ..
45: while (contentNames.isEmpty() == false
46: && entityNames.isEmpty() == false) {
47: final String lastEntity = (String) entityNames
48: .get(entityNames.size() - 1);
49: final String lastContent = (String) contentNames
50: .get(contentNames.size() - 1);
51: if (lastContent.equals(lastEntity) == false) {
52: break;
53: }
54: entityNames.remove(entityNames.size() - 1);
55: contentNames.remove(contentNames.size() - 1);
56: }
57:
58: final StringBuffer b = new StringBuffer();
59: for (int i = entityNames.size() - 1; i >= 0; i--) {
60: final String name = (String) entityNames.get(i);
61: b.append(name);
62: if (i != 0) {
63: b.append("/");//$NON-NLS-1$
64: }
65: }
66:
67: if (pattern == null) {
68: return b.toString();
69: }
70:
71: return MessageFormat.format(pattern, new String[] { b
72: .toString() });
73: } catch (ContentIOException cioe) {
74: throw new URLRewriteException();
75: }
76:
77: }
78: }
|