001 /*
002 * (c) Copyright 2010 University of Bristol
003 * All rights reserved.
004 * [See end of file]
005 */
006 package net.rootdev.javardfa;
007
008 import java.io.IOException;
009 import java.util.HashMap;
010 import java.util.Map;
011 import org.xml.sax.SAXException;
012 import org.xml.sax.XMLReader;
013
014 /**
015 *
016 * @author pldms
017 */
018 public class SimpleProfileCollector implements ProfileCollector {
019
020 public void getProfile(String profileURI, EvalContext context) {
021 try {
022 XMLReader reader =
023 ParserFactory.createReaderForFormat(
024 new SimpleCollector(context),
025 ParserFactory.Format.XHTML,
026 Setting.OnePointOne);
027 reader.parse(profileURI);
028 } catch (SAXException ex) {
029 ex.printStackTrace();
030 } catch (IOException ex) {
031 ex.printStackTrace();
032 }
033 }
034
035 static class SimpleCollector implements StatementSink {
036 private final EvalContext context;
037 private final Map<String, Value> subjToVals;
038
039 SimpleCollector(EvalContext context) {
040 this.context = context;
041 this.subjToVals = new HashMap<String, Value>();
042 }
043
044 public void start() {}
045
046 public void end() {}
047
048 // Doesn't use objects
049 public void addObject(String subject, String predicate, String object) {}
050
051 public void addLiteral(String subject, String predicate, String lex, String lang, String datatype) {
052 Value val = subjToVals.get(subject);
053 if (val == null && ( predicate.equals(ProfileCollector.prefix) ||
054 predicate.equals(ProfileCollector.term) ||
055 predicate.equals(ProfileCollector.uri))) {
056 val = new Value();
057 subjToVals.put(subject, val);
058 }
059
060 if (val == null) return;
061
062 if (predicate.equals(ProfileCollector.prefix)) {
063 if (val.uri != null) context.setPrefix(lex, val.uri);
064 else val.prefix = lex;
065 } else if (predicate.equals(ProfileCollector.term)) {
066 if (val.uri != null) context.setTerm(lex, val.uri);
067 else val.term = lex;
068 } else if (predicate.equals(ProfileCollector.uri)) {
069 if (val.prefix != null) context.setPrefix(val.prefix, lex);
070 else if (val.term != null) context.setTerm(val.term, lex);
071 else val.uri = lex;
072 }
073 }
074
075 public void addPrefix(String prefix, String uri) {}
076
077 public void setBase(String base) {}
078
079 }
080
081 static final class Value {
082 String prefix;
083 String term;
084 String uri;
085 }
086 }
087
088
089 /*
090 * (c) Copyright 2010 University of Bristol
091 * All rights reserved.
092 *
093 * Redistribution and use in source and binary forms, with or without
094 * modification, are permitted provided that the following conditions
095 * are met:
096 * 1. Redistributions of source code must retain the above copyright
097 * notice, this list of conditions and the following disclaimer.
098 * 2. Redistributions in binary form must reproduce the above copyright
099 * notice, this list of conditions and the following disclaimer in the
100 * documentation and/or other materials provided with the distribution.
101 * 3. The name of the author may not be used to endorse or promote products
102 * derived from this software without specific prior written permission.
103 *
104 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
105 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
106 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
107 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
108 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
109 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
110 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
111 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
112 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
113 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
114 */