Ignore:
Timestamp:
06/26/14 22:58:39 (10 years ago)
Author:
Oliver Schonefeld
Message:
  • increased raw parsing performance by making Saxon use the Xerces-J SAX parser
  • use XQuery for Schematron report processing
  • report line and column numbers for Schematron validation
File:
1 edited

Legend:

Unmodified
Added
Removed
  • CMDIValidator/trunk/cmdi-validator-core/src/main/java/eu/clarin/cmdi/validator/extensions/CheckHandlesExtension.java

    r5387 r5394  
    2222
    2323import org.apache.http.HttpStatus;
    24 import org.slf4j.Logger;
    25 import org.slf4j.LoggerFactory;
    26 
    2724import net.sf.saxon.s9api.SaxonApiException;
    2825import net.sf.saxon.s9api.XPathCompiler;
     
    3633import eu.clarin.cmdi.validator.CMDIValidatorWriteableResult;
    3734import eu.clarin.cmdi.validator.utils.HandleResolver;
     35import eu.clarin.cmdi.validator.utils.SaxonLocationUtils;
    3836
    3937public class CheckHandlesExtension extends CMDIValidatorExtension {
    40     private static final String XPATH = "//*:ResourceProxy[*:ResourceType/text() = 'Resource' or *:ResourceType/text() = 'Metadata']/*:ResourceRef/text()";
     38    private static final String XPATH = "//*:ResourceProxy[*:ResourceType/text() = 'Resource' or *:ResourceType/text() = 'Metadata']/*:ResourceRef";
    4139    private static final String HDL_SCHEME = "hdl";
    4240    private static final String HDL_PROXY_HTTP = "http";
     
    4442    private static final String HDL_PROXY_HOST = "hdl.handle.net";
    4543    private static final String URN_SCHEME = "urn";
    46     @SuppressWarnings("unused")
    47     private static final Logger logger =
    48             LoggerFactory.getLogger(CheckHandlesExtension.class);
    4944    private final boolean resolveHandles;
    5045    private HandleResolver resolver = null;
     
    9186            for (XdmItem item : selector) {
    9287                String handle = null;
     88                final int line   = SaxonLocationUtils.getLineNumber(item);
     89                final int column = SaxonLocationUtils.getColumnNumber(item);
    9390                final String h = item.getStringValue();
    9491                if (h != null) {
     
    9895                    } else {
    9996                        if (!handle.equals(h)) {
    100                             result.reportWarning(-1, -1, "handle '" + h +
     97                            result.reportWarning(line, column, "handle '" + h +
    10198                                    "' contains leading or tailing spaces " +
    10299                                    "within <ResourceRef> element");
     
    106103
    107104                if (handle != null) {
    108                     checkHandleURISyntax(handle, result);
    109                 } else {
    110                     result.reportError(-1, -1,
     105                    checkHandleURISyntax(handle, result, line, column);
     106                } else {
     107                    result.reportError(line, column,
    111108                            "invalid handle (<ResourceRef> was empty)");
    112109                }
     
    119116
    120117    private void checkHandleURISyntax(String handle,
    121             CMDIValidatorWriteableResult result) throws CMDIValidatorException {
     118            CMDIValidatorWriteableResult result, int line, int column)
     119            throws CMDIValidatorException {
    122120        try {
    123121            final URI uri = new URI(handle);
     
    130128                    final URI actionableURI =
    131129                            new URI(HDL_PROXY_HTTP, HDL_PROXY_HOST, path, null);
    132                     checkHandleResolves(actionableURI, result);
     130                    checkHandleResolves(actionableURI, result, line, column);
    133131                } catch (URISyntaxException e) {
    134132                    /* should not happen */
     
    138136            } else if (URN_SCHEME.equals(uri.getScheme())) {
    139137                if (resolveHandles) {
    140                     result.reportInfo(-1, -1, "PID '" + handle +
     138                    result.reportInfo(line, column, "PID '" + handle +
    141139                            "' skipped, because URN resolving is not supported");
    142140                } else {
    143                     result.reportInfo(-1, -1, "PID '" + handle +
     141                    result.reportInfo(line, column, "PID '" + handle +
    144142                            "' skipped, because URN sytax checking is not supported");
    145143                }
     
    148146                if (uri.getHost() != null) {
    149147                    if (!HDL_PROXY_HOST.equalsIgnoreCase(uri.getHost())) {
    150                         result.reportError(-1, -1,
     148                        result.reportError(line, column,
    151149                                "The URI of PID '" + handle +
    152150                                "' contains an unexpected host part of '" +
    153151                                uri.getHost() + "'");
    154152                    }
    155                     checkHandleResolves(uri, result);
    156                 } else {
    157                     result.reportError(-1, -1, "The URI of PID '" + handle +
    158                             "' is missing the host part");
     153                    checkHandleResolves(uri, result, line, column);
     154                } else {
     155                    result.reportError(line, column, "The URI of PID '" +
     156                            handle + "' is missing the host part");
    159157                }
    160158            } else {
    161159                if (uri.getScheme() != null) {
    162                     result.reportError(-1, -1,
     160                    result.reportError(line, column,
    163161                            "The URI of PID '" + handle +
    164162                            "' contains an unexpected schema part of '" +
    165163                            uri.getScheme() + "'");
    166164                } else {
    167                     result.reportError(-1, -1, "The URI of PID '" + handle +
    168                             "' is missing a proper schema part");
     165                    result.reportError(line, column, "The URI of PID '" +
     166                            handle + "' is missing a proper schema part");
    169167                }
    170168            }
    171169        } catch (URISyntaxException e) {
    172             result.reportError(-1, -1, "PID '" + handle +
     170            result.reportError(line, column, "PID '" + handle +
    173171                    "' is not a well-formed URI: " + e.getMessage());
    174172        }
     
    177175
    178176    private void checkHandleResolves(URI uri,
    179             CMDIValidatorWriteableResult result) throws CMDIValidatorException {
     177            CMDIValidatorWriteableResult result, int line, int column)
     178            throws CMDIValidatorException {
    180179        if (resolver != null) {
    181180            try {
     
    188187                    /* FALL-THROUGH */
    189188                case HttpStatus.SC_FORBIDDEN:
    190                     result.reportInfo(-1, -1, "PID '" + uri +
     189                    result.reportInfo(line, column, "PID '" + uri +
    191190                            "' resolved to an access protected resource (" +
    192191                            code + ")");
    193192                    break;
    194193                case HttpStatus.SC_NOT_FOUND:
    195                     result.reportError(-1, -1, "PID '" + uri +
    196                             "' resolved to an non-existing resource (" + code +
    197                             ")");
     194                    result.reportError(line, column, "PID '" + uri +
     195                            "' resolved to an non-existing resource (" +
     196                            code + ")");
    198197                    break;
    199198                case HandleResolver.TIMEOUT:
    200                     result.reportWarning(-1, -1, "Timeout while resolving PID '" +
    201                             uri + "'");
     199                    result.reportWarning(line, column,
     200                            "Timeout while resolving PID '" + uri + "'");
    202201                    break;
    203202                case HandleResolver.UNKNOWN_HOST:
    204                     result.reportWarning(-1, -1, "Unable to resolve host '" +
    205                             uri.getHost() + "' while resolving PID '" +
    206                             uri + "'");
     203                    result.reportWarning(line, column,
     204                            "Unable to resolve host '" + uri.getHost() +
     205                            "' while resolving PID '" + uri + "'");
    207206                    break;
    208207                case HandleResolver.ERROR:
    209                     result.reportWarning(-1, -1,
     208                    result.reportWarning(line, column,
    210209                            "An error occurred while resolving PID '" +
    211210                            uri + "'");
    212211                    break;
    213212                default:
    214                     result.reportWarning(-1, -1, "PID '" + uri +
    215                             "' resolved with an unexpected result (" + code +
    216                             ")");
     213                    result.reportWarning(-line, column, "PID '" + uri +
     214                            "' resolved with an unexpected result (" +
     215                            code + ")");
    217216                    break;
    218217                } // switch
Note: See TracChangeset for help on using the changeset viewer.