001 /*
002 * (c) Copyright 2010 University of Bristol
003 * All rights reserved.
004 * [See end of file]
005 */
006 package net.rootdev.javardfa.uri;
007
008 import java.util.LinkedList;
009 import java.util.List;
010 import java.util.Set;
011 import javax.xml.namespace.QName;
012 import javax.xml.stream.events.Attribute;
013 import javax.xml.stream.events.StartElement;
014 import net.rootdev.javardfa.Constants;
015 import net.rootdev.javardfa.EvalContext;
016 import net.rootdev.javardfa.Resolver;
017 import net.rootdev.javardfa.Setting;
018
019 /**
020 * This uses the RDFa 1.0 URI logic
021 *
022 * @author pldms
023 */
024 public class URIExtractor10 implements URIExtractor {
025 private Set<Setting> settings;
026 private final Resolver resolver;
027
028 public URIExtractor10(Resolver resolver) {
029 this.resolver = resolver;
030 }
031
032 public void setSettings(Set<Setting> settings) {
033 this.settings = settings;
034 }
035
036 public String getURI(StartElement element, Attribute attr, EvalContext context) {
037 QName attrName = attr.getName();
038 if (attrName.equals(Constants.href) || attrName.equals(Constants.src)) // A URI
039 {
040 if (attr.getValue().length() == 0) {
041 return context.getBase();
042 } else {
043 return resolver.resolve(context.getBase(), attr.getValue());
044 }
045 }
046 if (attrName.equals(Constants.about) || attrName.equals(Constants.resource)) // Safe CURIE or URI
047 {
048 return expandSafeCURIE(element, attr.getValue(), context);
049 }
050 if (attrName.equals(Constants.datatype)) // A CURIE
051 {
052 return expandCURIE(element, attr.getValue(), context);
053 }
054 throw new RuntimeException("Unexpected attribute: " + attr);
055 }
056
057 public List<String> getURIs(StartElement element, Attribute attr, EvalContext context) {
058 List<String> uris = new LinkedList<String>();
059 String[] curies = attr.getValue().split("\\s+");
060 boolean permitReserved = Constants.rel.equals(attr.getName()) ||
061 Constants.rev.equals(attr.getName());
062 for (String curie : curies) {
063 if (Constants.SpecialRels.contains(curie.toLowerCase())) {
064 if (permitReserved)
065 uris.add("http://www.w3.org/1999/xhtml/vocab#" + curie.toLowerCase());
066 } else {
067 String uri = expandCURIE(element, curie, context);
068 if (uri != null) {
069 uris.add(uri);
070 }
071 }
072 }
073 return uris;
074 }
075
076 public String expandCURIE(StartElement element, String value, EvalContext context) {
077 if (value.startsWith("_:")) {
078 if (!settings.contains(Setting.ManualNamespaces)) return value;
079 if (element.getNamespaceURI("_") == null) return value;
080 }
081 if (settings.contains(Setting.FormMode) && // variable
082 value.startsWith("?")) {
083 return value;
084 }
085 int offset = value.indexOf(":") + 1;
086 if (offset == 0) {
087 //throw new RuntimeException("Is this a curie? \"" + value + "\"");
088 return null;
089 }
090 String prefix = value.substring(0, offset - 1);
091
092 // Apparently these are not allowed to expand
093 if ("xml".equals(prefix) || "xmlns".equals(prefix)) return null;
094
095 String namespaceURI = prefix.length() == 0 ? "http://www.w3.org/1999/xhtml/vocab#" : element.getNamespaceURI(prefix);
096 if (namespaceURI == null) {
097 return null;
098 //throw new RuntimeException("Unknown prefix: " + prefix);
099 }
100
101 return namespaceURI + value.substring(offset);
102 }
103
104 public String expandSafeCURIE(StartElement element, String value, EvalContext context) {
105 if (value.startsWith("[") && value.endsWith("]")) {
106 return expandCURIE(element, value.substring(1, value.length() - 1), context);
107 } else {
108 if (value.length() == 0) {
109 return context.getBase();
110 }
111
112 if (settings.contains(Setting.FormMode) &&
113 value.startsWith("?")) {
114 return value;
115 }
116
117 return resolver.resolve(context.getBase(), value);
118 }
119 }
120
121 public String resolveURI(String uri, EvalContext context) {
122 return resolver.resolve(context.getBase(), uri);
123 }
124
125 }
126
127 /*
128 * (c) Copyright 2010 University of Bristol
129 * All rights reserved.
130 *
131 * Redistribution and use in source and binary forms, with or without
132 * modification, are permitted provided that the following conditions
133 * are met:
134 * 1. Redistributions of source code must retain the above copyright
135 * notice, this list of conditions and the following disclaimer.
136 * 2. Redistributions in binary form must reproduce the above copyright
137 * notice, this list of conditions and the following disclaimer in the
138 * documentation and/or other materials provided with the distribution.
139 * 3. The name of the author may not be used to endorse or promote products
140 * derived from this software without specific prior written permission.
141 *
142 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
143 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
144 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
145 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
146 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
147 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
148 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
149 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
150 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
151 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
152 */