-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathOpenGraph.java
More file actions
408 lines (361 loc) · 13.9 KB
/
OpenGraph.java
File metadata and controls
408 lines (361 loc) · 13.9 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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
package org.opengraph;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
/**
* A Java object representation of an Open Graph enabled webpage.
* A simplified layer over a Hastable.
*
* @author Callum Jones
*/
public class OpenGraph
{
private String pageUrl;
private ArrayList<OpenGraphNamespace> pageNamespaces;
private Hashtable<String, ArrayList<MetaElement>> metaAttributes;
private String baseType;
private boolean isImported; // determine if the object is a new incarnation or representation of a web page
private boolean hasChanged; // track if object has been changed
public final static String[] REQUIRED_META = new String[]{"title", "type", "image", "url" };
public final static Hashtable<String, String[]> BASE_TYPES = new Hashtable<String, String[]>();
static
{
BASE_TYPES.put("activity", new String[] {"activity", "sport"});
BASE_TYPES.put("business", new String[] {"bar", "company", "cafe", "hotel", "restaurant"});
BASE_TYPES.put("group", new String[] {"cause", "sports_league", "sports_team"});
BASE_TYPES.put("organization", new String[] {"band", "government", "non_profit", "school", "university"});
BASE_TYPES.put("person", new String[] {"actor", "athlete", "author", "director", "musician", "politician", "profile", "public_figure"});
BASE_TYPES.put("place", new String[] {"city", "country", "landmark", "state_province"});
BASE_TYPES.put("product", new String[] {"album", "book", "drink", "food", "game", "movie", "product", "song", "tv_show"});
BASE_TYPES.put("website", new String[] {"blog", "website", "article"});
}
/**
* Create an open graph representation for generating your own Open Graph object
*/
public OpenGraph()
{
pageNamespaces = new ArrayList<OpenGraphNamespace>();
metaAttributes = new Hashtable<String, ArrayList<MetaElement>>();
hasChanged = false;
isImported = false;
}
/**
* Fetch the open graph representation from a web site
* @param url The address to the web page to fetch Open Graph data
* @param ignoreSpecErrors Set this option to true if you don't wish to have an exception throw if the page does not conform to the basic 4 attributes
* @throws java.io.IOException If a network error occurs, the HTML parser will throw an IO Exception
* @throws java.lang.Exception A generic exception is throw if the specific page fails to conform to the basic Open Graph standard as define by the constant REQUIRED_META
*/
public OpenGraph(String url, boolean ignoreSpecErrors) throws java.io.IOException, Exception {
this();
isImported = true;
// download the (X)HTML content, but only up to the closing head tag. We do not want to waste resources parsing irrelevant content
URL pageURL = new URL(url);
URLConnection siteConnection = pageURL.openConnection();
Charset charset = getConnectionCharset(siteConnection);
BufferedReader dis = new BufferedReader(new InputStreamReader(siteConnection.getInputStream(), charset));
String inputLine;
StringBuffer headContents = new StringBuffer();
// Loop through each line, looking for the closing head element
while ((inputLine = dis.readLine()) != null)
{
if (inputLine.contains("</head>"))
{
inputLine = inputLine.substring(0, inputLine.indexOf("</head>") + 7);
inputLine = inputLine.concat("<body></body></html>");
headContents.append(inputLine + "\r\n");
break;
}
headContents.append(inputLine + "\r\n");
}
String headContentsStr = headContents.toString();
// parse the string HTML
Document parsedDocument = Jsoup.parse(headContentsStr);
// read in the declared namespaces
Elements headElement = parsedDocument.getElementsByTag("head");
boolean hasOGspec = false;
if (headElement.hasAttr("prefix")) {
String namespaceData = headElement.attr("prefix");
Pattern pattern = Pattern.compile("(([A-Za-z0-9_]+):\\s+(http:\\/\\/ogp.me\\/ns(\\/\\w+)*#))\\s*");
Matcher matcher = pattern.matcher(namespaceData);
while (matcher.find()) {
String prefix = matcher.group(2);
String documentURI = matcher.group(3);
pageNamespaces.add(new OpenGraphNamespace(prefix, documentURI));
if (prefix.equals("og"))
hasOGspec = true;
}
}
// some pages do not include the new OG spec
// this fixes compatibility
if (!hasOGspec)
pageNamespaces.add(new OpenGraphNamespace("og", "http:// ogp.me/ns#"));
// open only the meta tags
Elements metaData = parsedDocument.getElementsByTag("meta");
for (Element metaElement : metaData) {
for (OpenGraphNamespace namespace : pageNamespaces) {
String target = null;
if (metaElement.hasAttr("property"))
target = "property";
else if (metaElement.hasAttr("name"))
target = "name";
if (target != null && metaElement.attr(target).startsWith(namespace.getPrefix() + ":")) {
setProperty(namespace, metaElement.attr(target), metaElement.attr("content"));
break;
}
}
}
/**
* Check that page conforms to Open Graph protocol
*/
if (!ignoreSpecErrors)
{
for (String req : REQUIRED_META)
{
if (!metaAttributes.containsKey(req))
throw new Exception("Does not conform to Open Graph protocol");
}
}
/**
* Has conformed, now determine basic sub type.
*/
baseType = null;
String currentType = getContent("type");
// some apps use their OG namespace as a prefix
if (currentType != null)
{
for (OpenGraphNamespace ns : pageNamespaces)
{
if (currentType.startsWith(ns.getPrefix() + ":"))
{
currentType = currentType.replaceFirst(ns.getPrefix() + ":","");
break; // done here
}
}
}
for (String base : BASE_TYPES.keySet())
{
String[] baseList = BASE_TYPES.get(base);
boolean finished = false;
for (String expandedType : baseList)
{
if (expandedType.equals(currentType))
{
baseType = base;
finished = true;
break;
}
}
if (finished) break;
}
// read the original page url
URL realURL = siteConnection.getURL();
pageUrl = realURL.toExternalForm();
}
/**
* Gets the charset for specified connection.
* Content Type header is parsed to get the charset name.
*
* @param connection the connection.
* @return the Charset object for response charset name;
* if it's not found then the default charset.
*/
private static Charset getConnectionCharset(URLConnection connection)
{
String contentType = connection.getContentType();
if (contentType != null && contentType.length() > 0)
{
contentType = contentType.toLowerCase();
String charsetName = extractCharsetName(contentType);
if (charsetName != null && charsetName.length() > 0)
{
try
{
return Charset.forName(charsetName);
}
catch (Exception e) {
// specified charset is not found,
// skip it to return the default one
}
}
}
// return the default charset
return Charset.defaultCharset();
}
/**
* Extract the charset name form the content type string.
* Content type string is received from Content-Type header.
*
* @param contentType the content type string, must be not null.
* @return the found charset name or null if not found.
*/
private static String extractCharsetName(String contentType)
{
// split onto media types
final String[] mediaTypes = contentType.split(":");
if (mediaTypes.length > 0)
{
// use only the first one, and split it on parameters
final String[] params = mediaTypes[0].split(";");
// find the charset parameter and return it's value
for (String each : params)
{
each = each.trim();
if (each.startsWith("charset="))
{
// return the charset name
return each.substring(8).trim();
}
}
}
return null;
}
/**
* Get the basic type of the Open graph page as per the specification
* @return Base type as defined by specification, null otherwise
*/
public String getBaseType()
{
return baseType;
}
/**
* Get a value of a given Open Graph property
* @param property The Open graph property key
* @return Returns the value of the first property defined, null otherwise
*/
public String getContent(String property)
{
if (metaAttributes.containsKey(property) && metaAttributes.get(property).size() > 0)
return metaAttributes.get(property).get(0).getContent();
else
return null;
}
/**
* Get all the defined properties of the Open Graph object
* @return An array of all currently defined properties
*/
public MetaElement[] getProperties()
{
ArrayList<MetaElement> allElements = new ArrayList<MetaElement>();
for (ArrayList<MetaElement> collection : metaAttributes.values())
allElements.addAll(collection);
return (MetaElement[]) allElements.toArray(new MetaElement[allElements.size()]);
}
/**
* Get all the defined properties of the Open Graph object
* @param property The property to focus on
* @return An array of all currently defined properties
*/
public MetaElement[] getProperties(String property)
{
if (metaAttributes.containsKey(property))
{
ArrayList target = metaAttributes.get(property);
return (MetaElement[]) target.toArray(new MetaElement[target.size()]);
}
else
return null;
}
/**
* Get the original URL the Open Graph page was obtained from
* @return The address to the Open Graph object page
*/
public String getOriginalUrl()
{
return pageUrl;
}
/**
* Get the HTML representation of the Open Graph data.
* @return An array of meta elements as Strings
*/
public String[] toHTML()
{
// allocate the array
ArrayList<String> returnHTML = new ArrayList<String>();
int index = 0; // keep track of the index to insert into
for (ArrayList<MetaElement> elements : metaAttributes.values())
{
for (MetaElement element : elements)
returnHTML.add("<meta property=\"" + element.getNamespace() + ":" +
element.getProperty() + "\" content=\"" + element.getContent() + "\" />");
}
// return the array
return (String[]) returnHTML.toArray();
}
/**
* Get the XHTML representation of the Open Graph data.
* @return An array of meta elements as Strings
*/
public String[] toXHTML()
{
// allocate the array
ArrayList<String> returnHTML = new ArrayList<String>();
int index = 0; // keep track of the index to insert into
for (ArrayList<MetaElement> elements : metaAttributes.values())
{
for (MetaElement element : elements)
returnHTML.add("<meta name=\"" + element.getNamespace().getPrefix() + ":" +
element.getProperty() + "\" content=\"" + element.getContent() + "\" />");
}
// return the array
return (String[]) returnHTML.toArray();
}
/**
* Set the Open Graph property to a specific value
* @param namespace The OpenGraph namespace the content belongs to
* @param property The og:XXXX where XXXX is the property you wish to set
* @param content The value or contents of the property to be set
*/
public void setProperty(OpenGraphNamespace namespace, String property, String content)
{
if (!pageNamespaces.contains(namespace))
pageNamespaces.add(namespace);
property = property.replaceAll(namespace.getPrefix() + ":", "");
MetaElement element = new MetaElement(namespace, property, content);
if (!metaAttributes.containsKey(property))
metaAttributes.put(property, new ArrayList<MetaElement>());
metaAttributes.get(property).add(element);
}
/**
* Removed a defined property
* @param property The og:XXXX where XXXX is the property you wish to remove
*/
public void removeProperty(String property)
{
metaAttributes.remove(property);
}
/**
* Obtain the underlying HashTable
* @return The underlying structure as a Hashtable
*/
public Hashtable<String, ArrayList<MetaElement>> exposeTable() {
return metaAttributes;
}
/**
* Test if the Open Graph object was initially a representation of a web page
* @return True if the object is from a web page, false otherwise
*/
public boolean isFromWeb()
{
return isImported;
}
/**
* Test if the object has been modified by setters/deleters.
* This is only relevant if this object initially represented a web page
* @return True True if the object has been modified, false otherwise
*/
public boolean hasChanged()
{
return hasChanged;
}
}