sbailliez    02/02/26 14:43:15

  Modified:    src/java/org/apache/maven/jrcs/rcs Archive.java
                        ArchiveParser.java
  Added:       src/java/org/apache/maven/jrcs/rcs ArchiveFormat.java
               src/test/org/apache/maven/jrcs/rcs ArchiveFormatTest.java
  Log:
  - Removed the _ coding style.
  - Extracted a class specialized in formatting
  - get rid of RegExp and use Oro
  
  Revision  Changes    Path
  1.9       +104 -175  
jakarta-turbine-maven/src/java/org/apache/maven/jrcs/rcs/Archive.java
  
  Index: Archive.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-turbine-maven/src/java/org/apache/maven/jrcs/rcs/Archive.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- Archive.java      26 Feb 2002 15:13:27 -0000      1.8
  +++ Archive.java      26 Feb 2002 22:43:15 -0000      1.9
  @@ -59,6 +59,9 @@
   import java.io.FileOutputStream;
   import java.io.OutputStream;
   import java.io.OutputStreamWriter;
  +import java.io.InputStream;
  +import java.io.FileNotFoundException;
  +import java.io.IOException;
   import java.text.Format;
   import java.text.MessageFormat;
   import java.util.Collection;
  @@ -73,6 +76,7 @@
   
   import org.apache.maven.jrcs.diff.Diff;
   import org.apache.maven.jrcs.diff.PatchFailedException;
  +import org.apache.maven.jrcs.diff.DiffException;
   import org.apache.maven.jrcs.util.ToString;
   
   public class Archive
  @@ -80,19 +84,21 @@
   {
       public static final String RCS_NEWLINE = "\n";
   
  -    protected TrunkNode _head;
  -    protected Version _branch;
  -    protected Map _nodes = new TreeMap(); //!!! check Node.compareTo for correct 
RCS order
  -    protected Set _users = new TreeSet();
  -    protected Set _locked = new TreeSet();
  -    protected Map _symbols = new TreeMap();
  -    protected Phrases _phrases = new Phrases();
  -    protected String _desc = new String();
  -    protected boolean _strictLocking = true;
  -    protected String _expand;
  -    protected String _comment = "# ";
  -    protected String _filename = "__unknown__,v";
  +    protected TrunkNode head;
  +    protected Version branch;
  +    protected Map nodes = new TreeMap(); //!!! check Node.compareTo for correct RCS 
order
  +    protected Set users = new TreeSet();
  +    protected Set locked = new TreeSet();
  +    protected Map symbols = new TreeMap();
  +    protected Phrases phrases = new Phrases();
  +    protected String desc = new String();
  +    protected boolean strictLocking = true;
  +    protected String expand;
  +    protected String comment = "# ";
  +    protected String filename = "__unknown__,v";
   
  +    // synchronize this if this has to be used in MT !
  +    private final static ArchiveFormat FORMATTER = new ArchiveFormat();
   
       /**
        * Creates a new archive and sets the text of the initial revision.
  @@ -137,9 +143,9 @@
               vernum = vernum.newBranch(1);
           }
           // now add the _head node
  -        this._head = (TrunkNode) newNode(vernum, null);
  -        this._head.setText(text);
  -        this._head.setLog("Initial revision\n");
  +        this.head = (TrunkNode) newNode(vernum, null);
  +        this.head.setText(text);
  +        this.head.setLog("Initial revision\n");
           this.setDesc(desc);
       }
   
  @@ -149,9 +155,9 @@
        * @param fname The name to give to the archive.
        * @param input Where to read the archive from
        */
  -    public Archive(String fname, java.io.InputStream input) throws ParseException
  +    public Archive(String fname, InputStream input) throws ParseException
       {
  -        this._filename = fname;
  +        this.filename = fname;
           ArchiveParser.load(this, input);
       }
   
  @@ -159,10 +165,10 @@
        * Load an archive from an a file given by name.
        * @param path The path to the file wher the archive resides.
        */
  -    public Archive(String path) throws ParseException, java.io.FileNotFoundException
  +    public Archive(String path) throws ParseException, FileNotFoundException
       {
  -        this._filename = new java.io.File(path).getPath();
  -        ArchiveParser.load(this, this._filename);
  +        this.filename = new File(path).getPath();
  +        ArchiveParser.load(this, this.filename);
       }
   
       /**
  @@ -176,23 +182,23 @@
   
       public void setFileName(String name)
       {
  -      this._filename = name;
  +      this.filename = name;
       }
   
  -    public void save(java.io.OutputStream output)
  -            throws java.io.IOException
  +    public void save(OutputStream output)
  +            throws IOException
       {
           new OutputStreamWriter(output).write(toCharArray());
       }
   
       public void save(String path)
  -            throws java.io.IOException
  +            throws IOException
       {
           OutputStream output = new FileOutputStream(path);
           try
           {
               save(output);
  -            this._filename = new File(path).getPath();
  +            this.filename = new File(path).getPath();
           }
           finally
           {
  @@ -202,12 +208,12 @@
   
       protected void setHead(Version vernum) throws InvalidVersionNumberException
       {
  -        if (_head != null)
  +        if (head != null)
           {
  -            throw new HeadAlreadySetException(_head.version);
  +            throw new HeadAlreadySetException(head.version);
           }
  -        _head = new TrunkNode(vernum, null);
  -        _nodes.put(vernum, _head);
  +        head = new TrunkNode(vernum, null);
  +        nodes.put(vernum, head);
       }
   
       public void setBranch(String v) throws InvalidBranchVersionNumberException
  @@ -221,21 +227,21 @@
           {
               throw new InvalidBranchVersionNumberException(vernum);
           }
  -        if (_head == null || vernum.getBase(2).isGreaterThan(_head.version))
  +        if (head == null || vernum.getBase(2).isGreaterThan(head.version))
           {
  -            throw new InvalidBranchVersionNumberException(vernum + "is greater than 
_head version " + _head.version);
  +            throw new InvalidBranchVersionNumberException(vernum + "is greater than 
_head version " + head.version);
           }
  -        _branch = vernum;
  +        branch = vernum;
       }
   
       public void addUser(String name)
       {
  -        _users.add(name);
  +        users.add(name);
       }
   
       public void addSymbol(String sym, Version vernum) throws 
InvalidVersionNumberException
       {
  -        _symbols.put(sym, vernum);
  +        symbols.put(sym, vernum);
       }
   
       public void addLock(String user, Version vernum)
  @@ -247,37 +253,37 @@
           node.setLocker(user);
           if (user == null)
           {
  -            _locked.remove(node);
  +            locked.remove(node);
           }
           else
           {
  -            _locked.add(node);
  +            locked.add(node);
           }
       }
   
       public void setStrictLocking(boolean value)
       {
  -        _strictLocking = value;
  +        strictLocking = value;
       }
   
       public void setExpand(String value)
       {
  -        _expand = value;
  +        expand = value;
       }
   
       public void setComment(String value)
       {
  -        _comment = value;
  +        comment = value;
       }
   
       public void setDesc(String value)
       {
  -        _desc = value;
  +        desc = value;
       }
   
       public void addPhrase(String key, Collection values)
       {
  -        _phrases.put(key, values);
  +        phrases.put(key, values);
       }
   
       protected Node getNode(Version vernum)
  @@ -288,7 +294,7 @@
           {
               throw new InvalidVersionNumberException(vernum);
           }
  -        Node node = (Node) _nodes.get(vernum);
  +        Node node = (Node) nodes.get(vernum);
           if (node == null)
           {
               throw new NodeNotFoundException();
  @@ -312,11 +318,11 @@
           {
               throw new InvalidVersionNumberException(vernum);
           }
  -        Node node = (Node) _nodes.get(vernum);
  +        Node node = (Node) nodes.get(vernum);
           if (node == null)
           {
               node = Node.newNode(vernum, prev);
  -            _nodes.put(vernum, node);
  +            nodes.put(vernum, node);
           }
           return node;
       }
  @@ -351,7 +357,7 @@
           {
               throw new InvalidVersionNumberException(vernum);
           }
  -        Node node = (Node) _nodes.get(vernum);
  +        Node node = (Node) nodes.get(vernum);
           if (node == null)
           {
               throw new NodeNotFoundException(vernum);
  @@ -370,7 +376,7 @@
           toString(s, EOL);
           return s.toString();
       }
  -    
  +
       public char[] toCharArray()
       {
           return toString(Archive.RCS_NEWLINE).toCharArray();
  @@ -378,13 +384,13 @@
   
       protected Path getRevisionPath(Version vernum)
       {
  -        if (_head == null)
  +        if (head == null)
           {
               return null;
           }
           try
           {
  -            Path path = _head.pathTo(vernum, true);
  +            Path path = head.pathTo(vernum, true);
               Node revisionFound = path.last();
               if (revisionFound == null)
               {
  @@ -415,13 +421,13 @@
   
       public Version getRevisionVersion()
       {
  -        if (_branch != null)
  +        if (branch != null)
           {
  -            return getRevisionVersion(_branch);
  +            return getRevisionVersion(branch);
           }
  -        else if (_head != null)
  +        else if (head != null)
           {
  -            return _head.version;
  +            return head.version;
           }
           else
           {
  @@ -435,22 +441,22 @@
           String NLT = EOL + "\t";
   
           s.append("head");
  -        if (_head != null)
  +        if (head != null)
           {
               s.append("\t");
  -            _head.version.toString(s);
  +            head.version.toString(s);
           }
           s.append(EOI);
   
  -        if (_branch != null)
  +        if (branch != null)
           {
               s.append("branch\t");
  -            s.append(_branch.toString());
  +            s.append(branch.toString());
               s.append(EOI);
           }
   
           s.append("access");
  -        for (Iterator i = _users.iterator(); i.hasNext();)
  +        for (Iterator i = users.iterator(); i.hasNext();)
           {
               s.append(EOL);
               s.append("\t");
  @@ -459,7 +465,7 @@
           s.append(EOI);
   
           s.append("symbols");
  -        for (Iterator i = _symbols.entrySet().iterator(); i.hasNext();)
  +        for (Iterator i = symbols.entrySet().iterator(); i.hasNext();)
           {
               Map.Entry e = (Map.Entry) i.next();
               s.append(NLT);
  @@ -470,39 +476,39 @@
           s.append(EOI);
   
           s.append("locks");
  -        for (Iterator i = _locked.iterator(); i.hasNext();)
  +        for (Iterator i = locked.iterator(); i.hasNext();)
           {
               String locker = ((Node) i.next())._locker;
               s.append(NLT);
               s.append(locker);
           }
  -        if (_strictLocking)
  +        if (strictLocking)
           {
               s.append("; strict");
           }
           s.append(EOI);
   
  -        if (_comment != null)
  +        if (comment != null)
           {
               s.append("comment\t");
  -            s.append(Archive.quoteString(_comment));
  +            s.append(Archive.quoteString(comment));
               s.append(EOI);
           }
   
  -        if (_expand != null)
  +        if (expand != null)
           {
               s.append("expand\t");
  -            s.append(Archive.quoteString(_expand));
  +            s.append(Archive.quoteString(expand));
               s.append(EOI);
           }
   
  -        if (_phrases != null)
  +        if (phrases != null)
           {
  -            _phrases.toString(s, EOL);
  +            phrases.toString(s, EOL);
           }
           s.append(EOL);
   
  -        for (Iterator i = _nodes.values().iterator(); i.hasNext();)
  +        for (Iterator i = nodes.values().iterator(); i.hasNext();)
           {
               Node n = (Node) i.next();
               if (!n.version.isGhost() && n._text != null)
  @@ -514,10 +520,10 @@
           s.append(EOL + EOL);
           s.append("desc");
           s.append(EOL);
  -        s.append(quoteString(_desc));
  +        s.append(quoteString(desc));
           s.append(EOL);
   
  -        Node n = _head;
  +        Node n = head;
           while (n != null)
           {
               n.toText(s, EOL);
  @@ -572,7 +578,7 @@
   
       public Object[] getRevision()
               throws InvalidFileFormatException,
  -            org.apache.maven.jrcs.diff.DiffException,
  +            DiffException,
               NodeNotFoundException
       {
           return getRevision(false);
  @@ -580,16 +586,16 @@
   
       public Object[] getRevision(boolean annotate)
               throws InvalidFileFormatException,
  -            org.apache.maven.jrcs.diff.DiffException,
  +            DiffException,
               NodeNotFoundException
       {
  -        if (_branch != null)
  +        if (branch != null)
           {
  -            return getRevision(_branch);
  +            return getRevision(branch);
           }
  -        else if (_head != null)
  +        else if (head != null)
           {
  -            return getRevision(_head.version);
  +            return getRevision(head.version);
           }
           else
           {
  @@ -599,7 +605,7 @@
   
       public Object[] getRevision(String v)
               throws InvalidFileFormatException,
  -            org.apache.maven.jrcs.diff.PatchFailedException,
  +            PatchFailedException,
               InvalidVersionNumberException,
               NodeNotFoundException
       {
  @@ -610,14 +616,14 @@
               throws InvalidVersionNumberException,
               NodeNotFoundException,
               InvalidFileFormatException,
  -            org.apache.maven.jrcs.diff.PatchFailedException
  +            PatchFailedException
       {
           return getRevision(new Version(v), annotate);
       }
   
       public Object[] getRevision(Version vernum)
               throws InvalidFileFormatException,
  -            org.apache.maven.jrcs.diff.PatchFailedException,
  +            PatchFailedException,
               NodeNotFoundException
       {
           return getRevision(vernum, false);
  @@ -625,7 +631,7 @@
   
       public Object[] getRevision(Version vernum, boolean annotate)
               throws InvalidFileFormatException,
  -            org.apache.maven.jrcs.diff.PatchFailedException,
  +            PatchFailedException,
               NodeNotFoundException
       {
           Path path = getRevisionPath(vernum);
  @@ -642,23 +648,23 @@
   
       public Version addRevision(Object[] text, String log)
               throws InvalidFileFormatException,
  -            org.apache.maven.jrcs.diff.DiffException,
  +            DiffException,
               InvalidVersionNumberException,
               NodeNotFoundException
       {
  -        if (_branch != null)
  +        if (branch != null)
           {
  -            return addRevision(text, _branch, log);
  +            return addRevision(text, branch, log);
           }
           else
           {
  -            return addRevision(text, _head.version.next(), log);
  +            return addRevision(text, head.version.next(), log);
           }
       }
   
       public Version addRevision(Object[] text, String vernum, String log)
               throws InvalidFileFormatException,
  -            org.apache.maven.jrcs.diff.DiffException,
  +            DiffException,
               InvalidVersionNumberException,
               NodeNotFoundException
       {
  @@ -667,16 +673,16 @@
   
       public Version addRevision(Object[] text, Version vernum, String log)
               throws InvalidFileFormatException,
  -            org.apache.maven.jrcs.diff.DiffException,
  +            DiffException,
               NodeNotFoundException,
               InvalidVersionNumberException
       {
  -        if (_head == null)
  +        if (head == null)
           {
               throw new IllegalStateException("no _head node");
           }
   
  -        Path path = _head.pathTo(vernum, true);
  +        Path path = head.pathTo(vernum, true);
           Node target = path.last();
   
           if (vernum.size() < target.version.size())
  @@ -703,13 +709,13 @@
               vernum = vernum.next();
           }
   
  -        boolean headAdd = (target == _head && !vernum.isBranch());
  +        boolean headAdd = (target == head && !vernum.isBranch());
   
           text = removeKeywords(text);
           String deltaText;
           if (headAdd)
           {
  -            deltaText = Diff.diff(text, _head._text).toRCSString();
  +            deltaText = Diff.diff(text, head._text).toRCSString();
           }
           else
           {
  @@ -724,10 +730,10 @@
           Node newNode = null;
           if (headAdd)
           {
  -            newNode = newNode(vernum, _head);
  +            newNode = newNode(vernum, head);
               newNode.setText(text);
  -            _head.setText(deltaText);
  -            _head = (TrunkNode) newNode;
  +            head.setText(deltaText);
  +            head = (TrunkNode) newNode;
           }
           else
           { // adding a branch node
  @@ -746,65 +752,12 @@
           return newNode.version;
       }
   
  -    protected static final Format Header_FORMAT =
  -            new MessageFormat("Header: {0} {2} {3, date,yyyy/MM/dd HH:mm:ss} {4} 
{5} {6}");
  -    protected static final Format Id_FORMAT =
  -            new MessageFormat("Id: {1} {2} {3, date,yyyy/MM/dd HH:mm:ss} {4} {5} 
{6}");
  -    protected static final Format RCSFile_FORMAT =
  -            new MessageFormat("RCSfile: {1} ");
  -    protected static final Format Revision_FORMAT =
  -            new MessageFormat("Revision: {2} ");
  -    protected static final Format Date_FORMAT =
  -            new MessageFormat("Date: {3, date,yyyy/MM/dd HH:mm:ss} ");
  -    protected static final Format Author_FORMAT =
  -            new MessageFormat("Author: {4} ");
  -    protected static final Format State_FORMAT =
  -            new MessageFormat("State: {5} ");
  -    protected static final Format Locker_FORMAT =
  -            new MessageFormat("Locker: {6} ");
  -    protected static final Format Source_FORMAT =
  -            new MessageFormat("Source: {0} ");
  -
  -    protected static RE Id_re;
  -    protected static RE Header_re;
  -    protected static RE Source_re;
  -    protected static RE RCSfile_re;
  -    protected static RE Revision_re;
  -    protected static RE Date_re;
  -    protected static RE Author_re;
  -    protected static RE State_re;
  -    protected static RE Locker_re;
  -
  -    static void makeRegularExpressions() throws PatchFailedException
  -    {
  -        if (Locker_re != null)
  -        {
  -            return;
  -        }
  -        try
  -        {
  -            Id_re = new RE("\\$Id(:[^\\$]*)?\\$");
  -            Header_re = new RE("\\$Header(:[^\\$]*)?\\$");
  -            Source_re = new RE("\\$Source(:[^\\$]*)?\\$");
  -            RCSfile_re = new RE("\\$RCSfile(:[^\\$]*)?\\$");
  -            Revision_re = new RE("\\$Revision(:[^\\$]*)?\\$");
  -            Date_re = new RE("\\$Date(:[^\\$]*)?\\$");
  -            Author_re = new RE("\\$Author(:[^\\$]*)?\\$");
  -            State_re = new RE("\\$State(:[^\\$]*)?\\$");
  -            Locker_re = new RE("\\$Locker(:[^\\$]*)?\\$");
  -        }
  -        catch (RESyntaxException e)
  -        {
  -            throw new PatchFailedException(e.getMessage());
  -        }
  -    }
  -
       public Object[] doKeywords(Object[] text, Node rev)
  -            throws org.apache.maven.jrcs.diff.PatchFailedException
  +            throws PatchFailedException
       {
           Object[] revisionInfo = new Object[]{
  -            _filename,
  -            new java.io.File(_filename).getName(),
  +            filename,
  +            new File(filename).getName(),
               rev.version.toString(),
               rev._date,
               rev._author,
  @@ -812,47 +765,23 @@
               rev._locker
           };
   
  -        makeRegularExpressions();
           Object[] result = new Object[text.length];
           for (int i = 0; i < text.length; i++)
           {
               String s = text[i].toString();
  -            
  -            s = Id_re.subst(      s, "$" + Id_FORMAT.format(revisionInfo) + "$");
  -            s = Header_re.subst(  s, "$" + Header_FORMAT.format(revisionInfo) + 
"$");
  -            s = Source_re.subst(  s, "$" + Source_FORMAT.format(revisionInfo) + 
"$");
  -            s = RCSfile_re.subst( s, "$" + RCSFile_FORMAT.format(revisionInfo) + 
"$");
  -            s = Revision_re.subst(s, "$" + Revision_FORMAT.format(revisionInfo) + 
"$");
  -            s = Date_re.subst(    s, "$" + Date_FORMAT.format(revisionInfo) + "$");
  -            s = Author_re.subst(  s, "$" + Author_FORMAT.format(revisionInfo) + 
"$");
  -            s = State_re.subst(   s, "$" + State_FORMAT.format(revisionInfo) + "$");
  -            s = Locker_re.subst(  s, "$" + Locker_FORMAT.format(revisionInfo) + 
"$");
  -            //@TODO: should do something about Name and Log
  -            result[i] = s;
  +            result[i] = FORMATTER.format(s, revisionInfo);
           }
           return result;
       }
   
       protected static Object[] removeKeywords(Object[] text)
  -            throws org.apache.maven.jrcs.diff.PatchFailedException
  +            throws PatchFailedException
       {
  -        makeRegularExpressions();
           Object[] result = new Object[text.length];
           for (int i = 0; i < text.length; i++)
           {
               String s = text[i].toString();
  -
  -            s = Id_re.subst(      s, "$" + "Id" + "$");
  -            s = Header_re.subst(  s, "$" + "Header" + "$");
  -            s = Source_re.subst(  s, "$" + "Source" + "$");
  -            s = RCSfile_re.subst( s, "$" + "RCSfile" + "$");
  -            s = Revision_re.subst(s, "$" + "Revision" + "$");
  -            s = Date_re.subst(    s, "$" + "Date" + "$");
  -            s = Author_re.subst(  s, "$" + "Author" + "$");
  -            s = State_re.subst(   s, "$" + "State" + "$");
  -            s = Locker_re.subst(  s, "$" + "Locker" + "$");
  -            //@TODO: should do something about Name and Log
  -            result[i] = s;
  +            result[i] = FORMATTER.reset(s);
           }
           return result;
       }
  
  
  
  1.5       +1 -1      
jakarta-turbine-maven/src/java/org/apache/maven/jrcs/rcs/ArchiveParser.java
  
  Index: ArchiveParser.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-turbine-maven/src/java/org/apache/maven/jrcs/rcs/ArchiveParser.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- ArchiveParser.java        23 Feb 2002 13:52:59 -0000      1.4
  +++ ArchiveParser.java        26 Feb 2002 22:43:15 -0000      1.5
  @@ -198,7 +198,7 @@
                       expand(arc);
                       break;
                   case ID:
  -                    newPhrase(arc._phrases);
  +                    newPhrase(arc.phrases);
                       break;
                   default:
                       jj_consume_token(-1);
  
  
  
  1.1                  
jakarta-turbine-maven/src/java/org/apache/maven/jrcs/rcs/ArchiveFormat.java
  
  Index: ArchiveFormat.java
  ===================================================================
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2002 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Maven" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Maven", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  package org.apache.maven.jrcs.rcs;
  
  import java.text.Format;
  import java.text.MessageFormat;
  
  import org.apache.oro.text.perl.Perl5Util;
  import org.apache.oro.text.regex.Perl5Matcher;
  import org.apache.oro.text.regex.StringSubstitution;
  import org.apache.oro.text.regex.Substitution;
  import org.apache.oro.text.regex.Util;
  import org.apache.oro.text.regex.Pattern;
  import org.apache.oro.text.regex.Perl5Compiler;
  import org.apache.oro.text.regex.MalformedPatternException;
  
  /**
   * Formatter for the Archive. It is intended as an helper class to
   * replace the use of gnu.regexp. This class is NOT threadsafe.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]";>Stephane Bailliez</a>
   */
  final class ArchiveFormat
  {
      final Format Header_FORMAT =
              new MessageFormat("$Header: 
/home/cvs/jakarta-turbine-maven/src/java/org/apache/maven/jrcs/rcs/ArchiveFormat.java,v
 1.1 2002/02/26 22:43:15 sbailliez Exp $");
      final Format Id_FORMAT =
              new MessageFormat("$Id: ArchiveFormat.java,v 1.1 2002/02/26 22:43:15 
sbailliez Exp $");
      final Format RCSFile_FORMAT =
              new MessageFormat("$RCSfile: ArchiveFormat.java,v $");
      final Format Revision_FORMAT =
              new MessageFormat("$Revision: 1.1 $");
      final Format Date_FORMAT =
              new MessageFormat("$Date: 2002/02/26 22:43:15 $");
      final Format Author_FORMAT =
              new MessageFormat("$Author: sbailliez $");
      final Format State_FORMAT =
              new MessageFormat("$State: Exp $");
      final Format Locker_FORMAT =
              new MessageFormat("$Locker:  $");
      final Format Source_FORMAT =
              new MessageFormat("$Source: 
/home/cvs/jakarta-turbine-maven/src/java/org/apache/maven/jrcs/rcs/ArchiveFormat.java,v
 $");
  
      private final Pattern ID_RE;
      private final Pattern HEADER_RE;
      private final Pattern SOURCE_RE;
      private final Pattern RCSFILE_RE;
      private final Pattern REVISION_RE;
      private final Pattern DATE_RE;
      private final Pattern AUTHOR_RE;
      private final Pattern STATE_RE;
      private final Pattern LOCKER_RE;
  
      /** the substitution instance to be reused */
      private final StringSubstitution subst = new StringSubstitution();
  
      ArchiveFormat()
      {
          try
          {
              Perl5Compiler compiler = new Perl5Compiler();
              ID_RE = compiler.compile("\\$Id(:[^\\$]*)?\\$");
              HEADER_RE = compiler.compile("\\$Header(:[^\\$]*)?\\$");
              SOURCE_RE = compiler.compile("\\$Source(:[^\\$]*)?\\$");
              RCSFILE_RE = compiler.compile("\\$RCSfile(:[^\\$]*)?\\$");
              REVISION_RE = compiler.compile("\\$Revision(:[^\\$]*)?\\$");
              DATE_RE = compiler.compile("\\$Date(:[^\\$]*)?\\$");
              AUTHOR_RE = compiler.compile("\\$Author(:[^\\$]*)?\\$");
              STATE_RE = compiler.compile("\\$State(:[^\\$]*)?\\$");
              LOCKER_RE = compiler.compile("\\$Locker(:[^\\$]*)?\\$");
          }
          catch (MalformedPatternException e)
          {
              throw new ExceptionInInitializerError(e);
          }
      }
  
      /**  the matcher used for replacement */
      private final Perl5Matcher matcher = new Perl5Matcher();
  
      /**
       * format the given text made of RCS keywords with the appropriate
       * revision info.
       * @param text the input text containing the RCS keywords.
       * @param revisionInfo the revision information.
       * @return the formatted text with the RCS keywords.
       */
      String format(String text, Object[] revisionInfo)
      {
          String data = text;
          data = substitute(data, ID_RE, Id_FORMAT.format(revisionInfo));
          data = substitute(data, HEADER_RE, Header_FORMAT.format(revisionInfo));
          data = substitute(data, SOURCE_RE, Source_FORMAT.format(revisionInfo));
          data = substitute(data, RCSFILE_RE, RCSFile_FORMAT.format(revisionInfo));
          data = substitute(data, REVISION_RE, Revision_FORMAT.format(revisionInfo));
          data = substitute(data, DATE_RE, Date_FORMAT.format(revisionInfo));
          data = substitute(data, AUTHOR_RE, Author_FORMAT.format(revisionInfo));
          data = substitute(data, STATE_RE, State_FORMAT.format(revisionInfo));
          data = substitute(data, LOCKER_RE, Locker_FORMAT.format(revisionInfo));
          //@TODO: should do something about Name and Log
          return data;
      }
  
      /**
       * Reinitialize all RCS keywords match.
       * @param text the text to look for RCS keywords.
       * @return the text with initialized RCS keywords.
       */
      String reset(String text)
      {
          String data = text;
          data = substitute(data, ID_RE, "$Id: ArchiveFormat.java,v 1.1 2002/02/26 
22:43:15 sbailliez Exp $");
          data = substitute(data, HEADER_RE, "$Header: 
/home/cvs/jakarta-turbine-maven/src/java/org/apache/maven/jrcs/rcs/ArchiveFormat.java,v
 1.1 2002/02/26 22:43:15 sbailliez Exp $");
          data = substitute(data, SOURCE_RE, "$Source: 
/home/cvs/jakarta-turbine-maven/src/java/org/apache/maven/jrcs/rcs/ArchiveFormat.java,v
 $");
          data = substitute(data, RCSFILE_RE, "$RCSfile: ArchiveFormat.java,v $");
          data = substitute(data, REVISION_RE, "$Revision: 1.1 $");
          data = substitute(data, DATE_RE, "$Date: 2002/02/26 22:43:15 $");
          data = substitute(data, AUTHOR_RE, "$Author: sbailliez $");
          data = substitute(data, STATE_RE, "$State: Exp $");
          data = substitute(data, LOCKER_RE, "$Locker:  $");
          //@TODO: should do something about Name and Log
          return data;
      }
  
  
      /**
       * Helper method for substitution that will substitute all matches of
       * a given pattern.
       * @param input the text to look for substitutions.
       * @param pattern the pattern to replace in the input text.
       * @param substitution the string to use as a replacement for the pattern.
       * @return the text with the subsituted value.
       */
      private final String substitute(String input, Pattern pattern, String 
substitution)
      {
          subst.setSubstitution(substitution);
          final String output = Util.substitute(matcher, pattern, subst, input, 
Util.SUBSTITUTE_ALL);
          // no need to keep a reference to the last substitution string
          subst.setSubstitution("");
          return output;
      }
  
  }
  
  
  
  1.1                  
jakarta-turbine-maven/src/test/org/apache/maven/jrcs/rcs/ArchiveFormatTest.java
  
  Index: ArchiveFormatTest.java
  ===================================================================
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2002 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Maven" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Maven", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  package org.apache.maven.jrcs.rcs;
  
  import java.util.Date;
  
  import junit.framework.TestCase;
  
  /**
   * Basic test for the formatter.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]";>Stephane Bailliez</a>
   */
  public class ArchiveFormatTest extends TestCase
  {
      private static final ArchiveFormat FORMATTER = new ArchiveFormat();
      private static final String RCS_KEYWORDS =
              "$Id: ArchiveFormatTest.java,v 1.1 2002/02/26 22:43:15 sbailliez Exp 
$\n" +
              "$Header: 
/home/cvs/jakarta-turbine-maven/src/test/org/apache/maven/jrcs/rcs/ArchiveFormatTest.java,v
 1.1 2002/02/26 22:43:15 sbailliez Exp $\n" +
              "$Source: 
/home/cvs/jakarta-turbine-maven/src/test/org/apache/maven/jrcs/rcs/ArchiveFormatTest.java,v
 $\n" +
              "$RCSfile: ArchiveFormatTest.java,v $\n" +
              "$Revision: 1.1 $\n" +
              "$Date: 2002/02/26 22:43:15 $\n" +
              "$Author: sbailliez $\n" +
              "$State: Exp $\n" +
              "$Locker:  $\n";
      private static final String RCS_CLEAN_KEYWORDS =
              "$Id: ArchiveFormatTest.java,v 1.1 2002/02/26 22:43:15 sbailliez Exp 
$\n" +
              "$Header: 
/home/cvs/jakarta-turbine-maven/src/test/org/apache/maven/jrcs/rcs/ArchiveFormatTest.java,v
 1.1 2002/02/26 22:43:15 sbailliez Exp $\n" +
              "$Source: 
/home/cvs/jakarta-turbine-maven/src/test/org/apache/maven/jrcs/rcs/ArchiveFormatTest.java,v
 $\n" +
              "$RCSfile: ArchiveFormatTest.java,v $\n" +
              "$Revision: 1.1 $\n" +
              "$Date: 2002/02/26 22:43:15 $\n" +
              "$Author: sbailliez $\n" +
              "$State: Exp $\n" +
              "$Locker:  $\n";
  
      private static final Object[] REVISION_INFO = new Object[]{
              "a/b/c/d/File.ext",
              "File.ext",
              "1.1",
              new Date(),
              "theauthor",
              "thestate",
              "thelocker"
          };
  
      private static final String RCS_NOW =
          FORMATTER.Id_FORMAT.format(REVISION_INFO) + "\n" +
          FORMATTER.Header_FORMAT.format(REVISION_INFO) + "\n" +
          FORMATTER.Source_FORMAT.format(REVISION_INFO) + "\n" +
          FORMATTER.RCSFile_FORMAT.format(REVISION_INFO) + "\n" +
          FORMATTER.Revision_FORMAT.format(REVISION_INFO) + "\n" +
          FORMATTER.Date_FORMAT.format(REVISION_INFO) + "\n" +
          FORMATTER.Author_FORMAT.format(REVISION_INFO) + "\n" +
          FORMATTER.State_FORMAT.format(REVISION_INFO) + "\n" +
          FORMATTER.Locker_FORMAT.format(REVISION_INFO) + "\n";
  
      public ArchiveFormatTest(String s)
      {
          super(s);
      }
  
      public void testClean() throws Exception
      {
          String result = FORMATTER.reset(RCS_KEYWORDS);
          assertEquals(RCS_CLEAN_KEYWORDS, result);
      }
  
      public void testFormat() throws Exception
      {
          String result = FORMATTER.format(RCS_KEYWORDS, REVISION_INFO);
          assertEquals(RCS_NOW, result);
      }
  }
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to