}
return res;
}
public NodeImpl loadVarFromURI(XQueryContext context, String uri) throws IOException {
SAXAdapter adapter = new SAXAdapter(context);
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setNamespaceAware(true);
XMLReader xr;
try {
SAXParser parser = factory.newSAXParser();
xr = parser.getXMLReader();
xr.setContentHandler(adapter);
xr.setProperty(Namespaces.SAX_LEXICAL_HANDLER, adapter);
} catch (Exception e) {
throw new IOException(e);
}
try {
// URL url = new URL(uri);
// InputStreamReader isr = new InputStreamReader(url.openStream(), "UTF-8");
InputStreamReader isr = new InputStreamReader(new FileInputStream(uri), "UTF-8");
InputSource src = new InputSource(isr);
xr.parse(src);
isr.close();
adapter.getDocument().setDocumentURI(new File(uri).getAbsoluteFile().toString());
return (NodeImpl) adapter.getDocument();
} catch (SAXException e) {
//workaround BOM
if (e.getMessage().equals("Content is not allowed in prolog.")) {
try {
String xml = readFileAsString(new File(uri));
xml = xml.trim().replaceFirst("^([\\W]+)<","<");
InputSource src = new InputSource(new StringReader(xml));
xr.parse(src);
adapter.getDocument().setDocumentURI(new File(uri).getAbsoluteFile().toString());
return (NodeImpl) adapter.getDocument();
} catch (SAXException e1) {
throw new IOException(e);
}
}
throw new IOException(e);