001/*
002 * (c) Copyright 2010 University of Bristol
003 * All rights reserved.
004 * [See end of file]
005 */
006
007package net.rootdev.javardfa.output;
008
009import java.io.IOException;
010import java.util.HashMap;
011import java.util.Map;
012import net.rootdev.javardfa.ParserFactory;
013import net.rootdev.javardfa.ParserFactory.Format;
014import net.rootdev.javardfa.StatementSink;
015import org.xml.sax.InputSource;
016import org.xml.sax.SAXException;
017import org.xml.sax.XMLReader;
018
019/**
020 *
021 * @author pldms
022 */
023public class OGPReader implements StatementSink {
024
025    static final String NS = "http://opengraphprotocol.org/schema/";
026    static final int NSlen = NS.length();
027    private String base;
028    private final Map<String, String> content = new HashMap<String, String>();
029
030    public void start() {}
031
032    public void end() {}
033
034    public void addObject(String subject, String predicate, String object) {
035        collect(subject, predicate, object);
036    }
037
038    public void addLiteral(String subject, String predicate, String lex, String lang, String datatype) {
039        collect(subject, predicate, lex);
040    }
041
042    public void addPrefix(String prefix, String uri) {}
043
044    public void setBase(String base) {
045        this.base = base;
046    }
047
048    private void collect(String subject, String predicate, String value) {
049        if (!subject.equals(base)) return;
050        if (predicate.startsWith(NS)) content.put(predicate.substring(NSlen), value);
051        else content.put(predicate, value);
052    }
053
054    public Map<String, String> getContent() { return content; }
055
056    /**
057     * A rudimentary Open Graph Protocol parser
058     * @param url Source to parse
059     * @param format HTML or XHTML
060     * @return Map from key to value. For OGP properties the key is simple (e.g. email)
061     * @throws SAXException
062     * @throws IOException
063     */
064    public static Map<String, String> getOGP(String url, Format format) throws SAXException, IOException {
065        OGPReader reader = new OGPReader();
066        XMLReader parser = ParserFactory.createReaderForFormat(reader, format);
067        parser.parse(new InputSource(url));
068        return reader.getContent();
069    }
070
071}
072
073/*
074 * (c) Copyright 2010 University of Bristol
075 * All rights reserved.
076 *
077 * Redistribution and use in source and binary forms, with or without
078 * modification, are permitted provided that the following conditions
079 * are met:
080 * 1. Redistributions of source code must retain the above copyright
081 *    notice, this list of conditions and the following disclaimer.
082 * 2. Redistributions in binary form must reproduce the above copyright
083 *    notice, this list of conditions and the following disclaimer in the
084 *    documentation and/or other materials provided with the distribution.
085 * 3. The name of the author may not be used to endorse or promote products
086 *    derived from this software without specific prior written permission.
087 *
088 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
089 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
090 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
091 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
092 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
093 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
094 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
095 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
096 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
097 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
098 */