use of nu.validator.xml.TypedInputSource in project validator by validator.
the class ParseTreePrinter method service.
public void service() throws IOException {
request.setCharacterEncoding("utf-8");
String content = null;
String document = scrubUrl(request.getParameter("doc"));
document = ("".equals(document)) ? null : document;
try (Writer writer = new OutputStreamWriter(response.getOutputStream(), "UTF-8")) {
if (document == null && methodIsGet() && (content = request.getParameter("content")) == null) {
response.setContentType("text/html; charset=utf-8");
writer.write(FORM_HTML);
writer.flush();
return;
}
response.setContentType("text/plain; charset=utf-8");
try {
PrudentHttpEntityResolver entityResolver = new PrudentHttpEntityResolver(2048 * 1024, false, null);
entityResolver.setAllowGenericXml(false);
entityResolver.setAcceptAllKnownXmlTypes(false);
entityResolver.setAllowHtml(true);
entityResolver.setAllowXhtml(true);
TypedInputSource documentInput;
if (methodIsGet()) {
if (content == null) {
documentInput = (TypedInputSource) entityResolver.resolveEntity(null, document);
} else {
documentInput = new TypedInputSource(new StringReader(content));
if ("xml".equals(request.getParameter("parser"))) {
documentInput.setType("application/xhtml+xml");
} else {
documentInput.setType("text/html");
}
}
} else {
// POST
String postContentType = request.getContentType();
if (postContentType == null) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Content-Type missing");
return;
} else if (postContentType.trim().toLowerCase().startsWith("application/x-www-form-urlencoded")) {
response.sendError(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE, "application/x-www-form-urlencoded not supported. Please use multipart/form-data.");
return;
}
long len = request.getContentLength();
if (len > SIZE_LIMIT) {
throw new StreamBoundException("Resource size exceeds limit.");
}
ContentTypeParser contentTypeParser = new ContentTypeParser(null, false);
contentTypeParser.setAllowGenericXml(false);
contentTypeParser.setAcceptAllKnownXmlTypes(false);
contentTypeParser.setAllowHtml(true);
contentTypeParser.setAllowXhtml(true);
documentInput = contentTypeParser.buildTypedInputSource(document, null, postContentType);
documentInput.setByteStream(len < 0 ? new BoundedInputStream(request.getInputStream(), SIZE_LIMIT, document) : request.getInputStream());
documentInput.setSystemId(request.getHeader("Content-Location"));
}
String type = documentInput.getType();
XMLReader parser;
if ("text/html".equals(type) || "text/html-sandboxed".equals(type)) {
writer.write("HTML parser\n\n#document\n");
parser = new nu.validator.htmlparser.sax.HtmlParser();
parser.setProperty("http://validator.nu/properties/heuristics", Heuristics.ALL);
parser.setProperty("http://validator.nu/properties/xml-policy", XmlViolationPolicy.ALLOW);
} else if ("application/xhtml+xml".equals(type)) {
writer.write("XML parser\n\n#document\n");
parser = new SAXDriver();
parser.setFeature("http://xml.org/sax/features/external-general-entities", false);
parser.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
parser.setEntityResolver(new NullEntityResolver());
} else {
writer.write("Unsupported content type.\n");
writer.flush();
return;
}
TreeDumpContentHandler treeDumpContentHandler = new TreeDumpContentHandler(writer, false);
ListErrorHandler listErrorHandler = new ListErrorHandler();
parser.setContentHandler(treeDumpContentHandler);
parser.setProperty("http://xml.org/sax/properties/lexical-handler", treeDumpContentHandler);
parser.setErrorHandler(listErrorHandler);
parser.parse(documentInput);
writer.write("#errors\n");
for (String err : listErrorHandler.getErrors()) {
writer.write(err);
writer.write('\n');
}
} catch (SAXException e) {
writer.write("SAXException:\n");
writer.write(e.getMessage());
writer.write("\n");
} catch (IOException e) {
writer.write("IOException:\n");
writer.write(e.getMessage());
writer.write("\n");
} finally {
writer.flush();
}
}
}
use of nu.validator.xml.TypedInputSource in project validator by validator.
the class VerifierServletTransaction method schemaByUrl.
/**
* @param url
* @return
* @throws SAXException
* @throws IOException
* @throws IncorrectSchemaException
*/
private static Schema schemaByUrl(String url, EntityResolver resolver, PropertyMap pMap) throws SAXException, IOException, IncorrectSchemaException {
log4j.debug("Will load schema: " + url);
TypedInputSource schemaInput;
try {
schemaInput = (TypedInputSource) resolver.resolveEntity(null, url);
} catch (ClassCastException e) {
log4j.fatal(url, e);
throw e;
}
SchemaReader sr = null;
if ("application/relax-ng-compact-syntax".equals(schemaInput.getType())) {
sr = CompactSchemaReader.getInstance();
} else {
sr = new AutoSchemaReader();
}
Schema sch = sr.createSchema(schemaInput, pMap);
return sch;
}
use of nu.validator.xml.TypedInputSource in project validator by validator.
the class VerifierServletTransaction method resolveSchema.
@Override
public Schema resolveSchema(String url, PropertyMap options) throws SAXException, IOException, IncorrectSchemaException {
int i = Arrays.binarySearch(preloadedSchemaUrls, url);
if (i > -1) {
Schema rv = preloadedSchemas[i];
if (options.contains(WrapProperty.ATTRIBUTE_OWNER)) {
if (rv instanceof CheckerSchema) {
errorHandler.error(new SAXParseException("A non-schema checker cannot be used as an attribute schema.", null, url, -1, -1));
throw new IncorrectSchemaException();
} else {
// ugly fall through
}
} else {
return rv;
}
}
externalSchema = true;
TypedInputSource schemaInput = (TypedInputSource) entityResolver.resolveEntity(null, url);
SchemaReader sr = null;
if ("application/relax-ng-compact-syntax".equals(schemaInput.getType())) {
sr = CompactSchemaReader.getInstance();
} else {
sr = new AutoSchemaReader();
}
Schema sch = sr.createSchema(schemaInput, options);
if (Statistics.STATISTICS != null && "com.thaiopensource.validate.schematron.SchemaImpl".equals(sch.getClass().getName())) {
externalSchematron = true;
}
return sch;
}
use of nu.validator.xml.TypedInputSource in project validator by validator.
the class SimpleDocumentValidator method schemaByUrl.
private Schema schemaByUrl(String schemaUrl, ErrorHandler errorHandler) throws Exception, SchemaReadException {
PropertyMapBuilder pmb = new PropertyMapBuilder();
pmb.put(ValidateProperty.ERROR_HANDLER, errorHandler);
pmb.put(ValidateProperty.ENTITY_RESOLVER, entityResolver);
pmb.put(ValidateProperty.XML_READER_CREATOR, new Jaxp11XMLReaderCreator());
RngProperty.CHECK_ID_IDREF.add(pmb);
PropertyMap jingPropertyMap = pmb.toPropertyMap();
try {
TypedInputSource schemaInput = (TypedInputSource) entityResolver.resolveEntity(null, schemaUrl);
SchemaReader sr;
if ("application/relax-ng-compact-syntax".equals(schemaInput.getType())) {
sr = CompactSchemaReader.getInstance();
} else {
sr = new AutoSchemaReader();
}
return sr.createSchema(schemaInput, jingPropertyMap);
} catch (ClassCastException e) {
throw new SchemaReadException(String.format("Failed to resolve schema URL \"%s\".", schemaUrl));
}
}
use of nu.validator.xml.TypedInputSource in project validator by validator.
the class LocalCacheEntityResolver method resolveEntity.
/**
* @see org.xml.sax.EntityResolver#resolveEntity(java.lang.String,
* java.lang.String)
*/
@Override
public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
String path = PATH_MAP.get(systemId);
if (path != null) {
@SuppressWarnings("resource") InputStream stream = LOADER.getResourceAsStream(path);
if (stream != null) {
TypedInputSource is = new TypedInputSource();
if (systemId.endsWith(".rnc")) {
is.setType("application/relax-ng-compact-syntax");
if (!allowRnc) {
stream.close();
throw new IOException("Not an XML resource: " + systemId);
}
} else if (systemId.endsWith(".dtd")) {
is.setType("application/xml-dtd");
} else if (systemId.endsWith(".ent")) {
is.setType("application/xml-external-parsed-entity");
} else {
is.setType("application/xml");
}
is.setByteStream(stream);
is.setSystemId(systemId);
is.setPublicId(publicId);
return is;
}
}
return delegate.resolveEntity(publicId, systemId);
}
Aggregations