View Javadoc

1   /*
2    * ModeShape (http://www.modeshape.org)
3    * See the COPYRIGHT.txt file distributed with this work for information
4    * regarding copyright ownership.  Some portions may be licensed
5    * to Red Hat, Inc. under one or more contributor license agreements.
6    * See the AUTHORS.txt file in the distribution for a full listing of 
7    * individual contributors. 
8    *
9    * ModeShape is free software. Unless otherwise indicated, all code in ModeShape
10   * is licensed to you under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation; either version 2.1 of
12   * the License, or (at your option) any later version.
13   *
14   * ModeShape is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this software; if not, write to the Free
21   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23   */
24  package org.modeshape.sequencer.java;
25  
26  import java.io.IOException;
27  import java.io.InputStream;
28  import org.eclipse.jdt.core.dom.Name;
29  import org.eclipse.jdt.internal.compiler.util.Util;
30  import org.modeshape.common.util.CheckArg;
31  
32  /**
33   * Utility class for working with metadata.
34   */
35  public class JavaMetadataUtil {
36      /**
37       * Get the length of the input stream.
38       * 
39       * @param stream - the <code>InputStream</code>
40       * @return the length of the stream.
41       * @throws IOException - exceptional situation during calculating the length.
42       */
43      public static long length( InputStream stream ) throws IOException {
44          return stream.available();
45      }
46  
47      /**
48       * Gets Java source from the <code>InputStream</code>.
49       * 
50       * @param inputStream - the <code>FileInputStream</code>.
51       * @param length - the length of the java file.
52       * @param encoding - the encoding of the source, if there is one.
53       * @return the array character of the java source.
54       * @throws IOException - exceptional error can be thrown during the reading of the file.
55       */
56      public static char[] getJavaSourceFromTheInputStream( InputStream inputStream,
57                                                            long length,
58                                                            String encoding ) throws IOException {
59          char[] source = Util.getInputStreamAsCharArray(inputStream, (int)length, encoding);
60          return source;
61      }
62  
63      /**
64       * Get the fully qualified name from the <code>Name</code>.
65       * 
66       * @param name - the name to process.
67       * @return a FQN of the name.
68       */
69      public static String getName( Name name ) {
70          CheckArg.isNotNull(name, "name");
71          return name.getFullyQualifiedName();
72      }
73  
74      /**
75       * Create a path for the tree with index.
76       * 
77       * @param path the path.
78       * @param index the index begin with 1.
79       * @return the string
80       * @throws IllegalArgumentException if the path is null, blank or empty, or if the index is not a positive value
81       */
82      public static String createPathWithIndex( String path,
83                                                int index ) {
84          CheckArg.isNotEmpty(path, "path");
85          CheckArg.isPositive(index, "index");
86          return path + "[" + index + "]";
87      }
88  
89      /**
90       * Create a path for the tree without index.
91       * 
92       * @param path - the path.
93       * @return the string
94       * @throws IllegalArgumentException if the path is null, blank or empty
95       */
96      public static String createPath( String path ) {
97          CheckArg.isNotEmpty(path, "path");
98          return path;
99      }
100 
101     // prevent construction
102     private JavaMetadataUtil() {
103     }
104 }