Beispiel #1
0
 @Override
 public String convert(NodeInfo source) {
   switch (source.getNodeKind()) {
     case Node.DOCUMENT_NODE:
       return "";
     case Node.TEXT_NODE:
     case Node.CDATA_SECTION_NODE:
       return "text()";
     case Node.COMMENT_NODE:
       return "comment()";
     case Node.PROCESSING_INSTRUCTION_NODE:
       return "processing-instruction('" + source.getDisplayName() + "')";
     case Node.ELEMENT_NODE:
       String prefix = nsContext.getPrefix(source.getURI());
       String name = source.getLocalPart();
       return StringUtil.isEmpty(prefix) ? name : prefix + ':' + name;
     case Node.ATTRIBUTE_NODE:
       if (Namespaces.URI_XMLNS.equals(source.getURI()))
         return "namespace::" + source.getLocalPart();
       prefix = nsContext.getPrefix(source.getURI());
       name = source.getLocalPart();
       return '@' + (StringUtil.isEmpty(prefix) ? name : prefix + ':' + name);
     case NodeType.NAMESPACE:
       return "namespace::" + source.getLocalPart();
     default:
       return null;
   }
 }
Beispiel #2
0
  private String orderByPhrase() {
    List<String> orderByList = new ArrayList<String>();
    try {
      Collection<AnnotationValue> orderBys =
          ModelUtil.getAnnotationValue(method, mirror, "orderBy");
      for (AnnotationValue orderByValue : orderBys) {
        AnnotationMirror orderByMirror = (AnnotationMirror) orderByValue.getValue();
        String columnProperty = ModelUtil.getAnnotationValue(method, orderByMirror, "column");
        ColumnProperty column = columns.findByProperty(columnProperty);
        if (column == null)
          throw new AnnotationError(method, mirror, "invalid column property: " + columnProperty);
        Order order =
            Order.valueOf(
                ((VariableElement) ModelUtil.getAnnotationValue(method, orderByMirror, "order"))
                    .getSimpleName()
                    .toString());
        orderByList.add("\"+" + column.columnName(true) + "+\" " + order.keyword);
      }
    } catch (AnnotationError ex) {
      // ignore
    }

    return orderByList.size() > 0
        ? " ORDER BY " + StringUtil.join(orderByList.iterator(), ", ")
        : "";
  }
Beispiel #3
0
 public static String suggestFile(URI uri, String extension) {
   String path = uri.getPath();
   String tokens[] = StringUtil.getTokens(path, "/", true);
   String file = tokens[tokens.length - 1];
   int dot = file.indexOf(".");
   if (dot == -1) return file + '.' + extension;
   else return file.substring(0, dot + 1) + extension;
 }
Beispiel #4
0
 public static URL toURL(String systemID) {
   if (StringUtil.isWhitespace(systemID)) return null;
   systemID = systemID.trim();
   try {
     return new URL(systemID);
   } catch (MalformedURLException ex) {
     return FileUtil.toURL(new File(systemID));
   }
 }
Beispiel #5
0
  protected CharSequence[] defaultSQL(Iterator<VariableElement> iter) {
    int paramCount = method.getParameters().size();
    List<String> code = new ArrayList<String>();
    CollectionUtil.addAll(
        code,
        "java.util.List<String> __conditions = new java.util.ArrayList<String>("
            + paramCount
            + ");",
        "java.util.List<Object> __params = new java.util.ArrayList<Object>(" + paramCount + ");");

    List<String> params = new ArrayList<String>();
    List<String> where = new ArrayList<String>();
    while (iter.hasNext()) {
      VariableElement param = iter.next();
      String paramName = param.getSimpleName().toString();
      boolean primitive = ModelUtil.isPrimitive(param.asType());
      if (paramName.indexOf('_') == -1) {
        ColumnProperty column = getColumn(param);
        where.add("\"+" + column.columnName(true) + "+\"=?");
        params.add(column.toNativeTypeCode(paramName));
        if (!primitive) CollectionUtil.addAll(code, "if(" + paramName + "!=null){", PLUS);
        CollectionUtil.addAll(
            code,
            "__conditions.add(" + where.get(where.size() - 1).substring(2) + "\");",
            "__params.add(" + column.toNativeTypeCode(paramName) + ");");
        if (!primitive) CollectionUtil.addAll(code, MINUS, "}");
        iter.remove();
      } else {
        int underscore = paramName.indexOf('_');
        String hint = paramName.substring(0, underscore);
        String propertyName = paramName.substring(underscore + 1);
        ColumnProperty column = getColumn(param, propertyName);

        String hintValue = HINTS.get(hint);
        if (hintValue != null) {
          where.add("\"+" + column.columnName(true) + "+\"" + hintValue);
          params.add(column.toNativeTypeCode(paramName));
          if (!primitive) CollectionUtil.addAll(code, "if(" + paramName + "!=null){", PLUS);
          CollectionUtil.addAll(
              code,
              "__conditions.add(" + where.get(where.size() - 1).substring(2) + "\");",
              "__params.add(" + column.toNativeTypeCode(paramName) + ");");
          if (!primitive) CollectionUtil.addAll(code, MINUS, "}");
          iter.remove();
        } else if ("from".equals(hint)) {
          iter.remove();
          final VariableElement nextParam = iter.next();
          final String nextParamName = nextParam.getSimpleName().toString();
          boolean nextPrimitive = ModelUtil.isPrimitive(nextParam.asType());
          if (!nextParamName.equals("to_" + propertyName))
            throw new AnnotationError(
                method, "the next parameter of " + paramName + " must be to_" + propertyName);
          if (param.asType() != nextParam.asType())
            throw new AnnotationError(
                method, paramName + " and " + nextParamName + " must be of same type");
          where.add("\"+" + column.columnName(true) + "+\" BETWEEN ? and ?");
          params.add(column.toNativeTypeCode(paramName));
          params.add(column.toNativeTypeCode(nextParamName));
          if (!primitive || !nextPrimitive) {
            String condition = "";
            if (!primitive) condition = paramName + "!=null";
            if (!nextPrimitive) {
              if (condition.length() > 0) condition += " && ";
              condition += nextParamName + "!=null";
            }
            CollectionUtil.addAll(code, "if(" + condition + "){", PLUS);
          }
          CollectionUtil.addAll(
              code,
              "__conditions.add(" + where.get(where.size() - 1).substring(2) + "\");",
              "__params.add(" + column.toNativeTypeCode(paramName) + ");",
              "__params.add(" + column.toNativeTypeCode(nextParamName) + ");");
          if (!primitive || !nextPrimitive) CollectionUtil.addAll(code, MINUS, "}");
          iter.remove();
        } else throw new AnnotationError(param, "invalid hint: " + hint);
      }
    }

    Boolean ignoreNullConditions = false;
    try {
      ignoreNullConditions = ModelUtil.getAnnotationValue(method, mirror, "ignoreNullConditions");
    } catch (AnnotationError ex) {
      // ignore
    }

    if (ignoreNullConditions) {
      String orderByPhrase = orderByPhrase();
      if (orderByPhrase.length() > 0) initialQuery = "";

      String queryInitialValue;
      if (initialQuery == null) queryInitialValue = "null";
      else queryInitialValue = '"' + initialQuery + '"';
      CollectionUtil.addAll(
          code,
          "String __query = " + queryInitialValue + ";",
          "if(__conditions.size()>0)",
          PLUS,
          "__query "
              + (initialQuery == null ? "" : "+")
              + "= \" WHERE \" + "
              + StringUtil.class.getName()
              + ".join(__conditions.iterator(), \" AND \");",
          MINUS);

      if (orderByPhrase.length() > 0) {
        CollectionUtil.addAll(code, "__query += \"" + orderByPhrase + "\";");
      }

      CollectionUtil.addAll(code, "__query", "__params.toArray()");
      return code.toArray(new CharSequence[code.size()]);
    } else {
      StringBuilder query = new StringBuilder();
      if (initialQuery != null) query.append(initialQuery).append(' ');
      if (where.size() > 0)
        query.append("WHERE ").append(StringUtil.join(where.iterator(), " AND "));
      query.append(orderByPhrase());
      return new CharSequence[] {query, StringUtil.join(params.iterator(), ", ")};
    }
  }