|
From: <pm_...@us...> - 2011-05-18 10:11:51
|
Revision: 4353
http://mxquery.svn.sourceforge.net/mxquery/?rev=4353&view=rev
Author: pm_fischer
Date: 2011-05-18 10:11:44 +0000 (Wed, 18 May 2011)
Log Message:
-----------
For SAX parsing, disable namespace-prefixes-mode
1) Not available on Android
2) Cleaner namespace scope handling by using SAX callbacks
Android IOLib now works
Modified Paths:
--------------
trunk/MXQuery/android/src/ch/ethz/mxquery/util/IOLib.java
trunk/MXQuery/src/ch/ethz/mxquery/xdmio/xmlAdapters/NonSchemaValidatingSaxImportAdapter.java
trunk/MXQuery/src/ch/ethz/mxquery/xdmio/xmlAdapters/XDMImportAdapter.java
Modified: trunk/MXQuery/android/src/ch/ethz/mxquery/util/IOLib.java
===================================================================
--- trunk/MXQuery/android/src/ch/ethz/mxquery/util/IOLib.java 2011-05-17 10:42:00 UTC (rev 4352)
+++ trunk/MXQuery/android/src/ch/ethz/mxquery/util/IOLib.java 2011-05-18 10:11:44 UTC (rev 4353)
@@ -38,6 +38,7 @@
import ch.ethz.mxquery.exceptions.ErrorCodes;
import ch.ethz.mxquery.exceptions.MXQueryException;
import ch.ethz.mxquery.exceptions.QueryLocation;
+import ch.ethz.mxquery.util.Utils;
public class IOLib {
@@ -166,6 +167,12 @@
if (encoding == null)
encoding = "utf-8";
InputStream ins = null;
+ //android-specific treatment of local/file URIs:
+ //Strip all but the the last part
+ if (toOpen.startsWith("file:/")||toOpen.indexOf(":") < 0) {
+ String [] parts = Utils.split(toOpen, "/", true);
+ toOpen = parts[parts.length-1];
+ }
URI uri;
try {
uri = new URI(toOpen);
Modified: trunk/MXQuery/src/ch/ethz/mxquery/xdmio/xmlAdapters/NonSchemaValidatingSaxImportAdapter.java
===================================================================
--- trunk/MXQuery/src/ch/ethz/mxquery/xdmio/xmlAdapters/NonSchemaValidatingSaxImportAdapter.java 2011-05-17 10:42:00 UTC (rev 4352)
+++ trunk/MXQuery/src/ch/ethz/mxquery/xdmio/xmlAdapters/NonSchemaValidatingSaxImportAdapter.java 2011-05-18 10:11:44 UTC (rev 4353)
@@ -1,521 +1,547 @@
-/* Copyright 2006 - 2009 ETH Zurich
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package ch.ethz.mxquery.xdmio.xmlAdapters;
-
-import java.io.IOException;
-import java.util.Properties;
-import java.util.Vector;
-
-import javax.xml.parsers.ParserConfigurationException;
-import javax.xml.parsers.SAXParser;
-import javax.xml.parsers.SAXParserFactory;
-
-import org.xml.sax.Attributes;
-import org.xml.sax.InputSource;
-import org.xml.sax.SAXException;
-import org.xml.sax.SAXParseException;
-import org.xml.sax.XMLReader;
-import org.xml.sax.ext.DeclHandler;
-import org.xml.sax.ext.LexicalHandler;
-import org.xml.sax.helpers.DefaultHandler;
-
-import ch.ethz.mxquery.contextConfig.Context;
-import ch.ethz.mxquery.datamodel.QName;
-import ch.ethz.mxquery.datamodel.XQName;
-import ch.ethz.mxquery.datamodel.types.Type;
-import ch.ethz.mxquery.datamodel.xdm.CommentToken;
-import ch.ethz.mxquery.datamodel.xdm.ElementToken;
-import ch.ethz.mxquery.datamodel.xdm.NamedToken;
-import ch.ethz.mxquery.datamodel.xdm.ProcessingInstrToken;
-import ch.ethz.mxquery.datamodel.xdm.TextToken;
-import ch.ethz.mxquery.datamodel.xdm.Token;
-import ch.ethz.mxquery.datamodel.xdm.TokenInterface;
-import ch.ethz.mxquery.exceptions.DynamicException;
-import ch.ethz.mxquery.exceptions.ErrorCodes;
-import ch.ethz.mxquery.exceptions.MXQueryException;
-import ch.ethz.mxquery.exceptions.QueryLocation;
-import ch.ethz.mxquery.model.XDMIterator;
-import ch.ethz.mxquery.util.Utils;
-
-public class NonSchemaValidatingSaxImportAdapter extends XDMImportAdapter {
-
-
- SaxAdapter adapter;
- ParseWorker worker;
- MXQueryException parseException = null;
- protected InputSource tobeParsed;
-
- Vector tokens;
- TokenInterface [] currBulk;
- int bulkPos = 0;
- private static final int TOKENBLOCKSIZE = 100;
- private static final int PARSED_BLOCKS_WAITING = 10;
- int validationMode;
- boolean tidyInput;
- private boolean done = false;
-
-
- class SaxAdapter extends DefaultHandler implements LexicalHandler, DeclHandler{
-
- private boolean startCData;
-
- private XMLReader reader;
-
- private boolean inDTD = false;
-
- private StringBuffer pendingTextContent = new StringBuffer();
- TokenInterface [] currToPush = new TokenInterface[TOKENBLOCKSIZE];
- int blockPos = 0;
-
- public SaxAdapter(XMLReader source) {
- reader = source;
- }
-
- public void parse() throws SAXException, IOException,MXQueryException {
- try {
- if (reader == null) {
- String saxParser = System.getProperty("javax.xml.parsers.SAXParserFactory");
- if (tidyInput) {
- System.setProperty("javax.xml.parsers.SAXParserFactory", "org.ccil.cowan.tagsoup.jaxp.SAXFactoryImpl");
- } else
- System.setProperty("javax.xml.parsers.SAXParserFactory", "org.apache.xerces.jaxp.SAXParserFactoryImpl");
-
- SAXParserFactory spf = SAXParserFactory.newInstance();
- if (saxParser != null)
- System.setProperty("javax.xml.parsers.SAXParserFactory", saxParser);
- else {
- // use indirect way, since clearProperties is not available in 1.4
- Properties sysProps = System.getProperties();
- sysProps.remove("javax.xml.parsers.SAXParserFactory");
- }
-
- //if (!tidyInput) {
- spf.setNamespaceAware(true);
- spf.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
- //} else {
- // spf.setFeature("http://xml.org/sax/features/namespaces",false);
- //}
- if (validationMode == Context.DTD_VALIDATION) {
- spf.setFeature("http://xml.org/sax/features/validation", true);
- spf.setFeature("http://apache.org/xml/features/validation/dynamic", true);
- } else if (validationMode == Context.IGNORE_DTD){
- spf.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar",false);
- spf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd",false);
- }
-
- SAXParser parser = spf.newSAXParser();
- reader = parser.getXMLReader();
- }
- reader.setErrorHandler(this);
- reader.setProperty("http://xml.org/sax/properties/lexical-handler", this);
- if (validationMode == Context.DTD_VALIDATION) {
- reader.setProperty("http://xml.org/sax/properties/declaration-handler", this);
- }
- // reader.setProperty("http://apache.org/xml/properties/schema/external-schemaLocation", context.initDictionary().getSchemaLocations());
- reader.setContentHandler(this);
- if (tobeParsed != null)
- reader.parse(tobeParsed);
- else
- reader.parse((String)null);
- } catch (SAXException e) {
- throw new DynamicException(ErrorCodes.A0007_EC_IO,"Error creating validating input: "+e.toString(),loc);
- } catch (ParserConfigurationException e) {
- throw new MXQueryException(ErrorCodes.A0002_EC_NOT_SUPPORTED,"Error creating validating input - parser configuration error",loc);
- }
- }
-
- private void addToken(TokenInterface tok) throws SAXException{
- currToPush[blockPos++] = tok;
- if (blockPos == currToPush.length || tok == Token.END_SEQUENCE_TOKEN) {
- synchronized(tokens) {
- while (tokens.size() > PARSED_BLOCKS_WAITING)
- try {
- tokens.wait();
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- throw new SAXException("Stop worker thread");
- }
- tokens.add(currToPush);
- tokens.notify();
- }
- currToPush = new TokenInterface[TOKENBLOCKSIZE];
- blockPos = 0;
- }
- }
-
- public void processingInstruction(String target, String data) throws SAXException {
- emitPendingTextContent();
- try {
- addToken(new ProcessingInstrToken(createNextTokenId(Type.PROCESSING_INSTRUCTION,null), data, target,curNsScope));
- } catch (MXQueryException e) {
- e.printStackTrace();
- }
- }
-
- public void startDocument() throws SAXException {
- emitPendingTextContent();
- addToken(new Token(Type.START_DOCUMENT, createNextTokenId(Type.START_DOCUMENT,null),curNsScope));
- if (getURI() != null)
- curNsScope.setBaseURI(getURI());
- //level++;
- }
-
- public void endDocument() throws SAXException {
- emitPendingTextContent();
- addToken(new Token(Type.END_DOCUMENT, null,curNsScope));
- addToken(Token.END_SEQUENCE_TOKEN);
- level--;
- }
-
- /** Start element. */
- public void startElement(String uri, String localName, String qname, Attributes attributes) throws SAXException {
- emitPendingTextContent();
- level++;
- Vector myToks = new Vector();
- boolean foundId = false;
- boolean foundIdREFS = false;
-
- String xmlId = null;
- String [] xmlIdREFS = null;
-
- String prefix = null;
- String localPart;
- boolean createdNSScope = false;
- int splitPos = qname.indexOf(':');
- if (splitPos > 0) {
- prefix = qname.substring(0,splitPos);
- localPart = qname.substring(splitPos+1);
- } else
- localPart = qname;
- XQName qName = new QName(uri, prefix, localPart);
- NamedToken token = new ElementToken(Type.START_TAG, createNextTokenId(Type.START_TAG,qName.toString()), qName, curNsScope);
- myToks.add(token);
-
- for (int i = 0; i < attributes.getLength(); i++) {
- int type;
- boolean setID = false;
- type = Type.UNTYPED_ATOMIC; //TODO: DTD types annotation, e.g. ID?
-
- try {
- QName attQname = new QName(attributes.getQName(i));
- String attVal = attributes.getValue(i);
-
- if (!foundId && (type == Type.ID || isXMLId(attQname,qName))) {
- foundId = true;
- xmlId = attVal;
- }
- if (!foundIdREFS && (type == Type.IDREF || isIDREF(attQname,qName))) {
- foundIdREFS = true;
- xmlIdREFS = new String[]{attVal};
- }
-
- if (!foundIdREFS && (type == Type.IDREFS || isIDREFS(attQname,qName))) {
- foundIdREFS = true;
- xmlIdREFS = Utils.split(attVal," ", false);
- }
-
-
- boolean newOpened = checkOpenNsScopeAddNs(createdNSScope, attQname, attVal);
- if (newOpened && !createdNSScope)
- myToks.setElementAt(new ElementToken(Type.START_TAG, token.getNodeId(), qName, curNsScope), 0);
-
- if (attQname.getNamespacePrefix() == null || attQname.getNamespacePrefix().equals("")) {
- if (attQname.getLocalPart().equals("xmlns"))
- continue;
- } else if (attQname.getNamespacePrefix().equals("xmlns"))
- continue;
-
- attQname.setNamespaceURI(attributes.getURI(i));
- NamedToken attToken = createAttributeToken(type, attributes.getValue(i), attQname, curNsScope);
- if (foundId && !setID){
- attToken.setID(xmlId);
- setID = true;
- }
- if (foundIdREFS){
- attToken.setIDREFS(xmlIdREFS);
- foundIdREFS = false;
- }
- myToks.add(attToken);
- } catch (MXQueryException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- if (level < 2 && uri != null && localName.equals(qname)&& !createdNSScope) {
- try {
- checkOpenNsScopeAddNs(createdNSScope, new QName(Context.NS_XMLNS), uri);
- myToks.setElementAt(new ElementToken(Type.START_TAG, token.getNodeId(), qName, curNsScope), 0);
- } catch (MXQueryException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
-
-
- if (foundId) {
- NamedToken nmToken = (NamedToken)myToks.elementAt(0);
- nmToken.setID(xmlId);
- }
- for (int i=0;i<myToks.size();i++) {
- addToken((TokenInterface)myToks.elementAt(i));
- }
- } // startElement(String,String,String,Attributes)
-
-
- /** End element. */
- public void endElement(String uri, String localName, String qname) throws SAXException {
- emitPendingTextContent();
- String prefix = null;
- String localPart;
- //String[] qNameParts = qname.split(":");
- int splitPos = qname.indexOf(':');
- if (splitPos > 0) {
- prefix = qname.substring(0,splitPos);
- localPart = qname.substring(splitPos+1);
- } else
- localPart = qname;
-
- XQName qName = new QName(uri, prefix, localPart);
- NamedToken token = new ElementToken(Type.END_TAG, null, qName, curNsScope);
- addToken(token);
- checkCloseNsScope();
- level--;
- } // endElement(String,String,String)
-
- public void ignorableWhitespace(char[] ch, int start, int length)
- throws SAXException {
- characters(ch, start, length);
- }
-
- private void emitPendingTextContent() throws SAXException{
- if (pendingTextContent.length() > 0) {
- int type = Type.TEXT_NODE_UNTYPED_ATOMIC;
- try {
- addToken(new TextToken(type,createNextTokenId(type,null), pendingTextContent.toString(), curNsScope));
- } catch (MXQueryException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- pendingTextContent = new StringBuffer();
- }
-
- }
- public void characters(char[] ch, int start, int length) throws SAXException {
-
-// for (int i = start; i < start + length; i++) {
-// text.append(ch[i]);
-// }
- int type = Type.TEXT_NODE_UNTYPED_ATOMIC;
- if (startCData) {
- emitPendingTextContent();
- try {
- addToken(new TextToken(type,createNextTokenId(type,null), new String(ch,start,length), curNsScope));
- }catch (MXQueryException e) {
- throw new SAXException(e.toString());
- }
-
- } else {
- pendingTextContent.append(ch,start,length);
- }
- }
-
- public void error(SAXParseException e) throws SAXException {
- throw e;
- }
-
- public void fatalError(SAXParseException e) throws SAXException {
- throw e;
- }
-
- public void warning(SAXParseException e) throws SAXException {
- System.out.println("Warning: " + e.getMessage());
- }
-
- public void comment(char[] ch, int start, int end) throws SAXException {
- emitPendingTextContent();
- if (!inDTD) {
- // only take comments from the main file, not from the DTD
- String text = new String(ch, start, end);
- try {
- addToken(new CommentToken(createNextTokenId(Type.COMMENT,null), text,curNsScope));
- } catch (DynamicException e) {
- throw new SAXException(e);
- }
- }
- }
-
- public void endCDATA() throws SAXException {
- startCData = false;
- }
-
- public void endDTD() throws SAXException {
- inDTD = false;
- }
-
- public void endEntity(String arg0) throws SAXException {
- //System.out.println("Entity ended");
- }
-
- public void startCDATA() throws SAXException {
- startCData = true;
- }
-
- public void startDTD(String name, String publicID, String systemID) throws SAXException {
- systemid = systemID;
- publicid = publicID;
- dtdRootElem = name;
- inDTD = true;
- }
-
- public void startEntity(String arg0) throws SAXException {
- //System.out.println("Entitiy started "+arg0);
- }
-
- public void attributeDecl(String name, String name2, String type,String mode, String value) throws SAXException {
- if (type.equals("ID"))idsVector.add(name+"#"+name2);
- if (type.equals("IDREF")) idRefVector.add(name+"#"+name2);
- if (type.equals("IDREFS")) idRefsVector.add(name+"#"+name2);
- }
-
- public void elementDecl(String name, String model) throws SAXException {
- //System.out.println("element decl"+model);
- }
-
- public void externalEntityDecl(String name, String publicId,
- String systemId) throws SAXException {
-// System.out.println("external decl");
-
- }
-
- public void internalEntityDecl(String name, String value)
- throws SAXException {
- // System.out.println("internal decl");
-
- }
- }
-
-
- class ParseWorker extends Thread {
- Vector tokens;
- SaxAdapter adapter;
-
- public ParseWorker(Vector tokens, SaxAdapter adapter) {
- super();
- this.tokens = tokens;
- this.adapter = adapter;
- }
-
- public void run() {
- try {
- adapter.parse();
- }
- catch (SAXException e) {
- String message = e.getMessage();
- parseException = new DynamicException(ErrorCodes.E0027_DYNAMIC_VALIDATE_UNEXPECTED_VALIDITY, message, loc);
- synchronized(tokens) {
- tokens.notify();
- }
-
- } catch (IOException e) {
- parseException = new MXQueryException(ErrorCodes.A0007_EC_IO,"I/O Error while parsing: "+e.toString(),loc);
- synchronized(tokens) {
- tokens.notify();
- }
- } catch (MXQueryException e) {
- parseException = e;
- synchronized(tokens) {
- tokens.notify();
- }
- }
- }
- }
-
- public NonSchemaValidatingSaxImportAdapter(Context ctx, QueryLocation loc,InputSource xml, int validationMode, boolean tidyInput) {
- super(ctx,loc);
- adapter= new SaxAdapter(null);
- this.tokens = new Vector();
- tobeParsed = xml;
- this.tidyInput = tidyInput;
- this.validationMode= validationMode;
- }
-
- public NonSchemaValidatingSaxImportAdapter(Context ctx, QueryLocation loc,XMLReader source) {
- super(ctx,loc);
- adapter= new SaxAdapter(source);
- this.tokens = new Vector();
- }
-
- protected void init() throws MXQueryException {
- worker = new ParseWorker(tokens, adapter);
- worker.start();
- }
-
- protected XDMIterator copy(Context context, XDMIterator[] subIters, Vector nestedPredCtxStack) throws MXQueryException {
- XDMImportAdapter res = new NonSchemaValidatingSaxImportAdapter(this.context, loc,tobeParsed, validationMode, false);
- res.uri = uri;
- return res;
- }
- public TokenInterface next() throws MXQueryException {
- if (called == 0) {
- init();
- called++;
- getBulkFromQueue();
- }
- if (done)
- return Token.END_SEQUENCE_TOKEN;
- if (bulkPos == currBulk.length)
- getBulkFromQueue();
- TokenInterface tok = currBulk[bulkPos++];
- if (tok == Token.END_SEQUENCE_TOKEN)
- done = true;
- return tok;
- }
-
- private void getBulkFromQueue() throws MXQueryException {
- synchronized(tokens) {
- while (tokens.isEmpty() && parseException == null) {
- try {
- tokens.wait();
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- if (parseException != null)
- throw parseException;
- currBulk = (TokenInterface[])tokens.remove(0);
- bulkPos = 0;
- tokens.notify();
- }
- }
-
-
- protected void resetImpl() throws MXQueryException {
- super.resetImpl();
- done = false;
- worker.interrupt();
- }
-
- protected void freeResources(boolean restartable)
- throws MXQueryException {
- // TODO Auto-generated method stub
- super.freeResources(restartable);
- if (worker != null)
- worker.interrupt();
- }
-
-
-}
+/* Copyright 2006 - 2009 ETH Zurich
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package ch.ethz.mxquery.xdmio.xmlAdapters;
+
+import java.io.IOException;
+import java.util.Properties;
+import java.util.Vector;
+
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.parsers.SAXParser;
+import javax.xml.parsers.SAXParserFactory;
+
+import org.xml.sax.Attributes;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+import org.xml.sax.SAXParseException;
+import org.xml.sax.XMLReader;
+import org.xml.sax.ext.DeclHandler;
+import org.xml.sax.ext.LexicalHandler;
+import org.xml.sax.helpers.DefaultHandler;
+
+import ch.ethz.mxquery.contextConfig.Context;
+import ch.ethz.mxquery.datamodel.Namespace;
+import ch.ethz.mxquery.datamodel.QName;
+import ch.ethz.mxquery.datamodel.XQName;
+import ch.ethz.mxquery.datamodel.types.Type;
+import ch.ethz.mxquery.datamodel.xdm.CommentToken;
+import ch.ethz.mxquery.datamodel.xdm.ElementToken;
+import ch.ethz.mxquery.datamodel.xdm.NamedToken;
+import ch.ethz.mxquery.datamodel.xdm.ProcessingInstrToken;
+import ch.ethz.mxquery.datamodel.xdm.TextToken;
+import ch.ethz.mxquery.datamodel.xdm.Token;
+import ch.ethz.mxquery.datamodel.xdm.TokenInterface;
+import ch.ethz.mxquery.datamodel.xdm.XDMScope;
+import ch.ethz.mxquery.exceptions.DynamicException;
+import ch.ethz.mxquery.exceptions.ErrorCodes;
+import ch.ethz.mxquery.exceptions.MXQueryException;
+import ch.ethz.mxquery.exceptions.QueryLocation;
+import ch.ethz.mxquery.model.XDMIterator;
+import ch.ethz.mxquery.util.Utils;
+
+public class NonSchemaValidatingSaxImportAdapter extends XDMImportAdapter {
+
+
+ SaxAdapter adapter;
+ ParseWorker worker;
+ MXQueryException parseException = null;
+ protected InputSource tobeParsed;
+
+ Vector tokens;
+ TokenInterface [] currBulk;
+ int bulkPos = 0;
+ private static final int TOKENBLOCKSIZE = 100;
+ private static final int PARSED_BLOCKS_WAITING = 10;
+ int validationMode;
+ boolean tidyInput;
+ private boolean done = false;
+
+
+ class SaxAdapter extends DefaultHandler implements LexicalHandler, DeclHandler{
+
+ private boolean startCData;
+
+ private XMLReader reader;
+
+ private boolean inDTD = false;
+
+ private StringBuffer pendingTextContent = new StringBuffer();
+ TokenInterface [] currToPush = new TokenInterface[TOKENBLOCKSIZE];
+ int blockPos = 0;
+
+ public SaxAdapter(XMLReader source) {
+ reader = source;
+ }
+
+ public void parse() throws SAXException, IOException,MXQueryException {
+ try {
+ if (reader == null) {
+ String saxParser = System.getProperty("javax.xml.parsers.SAXParserFactory");
+ if (tidyInput) {
+ System.setProperty("javax.xml.parsers.SAXParserFactory", "org.ccil.cowan.tagsoup.jaxp.SAXFactoryImpl");
+ } else
+ System.setProperty("javax.xml.parsers.SAXParserFactory", "org.apache.xerces.jaxp.SAXParserFactoryImpl");
+
+ SAXParserFactory spf = SAXParserFactory.newInstance();
+ if (saxParser != null)
+ System.setProperty("javax.xml.parsers.SAXParserFactory", saxParser);
+ else {
+ // use indirect way, since clearProperties is not available in 1.4
+ Properties sysProps = System.getProperties();
+ sysProps.remove("javax.xml.parsers.SAXParserFactory");
+ }
+
+ //if (!tidyInput) {
+ spf.setNamespaceAware(true);
+ //spf.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
+ //} else {
+ // spf.setFeature("http://xml.org/sax/features/namespaces",false);
+ //}
+ if (validationMode == Context.DTD_VALIDATION) {
+ spf.setFeature("http://xml.org/sax/features/validation", true);
+ spf.setFeature("http://apache.org/xml/features/validation/dynamic", true);
+ } else if (validationMode == Context.IGNORE_DTD){
+ spf.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar",false);
+ spf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd",false);
+ }
+
+ SAXParser parser = spf.newSAXParser();
+ reader = parser.getXMLReader();
+ }
+ reader.setErrorHandler(this);
+ reader.setProperty("http://xml.org/sax/properties/lexical-handler", this);
+ if (validationMode == Context.DTD_VALIDATION) {
+ reader.setProperty("http://xml.org/sax/properties/declaration-handler", this);
+ }
+ // reader.setProperty("http://apache.org/xml/properties/schema/external-schemaLocation", context.initDictionary().getSchemaLocations());
+ reader.setContentHandler(this);
+ if (tobeParsed != null)
+ reader.parse(tobeParsed);
+ else
+ reader.parse((String)null);
+ } catch (SAXException e) {
+ throw new DynamicException(ErrorCodes.A0007_EC_IO,"Error creating validating input: "+e.toString(),loc);
+ } catch (ParserConfigurationException e) {
+ throw new MXQueryException(ErrorCodes.A0002_EC_NOT_SUPPORTED,"Error creating validating input - parser configuration error",loc);
+ }
+ }
+
+ private void addToken(TokenInterface tok) throws SAXException{
+ currToPush[blockPos++] = tok;
+ if (blockPos == currToPush.length || tok == Token.END_SEQUENCE_TOKEN) {
+ synchronized(tokens) {
+ while (tokens.size() > PARSED_BLOCKS_WAITING)
+ try {
+ tokens.wait();
+ } catch (InterruptedException e) {
+ // TODO Auto-generated catch block
+ throw new SAXException("Stop worker thread");
+ }
+ tokens.add(currToPush);
+ tokens.notify();
+ }
+ currToPush = new TokenInterface[TOKENBLOCKSIZE];
+ blockPos = 0;
+ }
+ }
+
+ public void processingInstruction(String target, String data) throws SAXException {
+ emitPendingTextContent();
+ try {
+ addToken(new ProcessingInstrToken(createNextTokenId(Type.PROCESSING_INSTRUCTION,null), data, target,curNsScope));
+ } catch (MXQueryException e) {
+ e.printStackTrace();
+ }
+ }
+
+ public void startDocument() throws SAXException {
+ emitPendingTextContent();
+ addToken(new Token(Type.START_DOCUMENT, createNextTokenId(Type.START_DOCUMENT,null),curNsScope));
+ if (getURI() != null)
+ curNsScope.setBaseURI(getURI());
+ //level++;
+ }
+
+ public void endDocument() throws SAXException {
+ emitPendingTextContent();
+ addToken(new Token(Type.END_DOCUMENT, null,curNsScope));
+ addToken(Token.END_SEQUENCE_TOKEN);
+ level--;
+ }
+
+ /** Start element. */
+ public void startElement(String uri, String localName, String qname, Attributes attributes) throws SAXException {
+ emitPendingTextContent();
+ level++;
+ Vector myToks = new Vector();
+ boolean foundId = false;
+ boolean foundIdREFS = false;
+
+ String xmlId = null;
+ String [] xmlIdREFS = null;
+
+ String prefix = null;
+ String localPart;
+ boolean createdNSScope = false;
+ int splitPos = qname.indexOf(':');
+ if (splitPos > 0) {
+ prefix = qname.substring(0,splitPos);
+ localPart = qname.substring(splitPos+1);
+ } else
+ localPart = qname;
+ XQName qName = new QName(uri, prefix, localPart);
+ NamedToken token = new ElementToken(Type.START_TAG, createNextTokenId(Type.START_TAG,qName.toString()), qName, curNsScope);
+ myToks.add(token);
+
+ for (int i = 0; i < attributes.getLength(); i++) {
+ int type;
+ boolean setID = false;
+ type = Type.UNTYPED_ATOMIC; //TODO: DTD types annotation, e.g. ID?
+
+ try {
+ QName attQname = new QName(attributes.getQName(i));
+ String attVal = attributes.getValue(i);
+
+ if (!foundId && (type == Type.ID || isXMLId(attQname,qName))) {
+ foundId = true;
+ xmlId = attVal;
+ }
+ if (!foundIdREFS && (type == Type.IDREF || isIDREF(attQname,qName))) {
+ foundIdREFS = true;
+ xmlIdREFS = new String[]{attVal};
+ }
+
+ if (!foundIdREFS && (type == Type.IDREFS || isIDREFS(attQname,qName))) {
+ foundIdREFS = true;
+ xmlIdREFS = Utils.split(attVal," ", false);
+ }
+
+
+ boolean newOpened = checkOpenNsScopeAddNs(createdNSScope, attQname, attVal);
+ //boolean newOpened = false;
+ if (newOpened && !createdNSScope)
+ myToks.setElementAt(new ElementToken(Type.START_TAG, token.getNodeId(), qName, curNsScope), 0);
+
+ if (attQname.getNamespacePrefix() == null || attQname.getNamespacePrefix().equals("")) {
+ if (attQname.getLocalPart().equals("xmlns"))
+ continue;
+ } else if (attQname.getNamespacePrefix().equals("xmlns"))
+ continue;
+
+ attQname.setNamespaceURI(attributes.getURI(i));
+ NamedToken attToken = createAttributeToken(type, attributes.getValue(i), attQname, curNsScope);
+ if (foundId && !setID){
+ attToken.setID(xmlId);
+ setID = true;
+ }
+ if (foundIdREFS){
+ attToken.setIDREFS(xmlIdREFS);
+ foundIdREFS = false;
+ }
+ myToks.add(attToken);
+ } catch (MXQueryException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+// if (level < 2 && uri != null && localName.equals(qname)&& !createdNSScope) {
+// try {
+// //checkOpenNsScopeAddNs(createdNSScope, new QName(Context.NS_XMLNS), uri);
+// //myToks.setElementAt(new ElementToken(Type.START_TAG, token.getNodeId(), qName, curNsScope), 0);
+// } catch (MXQueryException e) {
+// // TODO Auto-generated catch block
+// e.printStackTrace();
+// }
+// }
+
+
+ if (foundId) {
+ NamedToken nmToken = (NamedToken)myToks.elementAt(0);
+ nmToken.setID(xmlId);
+ }
+ for (int i=0;i<myToks.size();i++) {
+ addToken((TokenInterface)myToks.elementAt(i));
+ }
+ } // startElement(String,String,String,Attributes)
+
+
+ /** End element. */
+ public void endElement(String uri, String localName, String qname) throws SAXException {
+ emitPendingTextContent();
+ String prefix = null;
+ String localPart;
+ //String[] qNameParts = qname.split(":");
+ int splitPos = qname.indexOf(':');
+ if (splitPos > 0) {
+ prefix = qname.substring(0,splitPos);
+ localPart = qname.substring(splitPos+1);
+ } else
+ localPart = qname;
+
+ XQName qName = new QName(uri, prefix, localPart);
+ NamedToken token = new ElementToken(Type.END_TAG, null, qName, curNsScope);
+ addToken(token);
+ checkCloseNsScope();
+ level--;
+ } // endElement(String,String,String)
+
+ public void ignorableWhitespace(char[] ch, int start, int length)
+ throws SAXException {
+ characters(ch, start, length);
+ }
+
+ private void emitPendingTextContent() throws SAXException{
+ if (pendingTextContent.length() > 0) {
+ int type = Type.TEXT_NODE_UNTYPED_ATOMIC;
+ try {
+ addToken(new TextToken(type,createNextTokenId(type,null), pendingTextContent.toString(), curNsScope));
+ } catch (MXQueryException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ pendingTextContent = new StringBuffer();
+ }
+
+ }
+ public void characters(char[] ch, int start, int length) throws SAXException {
+
+// for (int i = start; i < start + length; i++) {
+// text.append(ch[i]);
+// }
+ int type = Type.TEXT_NODE_UNTYPED_ATOMIC;
+ if (startCData) {
+ emitPendingTextContent();
+ try {
+ addToken(new TextToken(type,createNextTokenId(type,null), new String(ch,start,length), curNsScope));
+ }catch (MXQueryException e) {
+ throw new SAXException(e.toString());
+ }
+
+ } else {
+ pendingTextContent.append(ch,start,length);
+ }
+ }
+
+ public void error(SAXParseException e) throws SAXException {
+ throw e;
+ }
+
+ public void fatalError(SAXParseException e) throws SAXException {
+ throw e;
+ }
+
+ public void warning(SAXParseException e) throws SAXException {
+ System.out.println("Warning: " + e.getMessage());
+ }
+
+ public void comment(char[] ch, int start, int end) throws SAXException {
+ emitPendingTextContent();
+ if (!inDTD) {
+ // only take comments from the main file, not from the DTD
+ String text = new String(ch, start, end);
+ try {
+ addToken(new CommentToken(createNextTokenId(Type.COMMENT,null), text,curNsScope));
+ } catch (DynamicException e) {
+ throw new SAXException(e);
+ }
+ }
+ }
+
+ public void endCDATA() throws SAXException {
+ startCData = false;
+ }
+
+ public void endDTD() throws SAXException {
+ inDTD = false;
+ }
+
+ public void endEntity(String arg0) throws SAXException {
+ //System.out.println("Entity ended");
+ }
+
+ public void startCDATA() throws SAXException {
+ startCData = true;
+ }
+
+ public void startDTD(String name, String publicID, String systemID) throws SAXException {
+ systemid = systemID;
+ publicid = publicID;
+ dtdRootElem = name;
+ inDTD = true;
+ }
+
+ public void startEntity(String arg0) throws SAXException {
+ //System.out.println("Entitiy started "+arg0);
+ }
+
+ public void attributeDecl(String name, String name2, String type,String mode, String value) throws SAXException {
+ if (type.equals("ID"))idsVector.add(name+"#"+name2);
+ if (type.equals("IDREF")) idRefVector.add(name+"#"+name2);
+ if (type.equals("IDREFS")) idRefsVector.add(name+"#"+name2);
+ }
+
+ public void elementDecl(String name, String model) throws SAXException {
+ //System.out.println("element decl"+model);
+ }
+
+ public void externalEntityDecl(String name, String publicId,
+ String systemId) throws SAXException {
+// System.out.println("external decl");
+
+ }
+
+ public void internalEntityDecl(String name, String value)
+ throws SAXException {
+ // System.out.println("internal decl");
+
+ }
+
+ public void startPrefixMapping(String prefix, String uri)
+ throws SAXException {
+ if (scopeDepth.peek() != level+1) {
+ curNsScope = new XDMScope(curNsScope);
+ scopeDepth.push(level+1);
+ }
+ Namespace nm = new Namespace(prefix, uri);
+ try {
+ curNsScope.addNamespace(nm);
+ } catch (MXQueryException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+
+ public void endPrefixMapping(String prefix) throws SAXException {
+ // TODO Auto-generated method stub
+ if (scopeDepth.peek() == level+1) {
+ curNsScope = curNsScope.getParent();
+ scopeDepth.pop();
+ }
+ }
+ }
+
+
+ class ParseWorker extends Thread {
+ Vector tokens;
+ SaxAdapter adapter;
+
+ public ParseWorker(Vector tokens, SaxAdapter adapter) {
+ super();
+ this.tokens = tokens;
+ this.adapter = adapter;
+ }
+
+ public void run() {
+ try {
+ adapter.parse();
+ }
+ catch (SAXException e) {
+ String message = e.getMessage();
+ parseException = new DynamicException(ErrorCodes.E0027_DYNAMIC_VALIDATE_UNEXPECTED_VALIDITY, message, loc);
+ synchronized(tokens) {
+ tokens.notify();
+ }
+
+ } catch (IOException e) {
+ parseException = new MXQueryException(ErrorCodes.A0007_EC_IO,"I/O Error while parsing: "+e.toString(),loc);
+ synchronized(tokens) {
+ tokens.notify();
+ }
+ } catch (MXQueryException e) {
+ parseException = e;
+ synchronized(tokens) {
+ tokens.notify();
+ }
+ }
+ }
+ }
+
+ public NonSchemaValidatingSaxImportAdapter(Context ctx, QueryLocation loc,InputSource xml, int validationMode, boolean tidyInput) {
+ super(ctx,loc);
+ adapter= new SaxAdapter(null);
+ this.tokens = new Vector();
+ tobeParsed = xml;
+ this.tidyInput = tidyInput;
+ this.validationMode= validationMode;
+ }
+
+ public NonSchemaValidatingSaxImportAdapter(Context ctx, QueryLocation loc,XMLReader source) {
+ super(ctx,loc);
+ adapter= new SaxAdapter(source);
+ this.tokens = new Vector();
+ }
+
+ protected void init() throws MXQueryException {
+ worker = new ParseWorker(tokens, adapter);
+ worker.start();
+ }
+
+ protected XDMIterator copy(Context context, XDMIterator[] subIters, Vector nestedPredCtxStack) throws MXQueryException {
+ XDMImportAdapter res = new NonSchemaValidatingSaxImportAdapter(this.context, loc,tobeParsed, validationMode, false);
+ res.uri = uri;
+ return res;
+ }
+ public TokenInterface next() throws MXQueryException {
+ if (called == 0) {
+ init();
+ called++;
+ getBulkFromQueue();
+ }
+ if (done)
+ return Token.END_SEQUENCE_TOKEN;
+ if (bulkPos == currBulk.length)
+ getBulkFromQueue();
+ TokenInterface tok = currBulk[bulkPos++];
+ if (tok == Token.END_SEQUENCE_TOKEN)
+ done = true;
+ return tok;
+ }
+
+ private void getBulkFromQueue() throws MXQueryException {
+ synchronized(tokens) {
+ while (tokens.isEmpty() && parseException == null) {
+ try {
+ tokens.wait();
+ } catch (InterruptedException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+ if (parseException != null)
+ throw parseException;
+ currBulk = (TokenInterface[])tokens.remove(0);
+ bulkPos = 0;
+ tokens.notify();
+ }
+ }
+
+
+ protected void resetImpl() throws MXQueryException {
+ super.resetImpl();
+ done = false;
+ worker.interrupt();
+ }
+
+ protected void freeResources(boolean restartable)
+ throws MXQueryException {
+ // TODO Auto-generated method stub
+ super.freeResources(restartable);
+ if (worker != null)
+ worker.interrupt();
+ }
+
+
+}
Modified: trunk/MXQuery/src/ch/ethz/mxquery/xdmio/xmlAdapters/XDMImportAdapter.java
===================================================================
--- trunk/MXQuery/src/ch/ethz/mxquery/xdmio/xmlAdapters/XDMImportAdapter.java 2011-05-17 10:42:00 UTC (rev 4352)
+++ trunk/MXQuery/src/ch/ethz/mxquery/xdmio/xmlAdapters/XDMImportAdapter.java 2011-05-18 10:11:44 UTC (rev 4353)
@@ -235,7 +235,7 @@
/** Check if a new XDM scope needs to be opened and opens it if needed*/
private boolean checkOpenXDMScope(boolean createdNSScope) {
- if (!createdNSScope) {
+ if (scopeDepth.peek() != level) {
scopeDepth.push(level);
curNsScope = new XDMScope(curNsScope);
createdNSScope = true;
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|