1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.link;
35
36 import info.magnolia.cms.beans.config.URI2RepositoryManager;
37 import info.magnolia.cms.i18n.I18nContentSupport;
38 import info.magnolia.context.MgnlContext;
39 import info.magnolia.objectfactory.Components;
40
41 import org.apache.commons.lang3.StringUtils;
42
43
44
45
46
47
48 public class AbsolutePathTransformer implements LinkTransformer {
49
50 boolean addContextPath = true;
51
52 boolean useURI2RepositoryMapping = true;
53
54 boolean useI18N = true;
55
56 public AbsolutePathTransformer() {
57 }
58
59
60
61
62
63
64
65
66 public AbsolutePathTransformer(boolean addContextPath, boolean useURI2RepositoryMapping, boolean useI18N) {
67 this.addContextPath = addContextPath;
68 this.useURI2RepositoryMapping = useURI2RepositoryMapping;
69 this.useI18N = useI18N;
70 }
71
72
73
74
75 @Override
76 public String transform(Link uuidLink) {
77 String linkStr;
78 if (useURI2RepositoryMapping) {
79 linkStr = getURI2RepositoryManager().getURI(uuidLink);
80 } else {
81 linkStr = getURI2RepositoryManager().getDefaultMapping().getURI(uuidLink);
82 }
83 linkStr += getURISuffix(uuidLink);
84 if (useI18N) {
85 linkStr = Components.getComponent(I18nContentSupport.class).toI18NURI(linkStr);
86 }
87 linkStr = prefixLink(linkStr);
88 return linkStr;
89 }
90
91 protected URI2RepositoryManager getURI2RepositoryManager() {
92 return Components.getComponent(URI2RepositoryManager.class);
93 }
94
95 protected String prefixLink(String linkStr) {
96 if (addContextPath) {
97 return MgnlContext.getContextPath() + linkStr;
98 }
99 return linkStr;
100 }
101
102
103
104
105 public String getURISuffix(Link uuidLink) {
106 String anchor = uuidLink.getAnchor();
107 String parameters = uuidLink.getParameters();
108
109 return "" + (StringUtils.isNotEmpty(anchor) ? "#" + anchor : "") +
110 (StringUtils.isNotEmpty(parameters) ? "?" + parameters : "");
111
112 }
113
114 public boolean isAddContextPath() {
115 return this.addContextPath;
116 }
117
118
119 public void setAddContextPath(boolean addContextPath) {
120 this.addContextPath = addContextPath;
121 }
122
123
124 public boolean isUseI18N() {
125 return this.useI18N;
126 }
127
128
129 public void setUseI18N(boolean useI18N) {
130 this.useI18N = useI18N;
131 }
132
133
134 public boolean isUseURI2RepositoryMapping() {
135 return this.useURI2RepositoryMapping;
136 }
137
138
139 public void setUseURI2RepositoryMapping(boolean useURI2RepositoryMapping) {
140 this.useURI2RepositoryMapping = useURI2RepositoryMapping;
141 }
142
143 }
144