Home » xmlbeans-2.5.0-src » repackage » [javadoc | source]

    1   /*   Copyright 2004 The Apache Software Foundation
    2    *
    3    *   Licensed under the Apache License, Version 2.0 (the "License");
    4    *   you may not use this file except in compliance with the License.
    5    *   You may obtain a copy of the License at
    6    *
    7    *       http://www.apache.org/licenses/LICENSE-2.0
    8    *
    9    *   Unless required by applicable law or agreed to in writing, software
   10    *   distributed under the License is distributed on an "AS IS" BASIS,
   11    *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   12    *   See the License for the specific language governing permissions and
   13    *  limitations under the License.
   14    */
   15   
   16   package repackage;
   17   
   18   import java.util.List;
   19   import java.util.ArrayList;
   20   import java.util.regex.Pattern;
   21   import java.util.regex.Matcher;
   22   import java.io.File;
   23   
   24   public class Repackager
   25   {
   26       public Repackager ( String repackageSpecs )
   27       {
   28           _fromPackages = new ArrayList();
   29           _toPackages = new ArrayList();
   30           
   31           List repackages = splitPath( repackageSpecs, ';' );
   32   
   33           // Sort the repackage spec so that longer from's are first to match
   34           // longest package first
   35           
   36           for ( ; ; )
   37           {
   38               boolean swapped = false;
   39   
   40               for ( int i = 1 ; i < repackages.size() ; i++ )
   41               {
   42                   String spec1 = (String) repackages.get( i - 1 );
   43                   String spec2 = (String) repackages.get( i );
   44                   
   45                   if (spec1.indexOf( ':' ) < spec2.indexOf( ':' ))
   46                   {
   47                       repackages.set( i - 1, spec2 );
   48                       repackages.set( i, spec1 );
   49                       
   50                       swapped = true;
   51                   }
   52               }
   53   
   54               if (!swapped)
   55                   break;
   56           }
   57   
   58           for ( int i = 0 ; i < repackages.size() ; i++ )
   59           {
   60               String spec = (String) repackages.get( i );
   61               
   62               int j = spec.indexOf( ':' );
   63   
   64               if (j < 0 || spec.indexOf( ':', j + 1 ) >= 0)
   65                   throw new RuntimeException( "Illegal repackage specification: " + spec );
   66   
   67               String from = spec.substring( 0, j );
   68               String to = spec.substring( j + 1 );
   69   
   70               _fromPackages.add( Repackager.splitPath( from, '.' ) );
   71               _toPackages.add( Repackager.splitPath( to, '.' ) );
   72           }
   73   
   74           _fromMatchers = new Matcher [ _fromPackages.size() * 2 ];
   75           _toPackageNames = new String [ _fromPackages.size() * 2 ];
   76   
   77           addPatterns( '.', 0 );
   78           addPatterns( '/', _fromPackages.size() );
   79       }
   80   
   81       void addPatterns ( char sep, int off )
   82       {
   83           for ( int i = 0 ; i < _fromPackages.size() ; i++ )
   84           {
   85               List from = (List) _fromPackages.get( i );
   86               List to = (List) _toPackages.get( i );
   87   
   88               String pattern = "";
   89               
   90               for ( int j = 0 ; j < from.size() ; j++ )
   91               {
   92                   if (j > 0)
   93                       pattern += "\\" + sep;
   94   
   95                   pattern += from.get( j );
   96               }
   97               
   98               String toPackage = "";
   99               
  100               for ( int j = 0 ; j < to.size() ; j++ )
  101               {
  102                   if (j > 0)
  103                       toPackage += sep;
  104   
  105                   toPackage += to.get( j );
  106               }
  107   
  108               _fromMatchers[ off + i ] = Pattern.compile( pattern ).matcher( "" );
  109               _toPackageNames[ off + i ] = toPackage;
  110           }
  111       }
  112   
  113       public StringBuffer repackage ( StringBuffer sb )
  114       {
  115           StringBuffer result = null;
  116   
  117           for ( int i = 0 ; i < _fromMatchers.length ; i++ )
  118           {
  119               Matcher m = (Matcher) _fromMatchers[ i ];
  120               
  121               m.reset( sb );
  122               
  123               for ( boolean found = m.find() ; found ; found = m.find() )
  124               {
  125                   if (result == null)
  126                       result = new StringBuffer();
  127   
  128                   m.appendReplacement( result, _toPackageNames[ i ] );
  129               }
  130   
  131               if (result != null)
  132               {
  133                   m.appendTail( result );
  134                   sb = result;
  135                   result = null;
  136               }
  137           }
  138   
  139           return sb;
  140       }
  141   
  142       public List getFromPackages ( )
  143       {
  144           return _fromPackages;
  145       }
  146   
  147       public List getToPackages ( )
  148       {
  149           return _toPackages;
  150       }
  151       
  152       public static ArrayList splitPath ( String path, char separator )
  153       {
  154           ArrayList components = new ArrayList();
  155           
  156           for ( ; ; )
  157           {
  158               int i = path.indexOf( separator );
  159   
  160               if (i < 0)
  161                   break;
  162   
  163               components.add( path.substring( 0, i ) );
  164               
  165               path = path.substring( i + 1 );
  166           }
  167   
  168           if (path.length() > 0)
  169               components.add( path );
  170   
  171           return components;
  172       }
  173       
  174       public static String dirForPath ( String path )
  175       {
  176           return new File(path).getParent();
  177       }
  178   
  179       private List _fromPackages;
  180       private List _toPackages;
  181       
  182       private Matcher[] _fromMatchers;
  183       private String[]  _toPackageNames;
  184   }

Home » xmlbeans-2.5.0-src » repackage » [javadoc | source]