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.states;
007:
008: import org.xml.sax.Attributes;
009: import org.xml.sax.SAXParseException;
010:
011: import com.hp.hpl.jena.rdf.arp.ARPErrorNumbers;
012: import com.hp.hpl.jena.rdf.arp.impl.ARPString;
013: import com.hp.hpl.jena.rdf.arp.impl.AbsXMLContext;
014: import com.hp.hpl.jena.rdf.arp.impl.AttributeLexer;
015: import com.hp.hpl.jena.rdf.arp.impl.ElementLexer;
016: import com.hp.hpl.jena.rdf.arp.impl.Names;
017: import com.hp.hpl.jena.rdf.arp.impl.ParserSupport;
018: import com.hp.hpl.jena.rdf.arp.impl.QNameLexer;
019: import com.hp.hpl.jena.rdf.arp.impl.Taint;
020: import com.hp.hpl.jena.rdf.arp.impl.TaintImpl;
021: import com.hp.hpl.jena.rdf.arp.impl.URIReference;
022: import com.hp.hpl.jena.rdf.arp.impl.XMLHandler;
023:
024: public abstract class Frame extends ParserSupport implements Names,
025: FrameI, ARPErrorNumbers {
026: final FrameI parent;
027:
028: protected boolean nonWhiteMsgGiven = false;
029:
030: public Taint taint = new TaintImpl();
031:
032: public FrameI getParent() {
033: return parent;
034: }
035:
036: public Frame(FrameI p, AttributeLexer ap) throws SAXParseException {
037: super (p.getXMLHandler(), ap.xml(p.getXMLContext()));
038: parent = p;
039: }
040:
041: public Frame(FrameI p, AbsXMLContext x) {
042: super (p.getXMLHandler(), x);
043: parent = p;
044: }
045:
046: public Frame(XMLHandler a, AbsXMLContext x) {
047: super (a, x);
048: parent = null;
049: }
050:
051: protected void warning(int i, String msg) throws SAXParseException {
052: warning(taint, i, msg);
053: }
054:
055: public void afterChild() {
056: taint = new TaintImpl();
057: }
058:
059: public void comment(char[] ch, int start, int length)
060: throws SAXParseException {
061: // generally ignore
062: }
063:
064: // public void checkIdSymbol(String id) throws SAXParseException {
065: // checkIdSymbol(xml,id);
066: // }
067: /**
068: * endElement is called on the state of the frame created by the matching
069: * startElement.
070: *
071: * @throws SAXParseException
072: *
073: */
074: public void endElement() throws SAXParseException {
075: // often nothing
076: }
077:
078: public void processingInstruction(String target, String data)
079: throws SAXParseException {
080: // generally ignored, maybe not what was intended.
081: warning(null, WARN_PROCESSING_INSTRUCTION_IN_RDF,
082: "A processing instruction is in RDF content. No processing was done. "
083: + suggestParsetypeLiteral());
084: }
085:
086: void processPropertyAttributes(AttributeLexer ap, Attributes atts,
087: AbsXMLContext x) throws SAXParseException {
088: if (ap.type != null) {
089: ((HasSubjectFrameI) this ).aPredAndObj(RDF_TYPE,
090: URIReference.resolve(this , x, ap.type));
091: }
092: int sz = atts.getLength();
093: if (ap.count != sz) {
094: for (int i = 0; i < sz; i++) {
095: if (!ap.done(i)) {
096: String uri = atts.getURI(i);
097: String lName = atts.getLocalName(i);
098: URIReference pred = URIReference.fromQName(this ,
099: uri, lName);
100: if (uri == null || uri.equals("")) {
101: warning(
102: pred,
103: WARN_UNQUALIFIED_ATTRIBUTE,
104: "Unqualified property attributes are not allowed. Property treated as a relative URI.");
105: }
106: if (rdfns.equals(uri)
107: && !QNameLexer.isKnownRDFProperty(lName)) {
108: warning(
109: pred,
110: WARN_UNKNOWN_RDF_ATTRIBUTE,
111: atts.getQName(i)
112: + " is not a recognized RDF property or type.");
113: }
114: ((HasSubjectFrameI) this ).aPredAndObj(pred,
115: new ARPString(this , atts.getValue(i), x));
116: }
117: }
118: }
119: }
120:
121: public void abort() {
122: // nothing.
123: }
124:
125: protected FrameI rdfStartElement(String uri, String localName,
126: String rawName, Attributes atts) throws SAXParseException {
127: ElementLexer el = new ElementLexer(taint, this , uri, localName,
128: rawName, E_RDF, 0, false);
129: if (el.goodMatch) {
130: AttributeLexer ap = new AttributeLexer(this , A_XMLBASE
131: | A_XMLLANG | A_XML_OTHER, 0);
132: if (ap.processSpecials(taint, atts) != atts.getLength()) {
133: warning(ERR_SYNTAX_ERROR,
134: "Illegal attributes on rdf:RDF");
135: }
136: // TODO this may be one point to intercept xml:base.
137: arp.startRDF();
138: return new WantTopLevelDescription(this , ap);
139: }
140: AttributeLexer ap = new AttributeLexer(this , A_XMLBASE
141: | A_XMLLANG, 0);
142: ap.processSpecials(taint, atts);
143: return new LookingForRDF(this , ap);
144: }
145:
146: /**
147: * Additional message if mixed content is found in a syntactically
148: * disallowed place. Subclasses override to suppress message.
149: *
150: */
151: String suggestParsetypeLiteral() {
152: return " Maybe there should be an rdf:parseType='Literal' for embedding mixed XML content in RDF.";
153: }
154:
155: public void characters(char[] ch, int start, int length)
156: throws SAXParseException {
157: if ((!nonWhiteMsgGiven) && !isWhite(ch, start, length)) {
158: nonWhiteMsgGiven = true;
159: warning(ERR_NOT_WHITESPACE,
160: "Expecting XML start or end element(s). String data \""
161: + new String(ch, start, length)
162: + "\" not allowed."
163: + suggestParsetypeLiteral()
164: + " Maybe a striping error.");
165: }
166: }
167:
168: }
169:
170: /*
171: * (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP All rights
172: * reserved.
173: *
174: * Redistribution and use in source and binary forms, with or without
175: * modification, are permitted provided that the following conditions are met:
176: * 1. Redistributions of source code must retain the above copyright notice,
177: * this list of conditions and the following disclaimer. 2. Redistributions in
178: * binary form must reproduce the above copyright notice, this list of
179: * conditions and the following disclaimer in the documentation and/or other
180: * materials provided with the distribution. 3. The name of the author may not
181: * be used to endorse or promote products derived from this software without
182: * specific prior written permission.
183: *
184: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
185: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
186: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
187: * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
188: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
189: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
190: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
191: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
192: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
193: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
194: */
|