-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLink.java
More file actions
312 lines (253 loc) · 8.75 KB
/
Link.java
File metadata and controls
312 lines (253 loc) · 8.75 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/* Copyright (c) 2008 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.gdata.data;
import com.google.gdata.util.common.xml.XmlNamespace;
import com.google.gdata.util.common.xml.XmlWriter;
import com.google.gdata.client.CoreErrorDomain;
import com.google.gdata.util.Namespaces;
import com.google.gdata.util.ParseException;
import com.google.gdata.util.XmlParser;
import com.google.gdata.util.XmlParser.ElementHandler;
import org.xml.sax.Attributes;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* External link type.
*/
public class Link extends ExtensionPoint implements ILink {
public Link() {}
public Link(String rel, String type, String href) {
this.rel = rel;
this.type = type;
setHref(href);
}
/**
* Link relation type. Possible values include {@code self}, {@code
* prev}, {@code next}, {@code enclosure}, etc.
*/
protected String rel;
public String getRel() {
return rel != null ? rel : Rel.ALTERNATE;
}
public void setRel(String v) { rel = v; }
/** MIME type of the link target. */
protected String type;
public String getType() { return type; }
public void setType(String v) { type = v; }
/** Link URI. */
protected String href;
public String getHref() { return href; }
public void setHref(String v) { href = v; }
/** Language of resource pointed to by href. */
protected String hrefLang;
public String getHrefLang() { return hrefLang; }
public void setHrefLang(String v) { hrefLang = v; }
/** Link title. */
protected String title;
public String getTitle() { return title; }
public void setTitle(String v) { title = v; }
/** Language of link title. */
protected String titleLang;
public String getTitleLang() { return titleLang; }
public void setTitleLang(String v) { titleLang = v; }
/** Length of the resource pointed to by href, in bytes. */
protected long length = -1;
public long getLength() { return length; }
public void setLength(long v) { length = v; }
/** Nested atom:content element or {@code null} if no inlined link content. */
protected Content content = null;
public Content getContent() { return content; }
public void setContent(Content c) { this.content = c; }
/** Etag of linked resource, or {@code null} if unknown */
protected String etag = null;
public String getEtag() { return etag; }
public void setEtag(String v) { etag = v; }
/**
* Returns whether this link matches the given {@code rel} and {@code type}
* values.
*
* @param relToMatch {@code rel} value to match or {@code null} to match any
* {@code rel} value.
* @param typeToMatch {@code type} value to match or {@code null} to match any
* {@code type} value.
*/
public boolean matches(String relToMatch, String typeToMatch) {
return (relToMatch == null || relToMatch.equals(getRel()))
&& (typeToMatch == null || typeToMatch.equals(this.type));
}
@Override
public XmlParser.ElementHandler getHandler(ExtensionProfile p,
String namespace, String localName, Attributes attrs) {
return new AtomHandler(p);
}
@Override
public void generate(XmlWriter w, ExtensionProfile p) throws IOException {
generateAtom(w, p);
}
/**
* Generates XML in the Atom format.
*
* @param w
* Output writer.
*
* @param extProfile
* Extension profile.
*
* @throws IOException
*/
public void generateAtom(XmlWriter w,
ExtensionProfile extProfile) throws IOException {
ArrayList<XmlWriter.Attribute> attrs =
new ArrayList<XmlWriter.Attribute>(3);
List<XmlNamespace> nsDecls = new ArrayList<XmlNamespace>();
if (rel != null) {
attrs.add(new XmlWriter.Attribute("rel", rel));
}
if (type != null) {
attrs.add(new XmlWriter.Attribute("type", type));
}
if (href != null) {
attrs.add(new XmlWriter.Attribute("href", href));
}
if (hrefLang != null) {
attrs.add(new XmlWriter.Attribute("hreflang", hrefLang));
}
if (title != null) {
attrs.add(new XmlWriter.Attribute("title", title));
}
if (titleLang != null) {
attrs.add(new XmlWriter.Attribute("xml:lang", titleLang));
}
if (length != -1) {
attrs.add(new XmlWriter.Attribute("length", String.valueOf(length)));
}
if (etag != null) {
nsDecls.add(Namespaces.gNs);
attrs.add(new XmlWriter.Attribute(Namespaces.gAlias, "etag", etag));
}
generateStartElement(w, Namespaces.atomNs, "link", attrs, nsDecls);
if (content != null) {
content.generateAtom(w, extProfile);
}
// Invoke ExtensionPoint.
generateExtensions(w, extProfile);
w.endElement(Namespaces.atomNs, "link");
}
/**
* Generates XML in the RSS format.
*
* @param w
* Output writer.
*
* @throws IOException
*/
public void generateRss(XmlWriter w) throws IOException {
ArrayList<XmlWriter.Attribute> attrs =
new ArrayList<XmlWriter.Attribute>(3);
if (rel != null && rel.equals("enclosure")) {
if (type != null) {
attrs.add(new XmlWriter.Attribute("type", type));
}
if (href != null) {
attrs.add(new XmlWriter.Attribute("url", href));
}
if (length != -1) {
attrs.add(new XmlWriter.Attribute("length", String.valueOf(length)));
}
w.simpleElement(Namespaces.rssNs, "enclosure", attrs, null);
} else if ("comments".equals(rel)) {
w.simpleElement(Namespaces.rssNs, "comments", null, href);
} else if (Rel.ALTERNATE.equals(rel)) {
w.simpleElement(Namespaces.rssNs, "link", null, href);
} else if (Rel.VIA.equals(rel)) {
if (href != null) {
attrs.add(new XmlWriter.Attribute("url", href));
w.simpleElement(Namespaces.rssNs, "source", attrs, null);
}
}
}
/** <atom:link> parser. */
public class AtomHandler extends ExtensionPoint.ExtensionHandler {
/**
* {@code true} if the 'href' attribute is required.
*/
private final boolean linkRequired;
public AtomHandler(ExtensionProfile extProfile) {
super(extProfile, Link.class);
linkRequired = true;
}
protected AtomHandler(ExtensionProfile extProfile,
Class<? extends Link> extendedClass) {
super(extProfile, extendedClass);
linkRequired = false;
}
@Override
public void processAttribute(String namespace,
String localName,
String value)
throws ParseException {
if (namespace.equals("")) {
if (localName.equals("rel")) {
rel = value;
} else if (localName.equals("type")) {
type = value;
} else if (localName.equals("href")) {
href = getAbsoluteUri(value);
} else if (localName.equals("hreflang")) {
hrefLang = value;
} else if (localName.equals("title")) {
title = value;
} else if (localName.equals("length")) {
try {
length = Integer.valueOf(value).longValue();
} catch (NumberFormatException e) {
throw new ParseException(
CoreErrorDomain.ERR.lengthNotInteger);
}
}
} else if (namespace.equals(Namespaces.g)) {
if (localName.equals("etag")) {
etag = value;
}
}
}
@Override
public ElementHandler getChildHandler(String namespace, String localName,
Attributes attrs) throws ParseException, IOException {
if (namespace.equals(Namespaces.atom)) {
if (localName.equals("content")) {
if (content != null) {
throw new ParseException(CoreErrorDomain.ERR.duplicateContent);
}
Content.ChildHandlerInfo chi =
Content.getChildHandler(extProfile, attrs);
content = chi.content;
return chi.handler;
}
}
return super.getChildHandler(namespace, localName, attrs);
}
@Override
public void processEndElement() throws ParseException {
if (linkRequired && href == null) {
throw new ParseException(
CoreErrorDomain.ERR.missingHrefAttribute);
}
titleLang = xmlLang;
// Allow undefined content.
}
}
}