001: /*
002: * (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: * [See end of file]
004: */
005:
006: package com.hp.hpl.jena.rdf.arp.impl;
007:
008: import java.util.Iterator;
009:
010: import org.xml.sax.SAXParseException;
011:
012: import com.hp.hpl.jena.iri.IRI;
013: import com.hp.hpl.jena.iri.IRIComponents;
014: import com.hp.hpl.jena.iri.Violation;
015: import com.hp.hpl.jena.iri.ViolationCodes;
016: import com.hp.hpl.jena.rdf.arp.ARPErrorNumbers;
017: import com.hp.hpl.jena.rdf.arp.lang.LanguageTag;
018: import com.hp.hpl.jena.rdf.arp.lang.LanguageTagCodes;
019: import com.hp.hpl.jena.rdf.arp.lang.LanguageTagSyntaxException;
020:
021: public abstract class AbsXMLContext implements ARPErrorNumbers,
022: LanguageTagCodes {
023:
024: // protected static String truncateXMLBase(String rslt) {
025: // if (rslt == null)
026: // return null;
027: // int hash = rslt.indexOf('#');
028: // if (hash != -1) {
029: // return rslt.substring(0, hash);
030: // }
031: // return rslt;
032: // }
033:
034: protected final String lang;
035:
036: protected final Taint langTaint;
037:
038: final Taint baseTaint;
039:
040: protected final IRI uri;
041:
042: protected final AbsXMLContext document;
043:
044: protected AbsXMLContext(boolean useDoc, AbsXMLContext document,
045: IRI uri, Taint baseT, String lang, Taint langT) {
046: // this.base=base;
047: this .lang = lang;
048: langTaint = langT;
049: baseTaint = baseT;
050: this .uri = uri;
051: this .document = useDoc ? (document == null ? this : document)
052: : null;
053: }
054:
055: protected static Taint initTaint(XMLHandler h, IRI base)
056: throws SAXParseException {
057: Taint rslt = new TaintImpl();
058: checkURI(h, rslt, base);
059: return rslt;
060: }
061:
062: // protected AbsXMLContext withBase(XMLHandler forErrors, String b)
063: // throws SAXParseException {
064: // TaintImpl taintB = new TaintImpl();
065: // IRI newB = resolveAsURI(forErrors, taintB, b, false);
066: // // TO DO update MALFORMED_CONTEXT
067: // if (newB.isVeryBad())
068: // return new XMLBaselessContext(forErrors,
069: // ERR_RESOLVING_AGAINST_MALFORMED_BASE, b);
070: // return new XMLContext(keepDocument(forErrors), document, newB
071: // .create(""), taintB, lang, langTaint);
072: // }
073:
074: public AbsXMLContext withBase(XMLHandler forErrors, String b)
075: throws SAXParseException {
076: TaintImpl taintB = new TaintImpl();
077: IRI newB = resolveAsURI(forErrors, taintB, b, false);
078: if (newB.isRelative())
079: return new XMLBaselessContext(forErrors,
080: ERR_RESOLVING_AGAINST_RELATIVE_BASE, newB
081: .create(""));
082:
083: if (newB.hasViolation(false))
084: return new XMLBaselessContext(forErrors,
085: ERR_RESOLVING_AGAINST_MALFORMED_BASE, newB);
086: return new XMLContext(keepDocument(forErrors), document, newB
087: .create(""), taintB, lang, langTaint);
088: }
089:
090: abstract boolean keepDocument(XMLHandler forErrors);
091:
092: protected AbsXMLContext withLang(XMLHandler forErrors, String l)
093: throws SAXParseException {
094:
095: Taint taint = new TaintImpl();
096: checkXMLLang(forErrors, taint, l);
097: return clone(uri, baseTaint, l, taint);
098: }
099:
100: abstract AbsXMLContext clone(IRI base, Taint baseT, String l,
101: Taint langT);
102:
103: public String getLang(Taint taint) {
104: if (langTaint.isTainted())
105: taint.taint();
106: return lang;
107: }
108:
109: // protected RDFURIReference getURI(XMLHandler forErrors, Taint taintMe,
110: // String relUri) throws SAXParseException {
111: // baseUsed(forErrors, taintMe, relUri, null);
112: // if (baseTaint.isTainted())
113: // taintMe.taint();
114: // return uri;
115: // }
116: final IRI resolveAsURI(XMLHandler forErrors, Taint taintMe,
117: String relUri) throws SAXParseException {
118: return resolveAsURI(forErrors, taintMe, relUri, true);
119: }
120:
121: final IRI resolveAsURI(XMLHandler forErrors, Taint taintMe,
122: String relUri, boolean checkBaseUse)
123: throws SAXParseException {
124: IRI rslt = uri.create(relUri);
125:
126: if (checkBaseUse)
127: checkBaseUse(forErrors, taintMe, relUri, rslt);
128:
129: checkURI(forErrors, taintMe, rslt);
130:
131: return rslt;
132: }
133:
134: abstract void checkBaseUse(XMLHandler forErrors, Taint taintMe,
135: String relUri, IRI rslt) throws SAXParseException;
136:
137: // abstract void baseUsed(XMLHandler forErrors, Taint taintMe, String
138: // relUri,
139: // String string) throws SAXParseException;
140:
141: protected static void checkURI(XMLHandler forErrors, Taint taintMe,
142: IRI rslt) throws SAXParseException {
143: if (rslt.hasViolation(false)) {
144:
145: Iterator it = rslt.violations(false);
146: while (it.hasNext()) {
147: Violation irie = (Violation) it.next();
148: // if (irie.getViolationCode() ==
149: // ViolationCodes.REQUIRED_COMPONENT_MISSING)
150: String msg = irie.getShortMessage();
151: String uri = rslt.toString();
152: // if (msg.endsWith(uri)) {
153: // msg = msg.substring(0, msg.length() - uri.length()) + "<"
154: // + uri + ">";
155: // } else {
156: // msg = "<" + uri + "> " + msg;
157: // }
158: if (irie.getViolationCode() == ViolationCodes.REQUIRED_COMPONENT_MISSING
159: && irie.getComponent() == IRIComponents.SCHEME) {
160: if (!forErrors.allowRelativeURIs())
161: forErrors.warning(taintMe, WARN_RELATIVE_URI,
162: "Relative URIs are not permitted in RDF: specifically <"
163: + rslt.toString() + ">");
164:
165: } else
166: forErrors.warning(taintMe, WARN_MALFORMED_URI,
167: "Bad URI: " + msg);
168: }
169: }
170: }
171:
172: public String resolve(XMLHandler forErrors, Taint taintMe, String u)
173: throws SAXParseException {
174: return resolveAsURI(forErrors, taintMe, u, true).toString();
175: }
176:
177: private void checkXMLLang(XMLHandler arp, Taint taintMe,
178: String newLang) throws SAXParseException {
179: if (newLang.equals(""))
180: return;
181: try {
182: LanguageTag tag = new LanguageTag(newLang);
183: int tagType = tag.tagType();
184: if (tagType == LT_ILLEGAL) {
185: arp.warning(taintMe, WARN_BAD_XMLLANG, tag
186: .errorMessage());
187: }
188: if ((tagType & LT_UNDETERMINED) == LT_UNDETERMINED) {
189: arp
190: .warning(taintMe, WARN_BAD_XMLLANG,
191: "Unnecessary use of language tag \"und\" prohibited by RFC3066");
192: }
193: if ((tagType & LT_IANA_DEPRECATED) == LT_IANA_DEPRECATED) {
194: arp.warning(taintMe, WARN_DEPRECATED_XMLLANG,
195: "Use of deprecated language tag \"" + newLang
196: + "\".");
197: }
198: if ((tagType & LT_PRIVATE_USE) == LT_PRIVATE_USE) {
199: arp.warning(taintMe, IGN_PRIVATE_XMLLANG,
200: "Use of (IANA) private language tag \""
201: + newLang + "\".");
202: } else if ((tagType & LT_LOCAL_USE) == LT_LOCAL_USE) {
203: arp.warning(taintMe, IGN_PRIVATE_XMLLANG,
204: "Use of (ISO639-2) local use language tag \""
205: + newLang + "\".");
206: } else if ((tagType & LT_EXTRA) == LT_EXTRA) {
207: arp.warning(taintMe, IGN_PRIVATE_XMLLANG,
208: "Use of additional private subtags on language \""
209: + newLang + "\".");
210: }
211: } catch (LanguageTagSyntaxException e) {
212: arp
213: .warning(taintMe, WARN_MALFORMED_XMLLANG, e
214: .getMessage());
215: }
216: }
217:
218: }
219:
220: /*
221: * (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP All rights
222: * reserved.
223: *
224: * Redistribution and use in source and binary forms, with or without
225: * modification, are permitted provided that the following conditions are met:
226: * 1. Redistributions of source code must retain the above copyright notice,
227: * this list of conditions and the following disclaimer. 2. Redistributions in
228: * binary form must reproduce the above copyright notice, this list of
229: * conditions and the following disclaimer in the documentation and/or other
230: * materials provided with the distribution. 3. The name of the author may not
231: * be used to endorse or promote products derived from this software without
232: * specific prior written permission.
233: *
234: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
235: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
236: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
237: * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
238: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
239: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
240: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
241: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
242: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
243: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
244: */
|