Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ADD] XQuery, validate:...-report. Closes #1177
- Loading branch information
1 parent
7b21a63
commit 1eb2f8f
Showing
15 changed files
with
513 additions
and
263 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
basex-core/src/main/java/org/basex/query/func/validate/ErrorInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package org.basex.query.func.validate; | ||
|
||
import org.basex.io.*; | ||
import org.xml.sax.*; | ||
|
||
/** | ||
* Error info. | ||
* | ||
* @author BaseX Team 2005-15, BSD License | ||
* @author Christian Gruen | ||
*/ | ||
final class ErrorInfo { | ||
/** Message. */ | ||
final String message; | ||
/** Level. */ | ||
final String level; | ||
/** URL. */ | ||
String url; | ||
/** Line number. */ | ||
int line = Integer.MIN_VALUE; | ||
/** Column number. */ | ||
int column = Integer.MIN_VALUE; | ||
|
||
/** | ||
* Constructor. | ||
* @param ex exception | ||
* @param level type | ||
* @param schema schema url | ||
*/ | ||
ErrorInfo(final SAXException ex, final String level, final IO schema) { | ||
this.level = level; | ||
|
||
String m = ex.getMessage(); | ||
Throwable e = ex; | ||
if(m.contains("Exception:")) { | ||
// may be recursively called if external validator (e.g. Saxon) is used | ||
while(e.getCause() != null) e = e.getCause(); | ||
if(e instanceof SAXException) m = e.getLocalizedMessage(); | ||
} | ||
|
||
if(ex instanceof SAXParseException) { | ||
final SAXParseException se = (SAXParseException) ex; | ||
final String id = se.getSystemId(); | ||
if(id != null) { | ||
final IO io = IO.get(id); | ||
if(schema == null || !io.isDir() && !schema.equals(id)) url = id; | ||
} | ||
line = se.getLineNumber(); | ||
column = se.getColumnNumber(); | ||
} | ||
message = m; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
final StringBuilder sb = new StringBuilder(); | ||
if(url != null) sb.append(url).append(", "); | ||
if(line != Integer.MIN_VALUE) sb.append(line).append(':').append(column).append(": "); | ||
return sb.append(message).toString(); | ||
} | ||
} |
37 changes: 36 additions & 1 deletion
37
basex-core/src/main/java/org/basex/query/func/validate/ValidateDtd.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,52 @@ | ||
package org.basex.query.func.validate; | ||
|
||
import java.io.*; | ||
import java.util.*; | ||
|
||
import javax.xml.parsers.*; | ||
|
||
import org.basex.io.*; | ||
import org.basex.io.serial.*; | ||
import org.basex.query.*; | ||
import org.basex.query.value.*; | ||
import org.basex.query.value.item.*; | ||
import org.xml.sax.*; | ||
|
||
/** | ||
* Function implementation. | ||
* | ||
* @author BaseX Team 2005-15, BSD License | ||
* @author Christian Gruen | ||
*/ | ||
public final class ValidateDtd extends ValidateDtdInfo { | ||
public class ValidateDtd extends ValidateFn { | ||
@Override | ||
public Value value(final QueryContext qc) throws QueryException { | ||
return check(qc); | ||
} | ||
|
||
@Override | ||
public ArrayList<ErrorInfo> errors(final QueryContext qc) throws QueryException { | ||
checkCreate(qc); | ||
return process(new Validation() { | ||
@Override | ||
void process(final ErrorHandler handler) | ||
throws IOException, ParserConfigurationException, SAXException, QueryException { | ||
|
||
final Item it = toNodeOrAtomItem(exprs[0], qc); | ||
SerializerOptions sp = null; | ||
|
||
// integrate doctype declaration via serialization parameters | ||
if(exprs.length > 1) { | ||
sp = new SerializerOptions(); | ||
final IO schema = prepare(checkPath(exprs[1], qc), handler); | ||
sp.set(SerializerOptions.DOCTYPE_SYSTEM, schema.url()); | ||
} | ||
|
||
final IO in = read(it, qc, sp); | ||
final SAXParserFactory sf = SAXParserFactory.newInstance(); | ||
sf.setValidating(true); | ||
sf.newSAXParser().parse(in.inputSource(), handler); | ||
} | ||
}); | ||
} | ||
} |
56 changes: 2 additions & 54 deletions
56
basex-core/src/main/java/org/basex/query/func/validate/ValidateDtdInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,17 @@ | ||
package org.basex.query.func.validate; | ||
|
||
import java.io.*; | ||
|
||
import javax.xml.parsers.*; | ||
|
||
import org.basex.io.*; | ||
import org.basex.io.serial.*; | ||
import org.basex.query.*; | ||
import org.basex.query.value.*; | ||
import org.basex.query.value.item.*; | ||
import org.xml.sax.*; | ||
|
||
/** | ||
* Validates a document against a DTD. | ||
* The following two variants exist: | ||
* | ||
* <ul>{@code validate:dtd($doc)} | ||
* <li>Looks for the document type declaration in {@code $doc} and | ||
* uses it for validation.</li> | ||
* <li>{@code $doc} must contain a DTD for this to work.</li> | ||
* <li>{@code $doc} is allowed to be either a {@code XML node} or a {@code | ||
* xs:string} pointing to an URL or a local file that will then be parsed | ||
* and validated.</li> | ||
* </ul> | ||
* <ul>{@code validate:dtd($doc, $dtd)} | ||
* <li>{@code $doc} is allowed to be either a {@code XML node} or a {@code | ||
* xs:string} pointing to an URL or a local file</li> | ||
* <li>{@code $dtd as xs:string} is expected to point to an URL or a local | ||
* file containing the document type definitions. </li> | ||
* </ul> | ||
* Function implementation. | ||
* | ||
* @author BaseX Team 2005-15, BSD License | ||
* @author Christian Gruen | ||
*/ | ||
public class ValidateDtdInfo extends ValidateFn { | ||
public final class ValidateDtdInfo extends ValidateDtd { | ||
@Override | ||
public Value value(final QueryContext qc) throws QueryException { | ||
return info(qc); | ||
} | ||
|
||
@Override | ||
public Value info(final QueryContext qc) throws QueryException { | ||
checkCreate(qc); | ||
return process(new Validation() { | ||
@Override | ||
void process(final ErrorHandler handler) | ||
throws IOException, ParserConfigurationException, SAXException, QueryException { | ||
|
||
final Item it = toNodeOrAtomItem(exprs[0], qc); | ||
SerializerOptions sp = null; | ||
|
||
// integrate doctype declaration via serialization parameters | ||
if(exprs.length > 1) { | ||
sp = new SerializerOptions(); | ||
IO dtd = checkPath(exprs[1], qc); | ||
tmp = createTmp(dtd); | ||
if(tmp != null) dtd = tmp; | ||
sp.set(SerializerOptions.DOCTYPE_SYSTEM, dtd.url()); | ||
} | ||
|
||
final IO in = read(it, qc, sp); | ||
final SAXParserFactory sf = SAXParserFactory.newInstance(); | ||
sf.setValidating(true); | ||
sf.newSAXParser().parse(in.inputSource(), handler); | ||
} | ||
}); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
basex-core/src/main/java/org/basex/query/func/validate/ValidateDtdReport.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package org.basex.query.func.validate; | ||
|
||
import org.basex.query.*; | ||
import org.basex.query.value.*; | ||
|
||
/** | ||
* Function implementation. | ||
* | ||
* @author BaseX Team 2005-15, BSD License | ||
* @author Christian Gruen | ||
*/ | ||
public final class ValidateDtdReport extends ValidateDtd { | ||
@Override | ||
public Value value(final QueryContext qc) throws QueryException { | ||
return report(qc); | ||
} | ||
} |
Oops, something went wrong.