Skip to content

Instantly share code, notes, and snippets.

@dustinschultz
Created June 5, 2013 23:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dustinschultz/5718281 to your computer and use it in GitHub Desktop.
Save dustinschultz/5718281 to your computer and use it in GitHub Desktop.
Comments after XML declaration cause XQQueryException
/**
* Copyright (C) [2013] [The FURTHeR Project]
*
* 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 edu.utah.further.core.qunit.xquery;
import java.io.IOException;
import javax.xml.namespace.QName;
import javax.xml.xquery.XQConnection;
import javax.xml.xquery.XQDataSource;
import javax.xml.xquery.XQException;
import javax.xml.xquery.XQPreparedExpression;
import javax.xml.xquery.XQResultSequence;
import net.xqj.basex.BaseXXQDataSource;
import org.basex.BaseXServer;
/**
* Comments after XML declaration cause XQQueryException
* <p>
* -----------------------------------------------------------------------------------<br>
* (c) 2008-2012 FURTHeR Project, Health Sciences IT, University of Utah<br>
* Contact: {@code <further@utah.edu>}<br>
* Biomedical Informatics, 26 South 2000 East<br>
* Room 5775 HSEB, Salt Lake City, UT 84112<br>
* Day Phone: 1-801-581-4080<br>
* -----------------------------------------------------------------------------------
*
* @author N. Dustin Schultz {@code <dustin.schultz@utah.edu>}
* @version Jun 5, 2013
*/
public class Bug
{
public static void main(final String[] args) throws IOException
{
final Thread t = new Thread(new BaseXServer());
t.start();
boolean passed = false;
final String okXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><element></element>";
passed = runXQuery(okXml);
System.out.println("Ok XML passed: " + passed);
final String problemXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><!-- License Comment --><element></element>";
passed = runXQuery(problemXml);
System.out.println("Problem XML passed: " + passed);
}
/**
* @throws XQException
*/
private static boolean runXQuery(final String xml)
{
XQConnection conn = null;
try
{
final XQDataSource xqs = new BaseXXQDataSource();
xqs.setProperty("serverName", "localhost");
xqs.setProperty("port", "1984");
xqs.setProperty("user", "admin");
xqs.setProperty("password", "admin");
conn = xqs.getConnection();
final XQPreparedExpression xqpe = conn
.prepareExpression("declare variable $x as document-node() external; $x");
xqpe.bindDocument(new QName("x"), xml, null, null);
final XQResultSequence rs = xqpe.executeQuery();
while (rs.next())
System.out.println(rs.getItemAsString(null));
conn.close();
return true;
}
catch (final XQException e)
{
if (conn != null)
{
try
{
conn.close();
}
catch (final XQException e1)
{
// ignore
}
}
e.printStackTrace();
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment