亲棋牌手机版

Home » apache-ivyde-sources-2.0.0.final-200907011148-RELEASE » org.apache.ivyde.common.ivyfile » [javadoc | source]
    1   /*
    2    *  Licensed to the Apache Software Foundation (ASF) under one or more
    3    *  contributor license agreements.  See the NOTICE file distributed with
    4    *  this work for additional information regarding copyright ownership.
    5    *  The ASF licenses this file to You under the Apache License, Version 2.0
    6    *  (the "License"); you may not use this file except in compliance with
    7    *  the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    *  Unless required by applicable law or agreed to in writing, software
   12    *  distributed under the License is distributed on an "AS IS" BASIS,
   13    *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    *  See the License for the specific language governing permissions and
   15    *  limitations under the License.
   16    *
   17    */
   18   package org.apache.ivyde.common.ivyfile;
   19   
   20   import java.io.File;
   21   import java.io.FileNotFoundException;
   22   import java.io.FileOutputStream;
   23   import java.io.IOException;
   24   import java.io.PrintWriter;
   25   import java.util.regex.Matcher;
   26   import java.util.regex.Pattern;
   27   
   28   import org.apache.ivy.core.module.id.ModuleId;
   29   import org.apache.ivy.core.module.id.ModuleRevisionId;
   30   import org.apache.ivy.util.FileUtil;
   31   
   32   public class IvyFileUpdater {
   33       private static final String NL = System.getProperty("line.separator");
   34   
   35       private static class UpdateInfo {
   36           int insertFromIndex = 0;
   37   
   38           int insertToIndex = 0;
   39   
   40           String prefix = "";
   41   
   42           String suffix = "";
   43   
   44           String insert = "";
   45       }
   46   
   47       public void addDependency(File ivyFile, String org, String name, String revision,
   48               String confMapping) throws IOException {
   49           ModuleRevisionId depId = new ModuleRevisionId(new ModuleId(org, name), revision);
   50           addDependency(ivyFile, depId, confMapping);
   51       }
   52   
   53       public void addDependency(File ivyFile, ModuleRevisionId depId, String confMapping)
   54               throws IOException {
   55           String content = FileUtil.readEntirely(ivyFile);
   56   
   57           UpdateInfo info = findUpdateInfoToAddDependency(content, depId, confMapping);
   58   
   59           update(ivyFile, content, info);
   60       }
   61   
   62       /**
   63        * Removes a direct dependency in the given Ivy file, or excludes it if it isn't a direct
   64        * dependency, unless the Ivy file declares no dependency at all, in which case the file isn't
   65        * changed.
   66        * 
   67        * @param ivyFile
   68        *            the file pointing to the Ivy file to update
   69        * @param depId
   70        *            the module id of the dependency to remove or exclude
   71        */
   72       public void removeOrExcludeDependency(File ivyFile, ModuleId depId) throws IOException {
   73           String content = FileUtil.readEntirely(ivyFile);
   74   
   75           UpdateInfo info = findUpdateInfoToRemoveDependency(content, depId);
   76           if (info != null) {
   77               update(ivyFile, content, info);
   78           }
   79       }
   80   
   81       private void update(File ivyFile, String content, UpdateInfo info) throws FileNotFoundException {
   82           PrintWriter w = new PrintWriter(new FileOutputStream(ivyFile));
   83           try {
   84               w.print(content.substring(0, info.insertFromIndex));
   85               w.print(info.prefix);
   86               w.print(info.insert);
   87               w.print(info.suffix);
   88               w.print(content.substring(info.insertToIndex));
   89               w.flush();
   90           } finally {
   91               w.close();
   92           }
   93       }
   94   
   95       private UpdateInfo findUpdateInfoToAddDependency(String content, ModuleRevisionId depId,
   96               String confMapping) {
   97           UpdateInfo info = new UpdateInfo();
   98           info.insert = getDependencyToAdd(depId, confMapping);
   99   
  100           Pattern dependenciesClose = Pattern.compile("<\\s*/dependencies");
  101           Matcher depsCloseMatcher = dependenciesClose.matcher(content);
  102           if (depsCloseMatcher.find()) {
  103               info.insertFromIndex = findLastDependencyEnd(content, depsCloseMatcher.start());
  104               if (info.insertFromIndex == -1) {
  105                   info.insertFromIndex = getLastMatchIndex(Pattern.compile("<\\s*dependencies.*?>"),
  106                       content, depsCloseMatcher.start());
  107                   if (info.insertFromIndex == -1) {
  108                       info.insertFromIndex = depsCloseMatcher.start();
  109                   } else {
  110                       info.prefix = NL;
  111                   }
  112               } else {
  113                   info.prefix = NL;
  114               }
  115               info.insertToIndex = info.insertFromIndex;
  116               return info;
  117           }
  118           Pattern depsOpenClose = Pattern.compile("<\\s*dependencies\\s*/>");
  119           Matcher depsOpenCloseMatcher = depsOpenClose.matcher(content);
  120           if (depsOpenCloseMatcher.find()) {
  121               info.insertFromIndex = depsOpenCloseMatcher.start();
  122               info.insertToIndex = depsOpenCloseMatcher.end();
  123               info.prefix = "<dependencies>" + NL;
  124               info.suffix = NL + "    </dependencies>";
  125               return info;
  126           }
  127           Pattern moduleClose = Pattern.compile("</\\s*ivy-module\\s*>");
  128           Matcher moduleCloseMatcher = moduleClose.matcher(content);
  129           if (moduleCloseMatcher.find()) {
  130               info.insertFromIndex = moduleCloseMatcher.start();
  131               info.insertToIndex = info.insertFromIndex;
  132               info.prefix = "    <dependencies>" + NL;
  133               info.suffix = NL + "    </dependencies>" + NL;
  134               return info;
  135           }
  136           return info;
  137       }
  138   
  139       private UpdateInfo findUpdateInfoToRemoveDependency(String content, ModuleId depId) {
  140           UpdateInfo info = new UpdateInfo();
  141   
  142           Matcher depsMatcher = Pattern.compile("<\\s*dependencies").matcher(content);
  143           if (!depsMatcher.find()) {
  144               // no dependencies at all, nothing to do
  145               return null;
  146           }
  147           Matcher depMatcher = Pattern.compile("<\\s*dependency\\s+.*name=[\"']([^\"']+)[\"']")
  148                   .matcher(content);
  149           int start = depsMatcher.start();
  150           while (depMatcher.find(start)) {
  151               if (depId.getName().equals(depMatcher.group(1))) {
  152                   // we have found the dependency to remove: let's remove it
  153                   info.insertFromIndex = depMatcher.start();
  154                   Matcher m = Pattern.compile("</\\s*dependency\\s*>").matcher(content);
  155                   if (m.find(info.insertFromIndex)) {
  156                       info.insertToIndex = m.end();
  157                   }
  158                   m = Pattern.compile("<\\s*dependency[^<]*?\\/\\>").matcher(content);
  159                   if (m.find(info.insertFromIndex)) {
  160                       info.insertToIndex = info.insertToIndex > 0 ? Math.min(info.insertToIndex, m
  161                               .end()) : m.end();
  162                   }
  163                   info.insertFromIndex = findStartOfBlock(content, info.insertFromIndex);
  164                   info.insertToIndex = findEndOfBlock(content, info.insertToIndex);
  165                   return info;
  166               }
  167               start = depMatcher.end();
  168           }
  169   
  170           // we haven't found the dependency in the list of declared dependencies
  171   
  172           if (start == depsMatcher.start()) {
  173               // no dependencies at all, nothing to do
  174               return null;
  175           }
  176           // there is at least one direct dependency, but not the one to remove, so we must exclude it
  177           Matcher depsCloseMatcher = Pattern.compile("<\\s*/dependencies").matcher(content);
  178           if (!depsCloseMatcher.find()) {
  179               // no closing tag for dependencies, probably malformed xml, nothing to do
  180               return null;
  181           }
  182           info.insertFromIndex = findLastDependencyEnd(content, depsCloseMatcher.start());
  183           info.insertToIndex = info.insertFromIndex;
  184           info.prefix = NL;
  185           info.insert = getDependencyToExclude(depId);
  186           return info;
  187       }
  188   
  189       private int findLastDependencyEnd(String content, int end) {
  190           int depCloseIndex = getLastMatchIndex(Pattern.compile("</\\s*dependency\\s*>"), content,
  191               end);
  192           int depOpCloseIndex = getLastMatchIndex(Pattern.compile("\\<\\s*dependency.*?\\/\\>"),
  193               content, end);
  194           return Math.max(depCloseIndex, depOpCloseIndex);
  195       }
  196   
  197       private String getDependencyToAdd(ModuleRevisionId depId, String confMapping) {
  198           String dep = "        <dependency org=\"" + depId.getOrganisation() + "\"";
  199           dep += " name=\"" + depId.getName() + "\"";
  200           dep += " rev=\"" + depId.getRevision() + "\"";
  201           if (confMapping != null) {
  202               dep += " conf=\"" + confMapping + "\"";
  203           }
  204           dep += " />";
  205           return dep;
  206       }
  207   
  208       private String getDependencyToExclude(ModuleId depId) {
  209           String dep = "        <exclude org=\"" + depId.getOrganisation() + "\"";
  210           dep += " module=\"" + depId.getName() + "\"";
  211           dep += " />";
  212           return dep;
  213       }
  214   
  215       private int getLastMatchIndex(Pattern pattern, String content, int end) {
  216           Matcher matcher = pattern.matcher(content);
  217           int index = -1;
  218           while (matcher.find(index + 1)) {
  219               if (matcher.end() > end) {
  220                   return index;
  221               } else {
  222                   index = matcher.end();
  223               }
  224           }
  225           return index;
  226       }
  227   
  228       private int findStartOfBlock(String content, int index) {
  229           for (index--; index >= 0; index--) {
  230               char c = content.charAt(index);
  231               if (c != ' ' && c != '\t') {
  232                   return index + 1;
  233               }
  234           }
  235           return 0;
  236       }
  237   
  238       private int findEndOfBlock(String content, int index) {
  239           for (; index < content.length(); index++) {
  240               char c = content.charAt(index);
  241               if (c != ' ' && c != '\t') {
  242                   if (c == '\n' || c == '\r') {
  243                       return index + 1;
  244                   }
  245                   return index;
  246               }
  247           }
  248           return index - 1;
  249       }
  250   
  251   }

Home » apache-ivyde-sources-2.0.0.final-200907011148-RELEASE » org.apache.ivyde.common.ivyfile » [javadoc | source]
乐优彩票天天红单下载 乐博现金lb377775 乐优彩票登入,备用,官方网址 乐优彩票网址是多少 乐优炫彩app 乐博28app下载 乐优炫彩app下载 乐优炫彩app天天红单 乐优炫彩天天红单 乐优炫彩官网