1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
package org.apache.tapestry.html; |
15 |
|
|
16 |
|
import org.apache.hivemind.ApplicationRuntimeException; |
17 |
|
import org.apache.tapestry.AbstractComponent; |
18 |
|
import org.apache.tapestry.IAsset; |
19 |
|
import org.apache.tapestry.IMarkupWriter; |
20 |
|
import org.apache.tapestry.IRequestCycle; |
21 |
|
import org.apache.tapestry.markup.MarkupWriterSource; |
22 |
|
import org.apache.tapestry.util.ContentType; |
23 |
|
|
24 |
|
import java.io.PrintWriter; |
25 |
|
import java.io.StringWriter; |
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
3 |
public abstract class Relation extends AbstractComponent |
36 |
|
{ |
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle) |
41 |
|
{ |
42 |
3 |
if (!cycle.isRewinding()) |
43 |
|
{ |
44 |
2 |
Shell shell = Shell.get(cycle); |
45 |
|
|
46 |
2 |
if (shell == null) |
47 |
1 |
throw new ApplicationRuntimeException( |
48 |
|
HTMLMessages.shellComponentRequired(), |
49 |
|
this.getLocation(), null); |
50 |
|
|
51 |
1 |
if (getUseBody() && getHref() == null) |
52 |
|
{ |
53 |
0 |
renderStyleTag(shell, writer, cycle); |
54 |
|
} |
55 |
|
else |
56 |
|
{ |
57 |
1 |
renderLinkTag(shell, writer, cycle); |
58 |
|
} |
59 |
|
} |
60 |
1 |
} |
61 |
|
|
62 |
|
protected void renderLinkTag(Shell shell, IMarkupWriter writer, IRequestCycle cycle) |
63 |
|
{ |
64 |
1 |
Object href = getHref(); |
65 |
1 |
boolean ok = (href instanceof String) || (href instanceof IAsset); |
66 |
1 |
if (!ok) |
67 |
1 |
throw new ApplicationRuntimeException(HTMLMessages.stringOrIAssetExpected(), getLocation(), null); |
68 |
|
|
69 |
|
String url; |
70 |
0 |
if (href instanceof String) |
71 |
|
{ |
72 |
0 |
url = (String) href; |
73 |
|
} |
74 |
|
else |
75 |
|
{ |
76 |
0 |
url = ((IAsset)href).buildURL(); |
77 |
|
} |
78 |
|
|
79 |
0 |
RelationBean bean = new RelationBean(); |
80 |
0 |
bean.setHref(url); |
81 |
0 |
bean.setMedia(getMedia()); |
82 |
0 |
bean.setRel(getRel()); |
83 |
0 |
bean.setRev(getRev()); |
84 |
0 |
bean.setTitle(getTitle()); |
85 |
0 |
bean.setType(getType()); |
86 |
0 |
shell.addRelation(bean); |
87 |
0 |
} |
88 |
|
|
89 |
|
protected void renderStyleTag(Shell shell, IMarkupWriter writer, IRequestCycle cycle) |
90 |
|
{ |
91 |
0 |
if (getBody() == null) |
92 |
|
{ |
93 |
0 |
return; |
94 |
|
} |
95 |
|
|
96 |
0 |
StringWriter sWriter = new StringWriter(); |
97 |
0 |
IMarkupWriter nested = getMarkupWriterSource().newMarkupWriter(new PrintWriter(sWriter), |
98 |
|
new ContentType(writer.getContentType())); |
99 |
|
|
100 |
0 |
nested.begin("style"); |
101 |
0 |
nested.attribute("type", "text/css"); |
102 |
|
|
103 |
0 |
if (getMedia()!=null) |
104 |
0 |
nested.attribute("media", getMedia()); |
105 |
0 |
if (getTitle()!=null) |
106 |
0 |
nested.attribute("title", getTitle()); |
107 |
|
|
108 |
0 |
renderBody(nested, cycle); |
109 |
0 |
nested.close(); |
110 |
|
|
111 |
0 |
shell.includeAdditionalContent(sWriter.toString()); |
112 |
0 |
} |
113 |
|
|
114 |
|
public abstract boolean getUseBody(); |
115 |
|
|
116 |
|
public abstract Object getHref(); |
117 |
|
|
118 |
|
public abstract String getRel(); |
119 |
|
|
120 |
|
public abstract String getRev(); |
121 |
|
|
122 |
|
public abstract String getType(); |
123 |
|
|
124 |
|
public abstract String getTitle(); |
125 |
|
|
126 |
|
public abstract String getMedia(); |
127 |
|
|
128 |
|
|
129 |
|
public abstract MarkupWriterSource getMarkupWriterSource(); |
130 |
|
|
131 |
|
} |