コード例 #1
0
ファイル: RegexpFilter.java プロジェクト: NCIP/cadsr-cgmdr
  /**
   * @param string
   * @param pattern
   * @param flags
   * @return
   * @throws XPathException
   */
  private Sequence match(String string, String pattern, int flags) throws XPathException {
    try {
      Matcher matcher;
      if (cachedRegexp == null
          || (!cachedRegexp.equals(pattern))
          || flags != cachedPattern.flags()) {
        matcher = Pattern.compile(pattern, flags).matcher(string);
        cachedPattern = matcher.pattern();
        cachedRegexp = string;
      } else {
        matcher = cachedPattern.matcher(string);
      }

      if (!matcher.find()) {
        return Sequence.EMPTY_SEQUENCE;
      } else {
        int items = matcher.groupCount() + 1;
        Sequence seq = new ValueSequence();
        seq.add(new StringValue(string));
        for (int i = 1; i < items; i++) {
          String val = matcher.group(i);
          if (val == null) {
            val = "";
          }
          seq.add(new StringValue(val));
        }
        return seq;
      }
    } catch (PatternSyntaxException e) {
      throw new XPathException("Invalid regular expression: " + e.getMessage(), e);
    }
  }
コード例 #2
0
ファイル: RegexpFilter.java プロジェクト: NCIP/cadsr-cgmdr
  public Sequence filter(Sequence[] args) throws org.exist.xquery.XPathException {
    // Check input parameters
    if (args.length != 2) {
      return Sequence.EMPTY_SEQUENCE;
    }

    // Get regular expression
    Pattern pattern = null;
    String regexp = args[1].getStringValue();
    if (cachedRegexp.equals(regexp) && cachedPattern != null) {
      // Cached compiled pattern is available!
      pattern = cachedPattern;

    } else {
      // Compile new pattern
      pattern = Pattern.compile(regexp);

      // Cache new pattern
      cachedPattern = pattern;
      cachedRegexp = regexp;
    }

    // Match pattern on string
    Matcher matcher = pattern.matcher(args[0].getStringValue());

    // Create response
    Sequence result = new ValueSequence();

    // Add each match to response sequence
    while (matcher.find()) {
      result.add(new StringValue(matcher.group()));
    }

    return result;
  }
コード例 #3
0
  @Override
  public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {

    Sequence result = new ValueSequence();

    // Check input parameters
    if (args.length == 0) {
      String uuid = UUIDGenerator.getUUIDversion4();
      result.add(new StringValue(uuid));

    } else if (args.length == 1) {
      String parameter = args[0].getStringValue();
      String uuid = UUIDGenerator.getUUIDversion3(parameter);
      result.add(new StringValue(uuid));

    } else {
      logger.error("Not a supported number of parameters");
      throw new XPathException("Not a supported number of parameters");
    }

    return result;
  }
コード例 #4
0
  /** @see BasicFunction#eval(Sequence[], Sequence) */
  public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {

    // Check input parameters
    if (args.length != 1 && args.length != 2) {
      return Sequence.EMPTY_SEQUENCE;
    }

    InputStream is = null;
    ValidationReport vr = null;

    try {
      // Get inputstream of XML instance document
      if (args[0].getItemType() == Type.ANY_URI || args[0].getItemType() == Type.STRING) {
        // anyURI provided
        String url = args[0].getStringValue();

        // Fix URL
        if (url.startsWith("/")) {
          url = "xmldb:exist://" + url;
        }

        is = new URL(url).openStream();

      } else if (args[0].getItemType() == Type.ELEMENT || args[0].getItemType() == Type.DOCUMENT) {
        // Node provided
        is = new NodeInputStream(context, args[0].iterate()); // new NodeInputStream()

      } else {
        LOG.error("Wrong item type " + Type.getTypeName(args[0].getItemType()));
        throw new XPathException(
            getASTNode(), "wrong item type " + Type.getTypeName(args[0].getItemType()));
      }

      // Perform validation
      if (args.length == 1) {
        // Validate using system catalog
        vr = validator.validate(is);

      } else {
        // Validate using resource speciefied in second parameter
        String url = args[1].getStringValue();

        if (url.startsWith("/")) {
          url = "xmldb:exist://" + url;
        }

        vr = validator.validate(is, url);
      }

    } catch (MalformedURLException ex) {
      LOG.error(ex);
      throw new XPathException(getASTNode(), "Invalid resource URI", ex);

    } catch (ExistIOException ex) {
      LOG.error(ex.getCause());
      throw new XPathException(getASTNode(), "eXistIOexception", ex.getCause());

    } catch (Exception ex) {
      LOG.error(ex);
      throw new XPathException(getASTNode(), "exception", ex);

    } finally {
      // Force release stream
      try {
        is.close();
      } catch (IOException ex) {
        LOG.debug("Attemted to close stream. ignore.", ex);
      }
    }

    // Create response
    if (isCalledAs("validate")) {
      Sequence result = new ValueSequence();
      result.add(new BooleanValue(vr.isValid()));
      return result;

    } else if (isCalledAs("validate-report")) {
      MemTreeBuilder builder = context.getDocumentBuilder();
      NodeImpl result = writeReport(vr, builder);
      return result;
    }

    // Oops
    LOG.error("invoked with wrong function name");
    throw new XPathException("unknown function");
  }
コード例 #5
0
  /**
   * @throws org.exist.xquery.XPathException
   * @see BasicFunction#eval(Sequence[], Sequence)
   */
  public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {

    // Check input parameters
    if (args.length != 2) {
      return Sequence.EMPTY_SEQUENCE;
    }

    ValidationReport report = new ValidationReport();
    InputSource instance = null;
    InputSource grammar = null;

    try {
      report.start();

      // Get inputstream of XML instance document
      instance = Shared.getInputSource(args[0].itemAt(0), context);

      // Validate using resource specified in second parameter
      grammar = Shared.getInputSource(args[1].itemAt(0), context);

      // Special setup for compact notation
      String grammarUrl = grammar.getSystemId();
      SchemaReader schemaReader =
          ((grammarUrl != null) && (grammarUrl.endsWith(".rnc")))
              ? CompactSchemaReader.getInstance()
              : null;

      // Setup validation properties. see Jing interface
      PropertyMapBuilder properties = new PropertyMapBuilder();
      ValidateProperty.ERROR_HANDLER.put(properties, report);

      // Register resolver for xmldb:exist:/// embedded URLs
      ExistResolver resolver = new ExistResolver(brokerPool);
      ValidateProperty.URI_RESOLVER.put(properties, resolver);
      ValidateProperty.ENTITY_RESOLVER.put(properties, resolver);

      // Setup driver
      ValidationDriver driver = new ValidationDriver(properties.toPropertyMap(), schemaReader);

      // Load schema
      driver.loadSchema(grammar);

      // Validate XML instance
      driver.validate(instance);

    } catch (MalformedURLException ex) {
      LOG.error(ex.getMessage());
      report.setException(ex);

    } catch (ExistIOException ex) {
      LOG.error(ex.getCause());
      report.setException(ex);

    } catch (Throwable ex) {
      LOG.error(ex);
      report.setException(ex);

    } finally {
      Shared.closeInputSource(instance);
      Shared.closeInputSource(grammar);
      report.stop();
    }

    // Create response
    if (isCalledAs("jing")) {
      Sequence result = new ValueSequence();
      result.add(new BooleanValue(report.isValid()));
      return result;

    } else /* isCalledAs("jing-report") */ {
      MemTreeBuilder builder = context.getDocumentBuilder();
      NodeImpl result = Shared.writeReport(report, builder);
      return result;
    }
  }
コード例 #6
0
  /**
   * @throws org.exist.xquery.XPathException
   * @see BasicFunction#eval(Sequence[], Sequence)
   */
  public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {

    // Check input parameters
    if (args.length != 2) {
      return Sequence.EMPTY_SEQUENCE;
    }

    ValidationReport report = new ValidationReport();
    StreamSource instance = null;
    StreamSource grammars[] = null;

    try {
      report.start();

      // Get inputstream for instance document
      instance = Shared.getStreamSource(args[0].itemAt(0), context);

      // Validate using resource speciefied in second parameter
      grammars = Shared.getStreamSource(args[1], context);

      for (StreamSource grammar : grammars) {
        String grammarUrl = grammar.getSystemId();
        if (grammarUrl != null && !grammarUrl.endsWith(".xsd")) {
          throw new XPathException("Only XML schemas (.xsd) are supported.");
        }
      }

      // Prepare
      String schemaLang = XMLConstants.W3C_XML_SCHEMA_NS_URI;
      SchemaFactory factory = SchemaFactory.newInstance(schemaLang);

      // Create grammar
      Schema schema = factory.newSchema(grammars);

      // Setup validator
      Validator validator = schema.newValidator();
      validator.setErrorHandler(report);

      // Perform validation
      validator.validate(instance);

    } catch (MalformedURLException ex) {
      LOG.error(ex.getMessage());
      report.setException(ex);

    } catch (ExistIOException ex) {
      LOG.error(ex.getCause());
      report.setException(ex);

    } catch (Throwable ex) {
      LOG.error(ex);
      report.setException(ex);

    } finally {
      report.stop();

      Shared.closeStreamSource(instance);
      Shared.closeStreamSources(grammars);
    }

    // Create response
    if (isCalledAs("jaxv")) {
      Sequence result = new ValueSequence();
      result.add(new BooleanValue(report.isValid()));
      return result;

    } else /* isCalledAs("jaxv-report") */ {
      MemTreeBuilder builder = context.getDocumentBuilder();
      NodeImpl result = Shared.writeReport(report, builder);
      return result;
    }
  }