コード例 #1
0
  public String extractXPathExpression(String xqFile, String inputFile, boolean unescape) {
    // get the xpath2 expr from xq file, first
    char[] cbuf = new char[2048]; //
    String content = null;
    String xpath2Expr = null;

    try {
      URL entryUrl = bundle.getEntry(xqFile);
      InputStream isxq = testResolve(entryUrl);
      if (useNewApi) {
        if (staticContextBuilder.getBaseUri() == null)
          try {
            staticContextBuilder.withBaseUri(entryUrl.toString());
          } catch (URISyntaxException e) {
            throw new RuntimeException(e); // drats
          }
      } else {
        if (dynamicContext.base_uri().getStringValue() == null)
          dynamicContext.set_base_uri(entryUrl.toString());
      }
      BufferedReader xqreader =
          new BufferedReader(new InputStreamReader(isxq, Charset.forName("UTF-8")));
      int nByte = xqreader.read(cbuf);
      assertTrue(xqFile, nByte < 2048);
      content = new String(cbuf).trim();
      //
      if (content.indexOf(INPUT_CONTEXT) != -1
          && content.indexOf(INPUT_CONTEXT1) == -1
          && content.indexOf(INPUT_CONTEXT2) == -1) {
        inputMap.put(INPUT_CONTEXT, inputFile);
      } else if (content.indexOf(INPUT_CONTEXT1) == -1) {
        inputMap.put(INPUT_CONTEXT1, inputFile);
      } else if (content.indexOf(INPUT_CONTEXT2) != -1) {
        inputMap.put(INPUT_CONTEXT2, inputFile);
      }
      //
      if (content.indexOf(DECLARE_NAMESPACE) != -1
          || content.indexOf(IMPORT_SCHEMA_NAMESPACE) != -1) {
        setupNamespace(content);
      }
      //
      assertTrue(content.lastIndexOf(S_COMMENT2) != -1); // assert to get
      xpath2Expr = content.substring(content.lastIndexOf(S_COMMENT2) + 2).trim();
      xqreader.close();
      isxq.close();
    } catch (IOException e) {
      throw new RuntimeException("Can't extract XPath expression from XQuery file : " + xqFile, e);
    }
    if (unescape && xpath2Expr.contains("&")) {
      return resolveCharacterReferences(xpath2Expr);
    } else {
      return xpath2Expr;
    }
  }
コード例 #2
0
 protected void setBaseUri(String uri) throws URISyntaxException {
   if (useNewApi) {
     staticContextBuilder.withBaseUri(uri);
   } else {
     dynamicContext.set_base_uri(uri);
   }
 }
コード例 #3
0
 protected void setDefaultCollation(String uri) {
   if (useNewApi) {
     staticContextBuilder.withDefaultCollation(uri);
   } else {
     dynamicContext.set_default_collation(uri);
   }
 }
コード例 #4
0
 protected void setCollections(Map map) {
   if (useNewApi) {
     dynamicContextBuilder.withCollections(map);
   } else {
     dynamicContext.set_collections(map);
   }
 }
コード例 #5
0
 protected void addFunctionLibrary(FunctionLibrary fl) {
   if (useNewApi) {
     staticContextBuilder.withFunctionLibrary(fl.getNamespace(), fl);
   } else {
     dynamicContext.add_function_library(fl);
   }
 }
コード例 #6
0
  protected DefaultDynamicContext setupDynamicContext(XSModel schema) {
    XercesTypeModel typeModel = schema != null ? new XercesTypeModel(schema) : null;
    if (useNewApi) {
      staticContextBuilder.withTypeModel(typeModel);

      staticContextBuilder.withNamespace("xs", "http://www.w3.org/2001/XMLSchema");
      staticContextBuilder.withNamespace("xsd", "http://www.w3.org/2001/XMLSchema");
      staticContextBuilder.withNamespace("fn", "http://www.w3.org/2005/xpath-functions");
      staticContextBuilder.withNamespace("xml", "http://www.w3.org/XML/1998/namespace");

      dynamicContextBuilder = new DynamicContextBuilder(staticContextBuilder);
      setupVariables(dynamicContext);
      try {
        dynamicContextBuilder.withTimezoneOffset(
            DatatypeFactory.newInstance().newDuration(false /*i.e. negative*/, 0, 0, 0, 5, 0, 0));
      } catch (DatatypeConfigurationException e) {
        throw new RuntimeException("Shouldn't fail here", e);
      }
      return null;
    }

    DefaultDynamicContext dc = new DefaultDynamicContext(typeModel);
    dynamicContext = dc;

    dc.add_namespace("xs", "http://www.w3.org/2001/XMLSchema");
    dc.add_namespace("xsd", "http://www.w3.org/2001/XMLSchema");
    dc.add_namespace("fn", "http://www.w3.org/2005/xpath-functions");
    dc.add_namespace("xml", "http://www.w3.org/XML/1998/namespace");

    dc.add_function_library(new FnFunctionLibrary());
    dc.add_function_library(new XSCtrLibrary());
    setupVariables(dc);
    return dc;
  }
コード例 #7
0
 protected void addNamespace(String prefix, String nsURI) {
   if (useNewApi) {
     if (prefix == null || "".equals(prefix)) {
       staticContextBuilder.withDefaultNamespace(nsURI);
     } else {
       staticContextBuilder.withNamespace(prefix, nsURI);
     }
   } else {
     dynamicContext.add_namespace(prefix, nsURI);
   }
 }
コード例 #8
0
 protected void setVariable(String name, ResultSequence values) {
   if (useNewApi) {
     staticContextBuilder.withVariable(
         new javax.xml.namespace.QName(name),
         new SimpleAtomicItemTypeImpl(
             BuiltinTypeLibrary.XS_ANYATOMICTYPE, ItemType.OCCURRENCE_NONE_OR_MANY));
     dynamicContextBuilder.withVariable(new javax.xml.namespace.QName(name), values);
   } else {
     dynamicContext.set_variable(new QName(name), values);
   }
 }
コード例 #9
0
  void setCollationProvider(final CollationProvider cp) {
    if (useNewApi) {
      staticContextBuilder.withCollationProvider(cp);
    } else {
      dynamicContext.set_collation_provider(
          new org.eclipse.wst.xml.xpath2.processor.CollationProvider() {

            public Comparator get_collation(String name) {
              return cp.getCollation(name);
            }
          });
    }
  }
コード例 #10
0
 protected void setVariable(String name, AnyType value) {
   if (useNewApi) {
     String ns = staticContextBuilder.getDefaultNamespace();
     if (value != null) {
       staticContextBuilder.withVariable(
           new javax.xml.namespace.QName(ns, name),
           new SimpleAtomicItemTypeImpl(value.getTypeDefinition(), ItemType.OCCURRENCE_ONE));
     } else {
       staticContextBuilder.withVariable(
           new javax.xml.namespace.QName(ns, name),
           new SimpleAtomicItemTypeImpl(
               BuiltinTypeLibrary.XS_UNTYPEDATOMIC, ItemType.OCCURRENCE_OPTIONAL));
     }
     dynamicContextBuilder.withVariable(new javax.xml.namespace.QName(ns, name), value);
   } else {
     dynamicContext.set_variable(new QName(name), value);
   }
 }
コード例 #11
0
  protected DynamicContext setupVariables(DynamicContext dc) {
    setVariable("x", (AnyType) null);
    setVariable("var", (AnyType) null);

    if (domDoc != null) {
      TypeModel typeModel =
          dynamicContext != null
              ? dynamicContext.getTypeModel(domDoc)
              : staticContextBuilder.getTypeModel();
      AnyType docType = new DocType(domDoc, typeModel);
      setVariable("input-context1", docType);
      setVariable("input-context", docType);
      if (domDoc2 == null) {
        setVariable("input-context2", docType);
      } else {
        setVariable("input-context2", (AnyType) new DocType(domDoc2, typeModel));
      }
    }
    return dc;
  }