View Javadoc
1   package org.apache.maven.jxr.pacman;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.FileInputStream;
23  import java.io.FileReader;
24  import java.io.IOException;
25  import java.io.InputStreamReader;
26  import java.io.Reader;
27  import java.io.StreamTokenizer;
28  import java.nio.file.Files;
29  import java.nio.file.Path;
30  
31  /**
32   * PacMan implementation of a JavaFile. This will parse out the file and
33   * determine package, class, and imports
34   *
35   * @author <a href="mailto:burton@apache.org">Kevin A. Burton</a>
36   * @version $Id$
37   */
38  public class JavaFileImpl
39      extends JavaFile
40  {
41      /**
42       * Create a new JavaFileImpl that points to a given file...
43       *
44       * @param path
45       * @param encoding
46       * @throws IOException
47       */
48      public JavaFileImpl( Path path, String encoding )
49          throws IOException
50      {
51          super( path, encoding );
52  
53          //always add java.lang.* to the package imports because the JVM always
54          //does this implicitly.  Unless we add this to the ImportTypes JXR
55          //won't pick up on this.
56          this.addImportType( new ImportType( "java.lang.*" ) );
57  
58          //now parse out this file.
59          this.parse();
60      }
61  
62      /**
63       * Open up the file and try to determine package, class and import
64       * statements.
65       */
66      private void parse()
67          throws IOException
68      {
69          StreamTokenizer stok = null;
70          try ( Reader reader = getReader() )
71          {
72              stok = this.getTokenizer( reader );
73  
74              parseRecursive( "", stok );
75          }
76          finally
77          {
78              stok = null;
79          }
80      }
81  
82      private void parseRecursive( String nestedPrefix, StreamTokenizer stok )
83              throws IOException
84      {
85          int openBracesCount = 0;
86  
87          char prevttype = Character.MIN_VALUE; // previous token type
88          boolean inTripleQuote = false; // used to toggle between inside/outside triple-quoted multi-line strings
89  
90          while ( stok.nextToken() != StreamTokenizer.TT_EOF )
91          {
92  
93              if ( stok.sval == null )
94              {
95                  if ( stok.ttype == '{' )
96                  {
97                      openBracesCount++;
98                  }
99                  else if ( stok.ttype == '}' )
100                 {
101                     if ( --openBracesCount == 0 )
102                     {
103                         // break out of recursive
104                         return;
105                     }
106                 }
107                 continue;
108             }
109             else
110             {
111                 if ( '"' == stok.ttype && '"' == prevttype )
112                 {
113                     inTripleQuote = !inTripleQuote;
114                 }
115                 prevttype = (char) stok.ttype;
116                 if ( inTripleQuote )
117                 {
118                     // skip content found inside triple-quoted multi-line Java 15 String
119                     continue;
120                 }
121             }
122 
123             //set the package
124             if ( "package".equals( stok.sval ) && stok.ttype != '\"' )
125             {
126                 stok.nextToken();
127                 if ( stok.sval != null )
128                 {
129                     this.setPackageType( new PackageType( stok.sval ) );
130                 }
131             }
132 
133             //set the imports
134             if ( "import".equals( stok.sval )  && stok.ttype != '\"' )
135             {
136                 stok.nextToken();
137 
138                 String name = stok.sval;
139 
140                 /*
141                 WARNING: this is a bug/non-feature in the current
142                 StreamTokenizer.  We needed to set the comment char as "*"
143                 and packages that are imported with this (ex "test.*") will be
144                 stripped( and become "test." ).  Here we need to test for this
145                 and if necessary re-add the char.
146                 */
147                 if ( name != null )
148                 {
149                     if ( name.charAt( name.length() - 1 ) == '.' )
150                     {
151                         name = name + '*';
152                     }
153                     this.addImportType( new ImportType( name ) );
154                 }
155             }
156 
157             // Add the class or classes. There can be several classes in one file so
158             // continue with the while loop to get them all.
159             if ( ( "class".equals( stok.sval ) || "interface".equals( stok.sval ) || "enum".equals( stok.sval ) )
160                     &&  stok.ttype != '"' )
161             {
162                 stok.nextToken();
163                 if ( stok.sval != null )
164                 {
165                     this.addClassType( new ClassType( nestedPrefix + stok.sval,
166                             getFilenameWithoutPathOrExtension( this.getPath() ) ) );
167                     parseRecursive( nestedPrefix + stok.sval + ".", stok );
168                 }
169             }
170 
171         }
172     }
173 
174     /**
175      * Get a StreamTokenizer for this file.
176      */
177     private StreamTokenizer getTokenizer( Reader reader )
178     {
179         StreamTokenizer stok = new StreamTokenizer( reader );
180         //int tok;
181 
182         stok.commentChar( '*' );
183         stok.wordChars( '_', '_' );
184 
185         // set tokenizer to skip comments
186         stok.slashStarComments( true );
187         stok.slashSlashComments( true );
188 
189         return stok;
190     }
191     
192     private Reader getReader()
193         throws IOException
194     {
195         if ( Files.notExists( this.getPath() ) )
196         {
197             throw new IOException( this.getPath() + " does not exist!" );
198         }
199 
200         if ( this.getEncoding() != null )
201         {
202             return new InputStreamReader( new FileInputStream( this.getPath().toFile() ), this.getEncoding() );
203         }
204         else
205         {
206             return new FileReader( this.getPath().toFile() );
207         }
208     }    
209 }