Java APG, Version 1.0
Author Lowell D. Thomas
Copyright © Coast to Coast Research, Inc. 2011

com.coasttocoastresearch.apg
Class Utilities

java.lang.Object
  extended by com.coasttocoastresearch.apg.Utilities

public class Utilities
extends java.lang.Object

The Utilities class defines several helper classes and static functions. These are stand-alone classes and functions that have general utility beyond the needs of any single class.


Nested Class Summary
static class Utilities.LineCatalog
          A class for reading a text file and cataloging its text lines.
static class Utilities.XMLWriter
          A class for assisting in the writing of XML files.
 
Method Summary
static java.lang.String charArrayToString(char[] input, int offset, int length, int maxChars)
          Converts a subset of a character array to a String representation.
static java.lang.String charArrayToXml(char[] input, int offset, int length)
          Converts a subset of a character array to XML string format.
static java.lang.String displayError(java.lang.Error e)
          Get the Error message and stack trace as a String.
static java.lang.String displayException(java.lang.Exception e)
          Get the Exception message and stack trace as a String.
static void displayFileInfo(java.io.PrintStream out, java.io.File file)
          Displays detailed information on File.
static void displayFileInfo(java.io.PrintStream out, java.lang.String name)
          Displays detailed information on the named file.
static java.io.File getFile(java.lang.String dir, java.lang.String filename)
          Return a File object for the named file.
static java.lang.String getFileAsString(java.lang.String fileName)
          Reads the named file and returns its contents as a String.
static java.lang.String getFileAsString(java.lang.String workingDir, java.lang.String fileName)
          Reads the named file and returns its contents as a String.
static java.lang.String indent(int length)
          Generates a string of spaces (0x20) which is often used to provide an indent to a line of text.
static java.lang.String parserStateToString(boolean down, boolean match, int length)
          Converts the parser state to a String representation.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

charArrayToString

public static java.lang.String charArrayToString(char[] input,
                                                 int offset,
                                                 int length,
                                                 int maxChars)
Converts a subset of a character array to a String representation. Non-printing characters (not in the range 32-126) are displayed as hex values (eg. x1F)

Parameters:
input - the character array to convert
offset - offset of the first character in the array subset to convert.
length - the number of characters in the array subset.
maxChars - imposes a maximum character length limit on the subset. If length is greater than maxChars, the subset will be truncated.
Returns:
the String representation of the character array subset.

charArrayToXml

public static java.lang.String charArrayToXml(char[] input,
                                              int offset,
                                              int length)
Converts a subset of a character array to XML string format.

Parameters:
input - the character array to convert
offset - offset of the first character in the array subset to convert.
length - the number of characters in the array subset.
Returns:
the XML representation of the character array subset.

displayError

public static java.lang.String displayError(java.lang.Error e)
Get the Error message and stack trace as a String.

Parameters:
e - the Error to display
Returns:
the Error message and stack trace as a String.

displayException

public static java.lang.String displayException(java.lang.Exception e)
Get the Exception message and stack trace as a String.

Parameters:
e - the Exception to display
Returns:
the Exception message and stack trace as a String.

displayFileInfo

public static void displayFileInfo(java.io.PrintStream out,
                                   java.io.File file)
                            throws java.io.IOException
Displays detailed information on File.

Parameters:
out - the output device to display on.
file - the File to get information on.
Throws:
java.io.IOException - thrown if the file cannot be found or opened.

displayFileInfo

public static void displayFileInfo(java.io.PrintStream out,
                                   java.lang.String name)
                            throws java.io.IOException
Displays detailed information on the named file.

Parameters:
out - the output device to display on.
name - the name of the file to get information on.
Throws:
java.io.IOException - thrown if the file cannot be found or opened.

getFile

public static java.io.File getFile(java.lang.String dir,
                                   java.lang.String filename)
Return a File object for the named file.

Parameters:
dir - directory to find the file in.
filename - name of the file to find.
Returns:
the File object or null if not the file could not be found

getFileAsString

public static java.lang.String getFileAsString(java.lang.String fileName)
                                        throws java.lang.NullPointerException,
                                               java.io.FileNotFoundException,
                                               java.io.IOException,
                                               java.lang.Exception
Reads the named file and returns its contents as a String.

Parameters:
fileName - name of the file to open and read.
Returns:
text of the file's contents.
Throws:
java.lang.NullPointerException - File
java.io.FileNotFoundException - File
java.io.IOException - File
java.lang.Exception - thrown if the named file does not exist or for some reason cannot be opened.

getFileAsString

public static java.lang.String getFileAsString(java.lang.String workingDir,
                                               java.lang.String fileName)
                                        throws java.lang.NullPointerException,
                                               java.io.FileNotFoundException,
                                               java.io.IOException,
                                               java.lang.Exception
Reads the named file and returns its contents as a String.

Parameters:
fileName - name of the file to open and read.
workingDir - working directory to find "fileName" in.
Returns:
text of the file's contents.
Throws:
java.lang.NullPointerException - File
java.io.FileNotFoundException - File
java.io.IOException - File
java.lang.Exception - thrown if the named file does not exist or for some reason cannot be opened.

indent

public static java.lang.String indent(int length)
Generates a string of spaces (0x20) which is often used to provide an indent to a line of text.

Parameters:
length - the number of spaces in the indent string.
Returns:
a String of "length" spaces

parserStateToString

public static java.lang.String parserStateToString(boolean down,
                                                   boolean match,
                                                   int length)
Converts the parser state to a String representation. Used in the formatting of trace and AST displays.

Parameters:
down - if true, the parser direction is "down" the syntax tree - before to parsing the branch below. If false, the direction is "up" after visiting the branch below.
match - if true, the phrase was matched (may be empty)
length - if match is true, the length of the matched phrase. Zero (0) length indicates an empty phrase matched.
Returns:
the String representation of the state.

Java APG, Version 1.0
Author Lowell D. Thomas
Copyright © Coast to Coast Research, Inc. 2011

Licence Notification

All the software in this distribution is free software:
you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program in the COPYING file. If not, see GPL, Version 2
or GPL, Version 3 or write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.