Example #1
0
  /**
   * 批量修改图片sel文件名,复制的方式拷贝到同级目录下的sel文件夹中 <br>
   * 修改成功后请再使用batchCreateSelFiles方法生成对应的sel.xml文件
   *
   * @param normalPath normal普通状态图片的文件夹
   * @param specialPath 特殊状态(pressed按下/checked选中)图片的文件夹
   * @param pre 需要添加的前缀
   * @param end 特殊状态(pressed按下/checked选中)后缀名
   */
  public static void batchRenameSelFiles(
      String normalPath, String specialPath, String pre, String end) {
    String savePath = normalPath.substring(0, normalPath.lastIndexOf("\\"));
    File saveDir = new File(savePath, "sel");
    if (!saveDir.exists()) {
      saveDir.mkdirs();
    }

    List<File> files = FileUtils.getAllFiles(normalPath);
    for (File file : files) {
      String fileName = pre + "_" + file.getName();
      fileName = fileName.replace(" ", "_").toLowerCase(Locale.CHINESE);

      File nFile = new File(saveDir, fileName);
      //			file.renameTo(nFile); // rename 直接移动文件,我们希望是复制并重命名
      FileUtils.copyFileByChannel(file, nFile);
    }
    List<File> filePresses = FileUtils.getAllFiles(specialPath);
    for (File file : filePresses) {
      String[] nameMap = FileUtils.getNameMap(file);

      String fileName = pre + "_" + nameMap[0] + "_" + end + nameMap[1];
      fileName = fileName.replace(" ", "_").toLowerCase(Locale.CHINESE);

      File nFile = new File(saveDir, fileName);
      //			file.renameTo(nFile); // rename 直接移动文件,我们希望是复制并重命名
      FileUtils.copyFileByChannel(file, nFile);
    }
  }
Example #2
0
 /**
  * 批量生成sel的xml文件 <br>
  * 比如图片名字是ic_img.png和按下的ic_img_pressed.png,那么最终生成的就是封装好的ic_img_sel.xml
  *
  * @param path 包含全部图片的文件路径
  * @param end 特殊状态(pressed按下/checked选中)后缀名
  */
 public static void batchCreateSelFiles(String path, String end) {
   List<File> files = FileUtils.getAllFiles(path);
   for (File file : files) {
     String fileName = FileUtils.getName(file);
     // 用normal状态的图片名生成对应的_sel.xml文件
     if (!fileName.endsWith(end)) {
       Document doc = createSelector(fileName, fileName + "_" + end, end);
       //				fileName + "_" + end
       File nFile = new File(path, fileName + "_sel.xml");
       XmlUtil.write2xml(nFile, doc);
     }
   }
 }
Example #3
0
 public static void relaceLayoutViewNames(
     String proPath, List<String> tarList, String replacement) {
   List<File> allFiles = FileUtils.getAllFiles(proPath);
   for (File file : allFiles) {
     // 如果是xml文件,且在layout..目录下,则视为布局文件
     if (file.getName().endsWith(".xml") && file.getParentFile().getName().startsWith("layout")) {
       Document document = XmlUtil.read(file);
       boolean hasReplace = XmlUtil.replaceElementName(document, tarList, replacement);
       if (hasReplace) {
         XmlUtil.write2xml(file, document);
       }
     }
   }
 }
Example #4
0
  /**
   * 删除无用的src文件
   *
   * @param rootPath 根目录的绝对路径
   */
  public static void delNoUseSrcFile(String rootPath) {
    List<File> files = FileUtils.getAllFiles(rootPath);
    out:
    for (File file : files) {
      String name = file.getName();
      // 只需要删除src包下的文件
      if (!file.getPath().contains("\\src\\")) {
        continue;
      }

      for (File compareFile : files) {
        String compareName = compareFile.getName();
        if (name.equals(compareName)) {
          // 自己跟自己不匹配
          continue;
        }

        // 只需要对比src包和layout包下的文件
        if (!compareFile.getPath().contains("\\src\\")
            && !compareFile.getPath().contains("\\layout")) {
          continue;
        }

        if (!compareFile.exists()) {
          continue;
        }

        // 如果对比文件的本文内容中包含文件名,则视为有使用
        String fileContent = FileUtils.readToString(compareFile);
        if (fileContent.contains(FileUtils.getName(file))) {
          continue out;
        }
      }

      // 删除没使用过的文件
      String absname = file.getAbsoluteFile().getName();
      boolean delete = file.delete();
      System.out.println(absname + " ... delete=" + delete);
    }
  }
Example #5
0
  /**
   * 将参数值抽取到values文件夹下
   *
   * <p>如textColor="#ff00aa",将#ff00aa这样的具体值替换为@color/colorname <br>
   * 并在color.xml文件内创建一个对应颜色item
   *
   * @param proPath 项目绝对路径
   * @param valuesXml values文件名称,如strings.xml dimens.xml等
   * @param type 抽取内容的前缀,如@color/,则type参数就输入"color"
   * @param itemName values文件内item的名称
   * @param itemAttrName values文件内item的参数,一般都是name
   * @param itemAttrValue values文件内item的参数值,也是抽取值后替换的名称
   * @param itemValue values文件内item的值,即替换后的值
   * @param values 被替换的内容,支持模糊替换,只要匹配集合里中其中一个就会被抽取替换,最终抽取成一个值itemValue
   * @param matchAttr 匹配参数名,即只会替换该参数名对应的值
   */
  @SuppressWarnings("unchecked")
  public static void extract2valuesByJsoup(
      String proPath,
      String valuesXml,
      String type,
      String itemName,
      String itemAttrName,
      String itemAttrValue,
      String itemValue,
      List<String> values,
      String matchAttr) {
    List<File> files = FileUtils.getAllFiles(new File(proPath));

    String valuesPath = proPath + "/res/values/" + valuesXml;
    File valuesFile = new File(valuesPath);
    if (!valuesFile.exists()) {
      System.out.println("文件不存在,请确定文件[" + valuesXml + "]位于/res/values/文件夹下,且文件名称正确");
      return;
    }

    int extractFileCount = 0;
    for (File file : files) {
      if (!file.getName().endsWith(".xml")) {
        continue;
      }

      if (file.getName().equals(valuesXml)) {
        continue;
      }

      Document tarDoc = XmlUtil.read(file);

      // 是否有替换操作
      boolean isReplace =
          XmlUtil.replaceAttrValue(tarDoc, values, "@" + type + "/" + itemAttrValue, matchAttr);

      if (!isReplace) {
        continue;
      }

      XmlUtil.write2xml(file, tarDoc);

      Document valuesDoc = XmlUtil.read(valuesFile);
      Element rootElement = valuesDoc.getRootElement();
      List<Element> elements = rootElement.elements();

      // 是否在values/xx.xml对应文件下下已有某个抽取过的值
      boolean hasInValues = false;

      for (Element element : elements) {
        String attrValue = element.attributeValue(itemAttrName);
        if (attrValue.equals(itemAttrValue)) {
          hasInValues = true;
          break;
        }
      }

      if (!hasInValues) {
        Element element = rootElement.addElement(itemName);
        element.addAttribute(itemAttrName, itemAttrValue);
        element.setText(itemValue);

        XmlUtil.write2xml(valuesFile, valuesDoc);
      }

      extractFileCount++;
    }

    System.out.println(
        "将" + values + "抽取为[" + valuesXml + "]文件下的[" + itemAttrValue + "=" + itemValue + "]");
    System.out.println("共抽取了" + extractFileCount + "个文件下的内容");
    System.out.println("-------------------------");
  }