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 | 0 | public abstract class Relation extends AbstractComponent |
36 | |
{ |
37 | |
|
38 | |
|
39 | |
|
40 | |
protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle) |
41 | |
{ |
42 | 0 | if (!cycle.isRewinding()) |
43 | |
{ |
44 | 0 | Shell shell = Shell.get(cycle); |
45 | |
|
46 | 0 | if (shell == null) |
47 | 0 | throw new ApplicationRuntimeException( |
48 | |
HTMLMessages.shellComponentRequired(), |
49 | |
this.getLocation(), null); |
50 | |
|
51 | 0 | if (getUseBody() && getHref() == null) |
52 | |
{ |
53 | 0 | renderStyleTag(shell, writer, cycle); |
54 | |
} |
55 | |
else |
56 | |
{ |
57 | 0 | renderLinkTag(shell, writer, cycle); |
58 | |
} |
59 | |
} |
60 | 0 | } |
61 | |
|
62 | |
protected void renderLinkTag(Shell shell, IMarkupWriter writer, IRequestCycle cycle) |
63 | |
{ |
64 | 0 | Object href = getHref(); |
65 | 0 | boolean ok = (href instanceof String) || (href instanceof IAsset); |
66 | 0 | if (!ok) |
67 | 0 | 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 | |
|
92 | 0 | if (getBody() == null || writer.getContentType() == null) |
93 | |
{ |
94 | 0 | return; |
95 | |
} |
96 | |
|
97 | 0 | StringWriter sWriter = new StringWriter(); |
98 | 0 | IMarkupWriter nested = getMarkupWriterSource().newMarkupWriter(new PrintWriter(sWriter), |
99 | |
new ContentType(writer.getContentType())); |
100 | |
|
101 | 0 | nested.begin("style"); |
102 | 0 | nested.attribute("type", "text/css"); |
103 | |
|
104 | 0 | if (getMedia()!=null) |
105 | 0 | nested.attribute("media", getMedia()); |
106 | 0 | if (getTitle()!=null) |
107 | 0 | nested.attribute("title", getTitle()); |
108 | |
|
109 | 0 | renderBody(nested, cycle); |
110 | 0 | nested.close(); |
111 | |
|
112 | 0 | shell.includeAdditionalContent(sWriter.toString()); |
113 | 0 | } |
114 | |
|
115 | |
public abstract boolean getUseBody(); |
116 | |
|
117 | |
public abstract Object getHref(); |
118 | |
|
119 | |
public abstract String getRel(); |
120 | |
|
121 | |
public abstract String getRev(); |
122 | |
|
123 | |
public abstract String getType(); |
124 | |
|
125 | |
public abstract String getTitle(); |
126 | |
|
127 | |
public abstract String getMedia(); |
128 | |
|
129 | |
|
130 | |
public abstract MarkupWriterSource getMarkupWriterSource(); |
131 | |
|
132 | |
} |