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.HashSet;
009: import java.util.Set;
010:
011: import org.xml.sax.SAXParseException;
012:
013: import com.hp.hpl.jena.rdf.arp.ARPErrorNumbers;
014: import com.hp.hpl.jena.rdf.arp.states.Frame;
015:
016: abstract public class QNameLexer implements Names, ARPErrorNumbers {
017: final int bad;
018: final int select;
019: final Frame frame;
020:
021: public QNameLexer(Frame f, int good, int bad) {
022: bad &= ~good;
023: this .bad = bad;
024: this .select = good | bad;
025: this .frame = f;
026: }
027:
028: private int xml(String wanted, int fl) {
029: return (fl & select) == fl && wanted.equals(getLocalName())
030: && getUri().equals(xmlns) ? fl : 0;
031: }
032:
033: abstract boolean isInRdfns(Taint taintMe) throws SAXParseException;
034:
035: abstract void error(Taint taintMe, int rslt)
036: throws SAXParseException;
037:
038: abstract void deprecatedAttribute(Taint me, int rslt)
039: throws SAXParseException;
040:
041: abstract String getLocalName();
042:
043: abstract String getUri();
044:
045: abstract String getQName();
046:
047: private int rdf(Taint taintMe, String wanted, int fl)
048: throws SAXParseException {
049: if ((fl & select) == fl && wanted.equals(getLocalName())) {
050: if (isInRdfns(taintMe))
051: return fl;
052: if (getQName().toLowerCase().startsWith("rdf:"))
053: frame
054: .warning(
055: taintMe,
056: WARN_NOT_RDF_NAMESPACE,
057: getQName()
058: + " is not special. "
059: + "The namespace binding of the RDF namespace is incorrect. It should be <"
060: + rdfns + "> not <" + getUri()
061: + ">");
062: }
063: return 0;
064: }
065:
066: int lookup(Taint taintMe) throws SAXParseException {
067: int rslt = lookupNoMsg(taintMe);
068: if ((rslt & bad) != 0) {
069: switch (rslt) {
070: case A_DEPRECATED:
071: deprecatedAttribute(taintMe, rslt);
072: break;
073: case A_BAGID:
074: bagIDAttribute(taintMe, rslt);
075: break;
076: default:
077: error(taintMe, rslt);
078: }
079: }
080: return rslt;
081: }
082:
083: abstract void bagIDAttribute(Taint taintMe, int rslt)
084: throws SAXParseException;
085:
086: private int lookupNoMsg(Taint taintMe) throws SAXParseException {
087: char firstChar;
088: try {
089: firstChar = getLocalName().charAt(0);
090: } catch (StringIndexOutOfBoundsException e) {
091: // Yes this really happens with the DOM one.
092: // How disgusting.
093: // When xmlns="eg:a"
094: // xmlns is the prefix ...
095: // System.err.println(getUri());
096: if (this .getUri().equals(xmlnsns))
097: return A_XMLNS;
098: throw e;
099: }
100: switch (firstChar) {
101: case 'b': /* base bagID */
102: switch (getLocalName().length()) {
103: case 4:
104: return xml("base", A_XMLBASE);
105: case 5:
106: return rdf(taintMe, "bagID", A_BAGID);
107: }
108: break;
109: case 'l': /* lang li */
110: switch (getLocalName().length()) {
111: case 2:
112: return rdf(taintMe, "li", E_LI);
113: case 4:
114: return xml("lang", A_XMLLANG);
115: }
116: break;
117: case 's': /* space */
118: return xml("space", A_XML_OTHER);
119: case 'i': /* space */
120: return xml("id", A_XML_OTHER);
121: case 'I': /* ID */
122: return rdf(taintMe, "ID", A_ID);
123: case 'n': /* nodeID */
124: return rdf(taintMe, "nodeID", A_NODEID);
125: case 'a': /* about aboutEach aboutEachPrefix */
126: switch (getLocalName().length()) {
127: case 5:
128: return rdf(taintMe, "about", A_ABOUT);
129: case 9:
130: return rdf(taintMe, "aboutEach", A_DEPRECATED);
131: case 15:
132: return rdf(taintMe, "aboutEachPrefix", A_DEPRECATED);
133: }
134: break;
135: case 'r': /* resource */
136: return rdf(taintMe, "resource", A_RESOURCE);
137: case 'R': /* resource */
138: return rdf(taintMe, "RDF", E_RDF);
139: case 'd': /* datatype */
140: return rdf(taintMe, "datatype", A_DATATYPE);
141: case 't': /* type */
142: return rdf(taintMe, "type", A_TYPE);
143: case 'p': /* parseType */
144: return rdf(taintMe, "parseType", A_PARSETYPE);
145: case 'D': /* Description */
146: return rdf(taintMe, "Description", E_DESCRIPTION);
147: }
148: return 0;
149: }
150:
151: // static final Set rdfnames = new HashSet();
152: // static {
153: // rdfnames.add("Description");
154: // rdfnames.add("RDF");
155: // rdfnames.add("li");
156: // }
157:
158: static final Set knownRDFProperties = new HashSet();
159:
160: static final Set knownRDFTypes = knownRDFProperties;
161: static {
162: knownRDFTypes.add("Bag");
163: knownRDFTypes.add("Seq");
164: knownRDFTypes.add("Alt");
165: knownRDFTypes.add("List");
166: knownRDFTypes.add("XMLLiteral");
167: knownRDFTypes.add("Property");
168: knownRDFProperties.add("type");
169: knownRDFTypes.add("Statement");
170: knownRDFProperties.add("subject");
171: knownRDFProperties.add("predicate");
172: knownRDFProperties.add("object");
173: knownRDFProperties.add("value");
174: knownRDFProperties.add("first");
175: knownRDFProperties.add("rest");
176: // not strictly true.
177: knownRDFProperties.add("nil");
178: }
179:
180: // static final Set knownBadRDFNames = new HashSet();
181: // static {
182: // knownBadRDFNames.add("ID");
183: // knownBadRDFNames.add("about");
184: // knownBadRDFNames.add("aboutEach");
185: // knownBadRDFNames.add("aboutEachPrefix");
186: // knownBadRDFNames.add("resource");
187: // knownBadRDFNames.add("bagID");
188: // knownBadRDFNames.add("parseType");
189: // knownBadRDFNames.add("datatype");
190: // knownBadRDFNames.add("li");
191: // knownBadRDFNames.add("type");
192: // knownBadRDFNames.add("Description");
193: // knownBadRDFNames.add("nodeID");
194: // }
195:
196: protected static boolean isMemberProperty(String name) {
197: if (name.startsWith("_")) {
198: String number = name.substring(1);
199: if (number.startsWith("-") || number.startsWith("0"))
200: return false;
201: try {
202: Integer.parseInt(number);
203: return true;
204: } catch (NumberFormatException e) {
205: try {
206: // It might be > Integer.MAX_VALUE
207: new java.math.BigInteger(number);
208: return true;
209: } catch (NumberFormatException ee) {
210: return false;
211: }
212: }
213: }
214: return false;
215: }
216:
217: static public boolean isKnownRDFProperty(String name) {
218: return knownRDFProperties.contains(name)
219: || isMemberProperty(name);
220: }
221:
222: static public boolean isKnownNonMemberRDFProperty(String name) {
223: return knownRDFProperties.contains(name);
224: }
225:
226: }
227:
228: /*
229: * (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
230: * All rights reserved.
231: *
232: * Redistribution and use in source and binary forms, with or without
233: * modification, are permitted provided that the following conditions
234: * are met:
235: * 1. Redistributions of source code must retain the above copyright
236: * notice, this list of conditions and the following disclaimer.
237: * 2. Redistributions in binary form must reproduce the above copyright
238: * notice, this list of conditions and the following disclaimer in the
239: * documentation and/or other materials provided with the distribution.
240: * 3. The name of the author may not be used to endorse or promote products
241: * derived from this software without specific prior written permission.
242: *
243: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
244: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
245: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
246: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
247: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
248: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
249: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
250: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
251: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
252: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
253: */
|