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.BitSet;
009:
010: import org.xml.sax.Attributes;
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: public class AttributeLexer extends QNameLexer implements
017: ARPErrorNumbers {
018:
019: public AttributeLexer(Frame f, int which, int bad) {
020: super (f, which, bad);
021: }
022:
023: String lang;
024:
025: String base;
026:
027: public String about;
028:
029: public String datatype;
030:
031: public String resource;
032:
033: public String nodeID;
034:
035: public String id;
036:
037: public String type;
038:
039: public String parseType;
040:
041: public BitSet done;
042:
043: public int count;
044:
045: int index;
046:
047: Attributes att;
048:
049: AbsXMLContext xml;
050:
051: public int processSpecials(Taint taintMe, Attributes a)
052: throws SAXParseException {
053: att = a;
054: int sz = a.getLength();
055: done = new BitSet(sz);
056: count = 0;
057: for (index = 0; index < sz; index++) {
058: boolean matched = true;
059: switch (lookup(taintMe)) {
060: case A_XMLBASE:
061: base = value();
062: frame
063: .warning(null, IGN_XMLBASE_USED,
064: "Use of attribute xml:base is not envisaged in RDF Model&Syntax.");
065: break;
066: case A_DEPRECATED:
067: case A_BAGID:
068: case E_LI:
069: case E_RDF:
070: case E_DESCRIPTION:
071: break;
072:
073: case A_XMLLANG:
074: lang = value();
075:
076: break;
077: case A_XML_OTHER:
078: case A_XMLNS:
079: break;
080: case A_ID:
081: id = value(taintMe, id);
082: // frame.checkIdSymbol(id);
083: break;
084: case A_NODEID:
085: nodeID = value(taintMe, nodeID);
086: break;
087: case A_ABOUT:
088: about = value(taintMe, about);
089: break;
090: case A_RESOURCE:
091: resource = value(taintMe, resource);
092: break;
093: case A_DATATYPE:
094: datatype = value(taintMe, datatype);
095: break;
096: case A_TYPE:
097: type = value(taintMe, type);
098: break;
099: case A_PARSETYPE:
100: parseType = value(taintMe, parseType);
101: break;
102: case 0:
103: if ((select & A_XML_OTHER) == A_XML_OTHER) {
104: String qn = getQName();
105: if ((qn.length() >= 3 && qn.substring(0, 3)
106: .toLowerCase().equals("xml"))
107: || xmlns.equals(getUri())) {
108: // Some tools, e.g. DOM, won't let us switch off
109: // namespaces. Hence, they fall through to here.
110: if (!xmlnsns.equals(getUri()))
111: frame
112: .warning(
113: null,
114: WARN_UNKNOWN_XML_ATTRIBUTE,
115: "XML attribute: "
116: + getQName()
117: + " is not known and is being discarded.");
118: break;
119: }
120: }
121: matched = false;
122: break;
123: default:
124: throw new IllegalStateException("impossible");
125: }
126: if (matched) {
127: done.set(index);
128: count++;
129: }
130: }
131: xml = computeXml(frame.getXMLContext());
132: return count;
133: }
134:
135: public AbsXMLContext xml(AbsXMLContext in) throws SAXParseException {
136: if (xml == null)
137: xml = computeXml(in);
138: return xml;
139: }
140:
141: private AbsXMLContext computeXml(AbsXMLContext in)
142: throws SAXParseException {
143: if (base != null) {
144: in = in.withBase(frame.arp, base);
145: }
146: if (lang != null)
147: in = in.withLang(frame.arp, lang);
148: return in;
149: }
150:
151: boolean isInRdfns(Taint taintMe) throws SAXParseException {
152: String uri = getUri();
153: if (rdfns.equals(uri))
154: return true;
155: if (uri.equals("")) {
156: frame.warning(taintMe, WARN_UNQUALIFIED_ATTRIBUTE,
157: "unqualified use of rdf:" + getQName()
158: + " is deprecated.");
159: return true;
160: }
161: return false;
162: }
163:
164: void error(Taint taintMe, int r) throws SAXParseException {
165: // TODO: not for 2.3. specialize ERR_SYNTAX_ERROR ?
166: int e = ERR_SYNTAX_ERROR;
167: switch (r) {
168: case E_LI:
169: case E_DESCRIPTION:
170: case E_RDF:
171: case A_DEPRECATED:
172: case A_BAGID:
173: e = ERR_BAD_RDF_ATTRIBUTE;
174: break;
175:
176: }
177: frame.warning(taintMe, e, getQName()
178: + " not allowed as attribute"
179: + (e == ERR_BAD_RDF_ATTRIBUTE ? "." : " here."));
180:
181: }
182:
183: void deprecatedAttribute(Taint me, int r) throws SAXParseException {
184: frame.warning(me, ERR_BAD_RDF_ATTRIBUTE, getQName()
185: + " has been deprecated.");
186: }
187:
188: String getLocalName() {
189: return att.getLocalName(index);
190: }
191:
192: String getUri() {
193: return att.getURI(index);
194: }
195:
196: private String value() {
197: return att.getValue(index);
198: }
199:
200: /**
201: Answer the xml:base value, or null if there wasn't one.
202: [Added by kers, in search of xml:base processing]
203: */
204: public String getXMLBase() {
205: return base;
206: }
207:
208: private String value(Taint taintMe, String prev)
209: throws SAXParseException {
210: if (prev != null) {
211: frame
212: .warning(
213: taintMe,
214: ERR_SYNTAX_ERROR,
215: "Cannot use "
216: + getQName()
217: + " in both qualified and unqualifed form on same element");
218: }
219: return att.getValue(index);
220: }
221:
222: String getQName() {
223: return att.getQName(index);
224: }
225:
226: public boolean done(int i) {
227: return done.get(i);
228: }
229:
230: void bagIDAttribute(Taint taintMe, int rslt)
231: throws SAXParseException {
232: deprecatedAttribute(null, rslt);
233: }
234: }
235:
236: /*
237: * (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP All rights
238: * reserved.
239: *
240: * Redistribution and use in source and binary forms, with or without
241: * modification, are permitted provided that the following conditions are met:
242: * 1. Redistributions of source code must retain the above copyright notice,
243: * this list of conditions and the following disclaimer. 2. Redistributions in
244: * binary form must reproduce the above copyright notice, this list of
245: * conditions and the following disclaimer in the documentation and/or other
246: * materials provided with the distribution. 3. The name of the author may not
247: * be used to endorse or promote products derived from this software without
248: * specific prior written permission.
249: *
250: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
251: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
252: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
253: * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
254: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
255: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
256: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
257: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
258: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
259: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
260: */
|