001/*
002 * (c) Copyright 2009 University of Bristol
003 * All rights reserved.
004 * [See end of file]
005 */
006package net.rootdev.javardfa;
007
008import java.util.Collections;
009import java.util.HashMap;
010import java.util.Iterator;
011import java.util.LinkedList;
012import java.util.List;
013import java.util.Map;
014import javax.xml.namespace.NamespaceContext;
015
016public final class EvalContext implements NamespaceContext {
017
018    EvalContext parent;
019    String base;
020    String parentSubject;
021    String parentObject;
022    String language;
023    String vocab;
024    List<String> forwardProperties;
025    List<String> backwardProperties;
026    Map<String, String> xmlnsMap = Collections.EMPTY_MAP;
027    Map<String, String> prefixMap = Collections.EMPTY_MAP;
028    Map<String, String> termMap = Collections.EMPTY_MAP;
029    Map<String, List<String>> listMap = Collections.EMPTY_MAP;
030
031    protected EvalContext(String base) {
032        super();
033        this.base = base;
034        this.parentSubject = base;
035        this.forwardProperties = new LinkedList<String>();
036        this.backwardProperties = new LinkedList<String>();
037    }
038
039    public EvalContext(EvalContext toCopy) {
040        super();
041        this.base = toCopy.base;
042        this.parentSubject = toCopy.parentSubject;
043        this.parentObject = toCopy.parentObject;
044        this.language = toCopy.language;
045        this.forwardProperties = new LinkedList<String>(toCopy.forwardProperties);
046        this.backwardProperties = new LinkedList<String>(toCopy.backwardProperties);
047        this.parent = toCopy;
048        this.vocab = toCopy.vocab;
049    }
050
051    public void setBase(String abase) {
052        // This is very dodgy. We want to check if ps and po have been changed
053        // from their typical values (base).
054        // Base changing happens very late in the day when we're streaming, and
055        // it is very fiddly to handle
056        boolean setPS = parentSubject == base;
057        boolean setPO = parentObject == base;
058
059        if (abase.contains("#")) {
060            this.base = abase.substring(0, abase.indexOf("#"));
061        } else {
062            this.base = abase;
063        }
064
065        if (setPS) this.parentSubject = base;
066        if (setPO) this.parentObject = base;
067        
068        if (parent != null) {
069            parent.setBase(base);
070        }
071    }
072
073    @Override
074    public String toString() {
075        return
076            String.format("[\n\tBase: %s\n\tPS: %s\n\tPO: %s\n\tlang: %s\n\tIncomplete: -> %s <- %s\n]",
077                base, parentSubject, parentObject, language,
078                forwardProperties.size(), backwardProperties.size()
079                );
080    }
081
082    /**
083     * RDFa 1.1 prefix support
084     * @param prefix Prefix
085     * @param uri URI
086     */
087    public void setPrefix(String prefix, String uri) {
088        if (uri.length() == 0) {
089            uri = base;
090        }
091        if (prefixMap == Collections.EMPTY_MAP) prefixMap = new HashMap<String, String>();
092        prefixMap.put(prefix, uri);
093    }
094    
095    /**
096     * RDFa 1.1 prefix support
097     * @param prefix Prefix
098     * @param uri URI
099     */
100    public void setPrefixes(Map<String, String> prefixes) {
101        if (prefixMap == Collections.EMPTY_MAP) prefixMap = new HashMap<String, String>();
102        prefixMap.putAll(prefixes);
103    }
104    
105    /**
106     * RDFa 1.1 prefix support.
107     * @param prefix
108     * @return
109     */
110    public String getURIForPrefix(String prefix) {
111        if (prefixMap.containsKey(prefix)) {
112            return prefixMap.get(prefix);
113        } else if (xmlnsMap.containsKey(prefix)) {
114            return xmlnsMap.get(prefix);
115        } else if (parent != null) {
116            return parent.getURIForPrefix(prefix);
117        } else {
118            return null;
119        }
120    }
121
122    // Namespace methods
123    public void setNamespaceURI(String prefix, String uri) {
124        /*if (uri.length() == 0) {
125            uri = base;
126        }
127        if (xmlnsMap == Collections.EMPTY_MAP) xmlnsMap = new HashMap<String, String>();
128        xmlnsMap.put(prefix, uri);*/
129        setPrefix(prefix, uri);
130    }
131
132    public String getNamespaceURI(String prefix) {
133        /*if (xmlnsMap.containsKey(prefix)) {
134            return xmlnsMap.get(prefix);
135        } else if (parent != null) {
136            return parent.getNamespaceURI(prefix);
137        } else {
138            return null;
139        }*/
140        return getURIForPrefix(prefix);
141    }
142    
143    // I'm not sure about this 1.1 term business. Reuse prefix map
144    public void setTerm(String term, String uri) {
145       setPrefix(term + ":", uri);
146    }
147
148    public String getURIForTerm(String term) {
149        return getURIForPrefix(term + ":");
150    }
151
152    public String getBase() {
153        return base;
154    }
155
156    public String getVocab() {
157        return vocab;
158    }
159
160    public String getPrefix(String namespaceURI) {
161        throw new UnsupportedOperationException("Not supported yet.");
162    }
163
164    public Iterator getPrefixes(String namespaceURI) {
165        throw new UnsupportedOperationException("Not supported yet.");
166    }
167    
168}
169
170/*
171 * (c) Copyright 2009 University of Bristol
172 * All rights reserved.
173 *
174 * Redistribution and use in source and binary forms, with or without
175 * modification, are permitted provided that the following conditions
176 * are met:
177 * 1. Redistributions of source code must retain the above copyright
178 *    notice, this list of conditions and the following disclaimer.
179 * 2. Redistributions in binary form must reproduce the above copyright
180 *    notice, this list of conditions and the following disclaimer in the
181 *    documentation and/or other materials provided with the distribution.
182 * 3. The name of the author may not be used to endorse or promote products
183 *    derived from this software without specific prior written permission.
184 *
185 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
186 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
187 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
188 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
189 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
190 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
191 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
192 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
193 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
194 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
195 */