예제 #1
0
  private void writeValue(String key, String space, String prefix, Object value, BufferedWriter out)
      throws IOException {
    //        key = key.indexOf('.') > -1 ? InvokerHelper.inspect(key) : key;
    boolean isKeyword = KEYWORDS.contains(key);
    key = isKeyword ? InvokerHelper.inspect(key) : key;

    if (!StringGroovyMethods.asBoolean(prefix) && isKeyword) prefix = "this.";
    out.append(space).append(prefix).append(key).append('=').append(InvokerHelper.inspect(value));
    out.newLine();
  }
예제 #2
0
 private MetaClass skipTokens(LinkedList<String> tokens, MetaClass metaClass) {
   while (tokens.size() > 1) {
     Class theClass = metaClass.getTheClass();
     String token = tokens.pollFirst();
     Class selectedClass = null;
     if (propertyPattern.matcher(token).matches()) {
       String getterName = "get" + StringGroovyMethods.capitalize((CharSequence) token);
       for (MetaMethod metaMethod : metaClass.getMetaMethods()) {
         if (metaMethod.getName().equals(getterName)) {
           selectedClass = metaMethod.getReturnType();
           break;
         }
       }
       if (selectedClass == null)
         for (Method method : theClass.getMethods()) {
           if (method.getParameterTypes().length != 0) continue;
           if (method.getName().equals(getterName)) {
             selectedClass = method.getReturnType();
             break;
           }
         }
       if (selectedClass == null) {
         MetaProperty metaProperty = metaClass.getMetaProperty(token);
         if (metaProperty == null) return null;
         selectedClass = metaProperty.getType();
       }
     } else if (methodPattern.matcher(token).matches()) {
       String curMethodName = token.substring(0, token.indexOf('('));
       for (MetaMethod metaMethod : metaClass.getMetaMethods()) {
         String name = metaMethod.getName();
         if (name.equals(curMethodName)) {
           selectedClass = metaMethod.getReturnType();
           break;
         }
       }
       if (selectedClass == null)
         for (Method method : theClass.getMethods()) {
           String name = method.getName();
           if (name.equals(curMethodName)) {
             selectedClass = method.getReturnType();
             break;
           }
         }
     }
     if (selectedClass == null) return null;
     metaClass = InvokerHelper.getMetaClass(selectedClass);
   }
   return metaClass;
 }
 protected String makeRelativeIfPossible(String fileName, String base) throws IOException {
   if (StringUtils.hasLength(base)) {
     fileName = StringGroovyMethods.minus(fileName, new File(base).getCanonicalPath());
   }
   return fileName;
 }
예제 #4
0
  private void writeConfig(
      String prefix, ConfigObject map, BufferedWriter out, int tab, boolean apply)
      throws IOException {
    String space = apply ? StringGroovyMethods.multiply(TAB_CHARACTER, tab) : "";

    for (Object o1 : map.keySet()) {
      String key = (String) o1;
      Object v = map.get(key);

      if (v instanceof ConfigObject) {
        ConfigObject value = (ConfigObject) v;

        if (!value.isEmpty()) {

          Object dotsInKeys = null;
          for (Object o : value.entrySet()) {
            Entry e = (Entry) o;
            String k = (String) e.getKey();
            if (k.indexOf('.') > -1) {
              dotsInKeys = e;
              break;
            }
          }

          int configSize = value.size();
          Object firstKey = value.keySet().iterator().next();
          Object firstValue = value.values().iterator().next();

          int firstSize;
          if (firstValue instanceof ConfigObject) {
            firstSize = ((ConfigObject) firstValue).size();
          } else {
            firstSize = 1;
          }

          if (configSize == 1 || DefaultGroovyMethods.asBoolean(dotsInKeys)) {
            if (firstSize == 1 && firstValue instanceof ConfigObject) {
              key = KEYWORDS.contains(key) ? InvokerHelper.inspect(key) : key;
              String writePrefix = prefix + key + "." + firstKey + ".";
              writeConfig(writePrefix, (ConfigObject) firstValue, out, tab, true);
            } else if (!DefaultGroovyMethods.asBoolean(dotsInKeys)
                && firstValue instanceof ConfigObject) {
              writeNode(key, space, tab, value, out);
            } else {
              for (Object j : value.keySet()) {
                Object v2 = value.get(j);
                Object k2 = ((String) j).indexOf('.') > -1 ? InvokerHelper.inspect(j) : j;
                if (v2 instanceof ConfigObject) {
                  key = KEYWORDS.contains(key) ? InvokerHelper.inspect(key) : key;
                  writeConfig(prefix + key, (ConfigObject) v2, out, tab, false);
                } else {
                  writeValue(key + "." + k2, space, prefix, v2, out);
                }
              }
            }
          } else {
            writeNode(key, space, tab, value, out);
          }
        }
      } else {
        writeValue(key, space, prefix, v, out);
      }
    }
  }