cds.astro
Class Parsing

java.lang.Object
  extended by cds.astro.Astroformat
      extended by cds.astro.Parsing

public class Parsing
extends Astroformat

This class interprets text and is able to convert to numbers, or find a symbol in a list of symbols. Numbers can use ...×10+/-exp as well as ...e+/-exp notation; numbers may also be expressed in sexagesimal.

This class returns, for the most recently analyzed number:

Version:
0.7 : 07-Sep-2002 inside Unit class
, 1.0 : 28-Feb-2004 separated Udef class, 1.1 : 10-Aug-2004 Homogenize Parsing + Editing classes, 1.2 : 02-Sep-2006 Interpret dates, 1.3 : 26-Oct-2006 Accept blanks after +/-; added matchingQuote and toString(int len)
Author:
Francois Ochsenbein -- francois@astro.u-strasbg.fr

Field Summary
 char[] a
          The text as an array of chars
static int DEBUG
          Debugging level
static int ERROR
          The last (sexagesimal) parsing has a component greater than 60
 java.lang.String error_message
          Error message, if any (only in parseComplex)
 int length
          The length of the array, can be shorter than the actual size
static int OK
          The status of the last parsing operation is OK
 int pos
          The current position in the array
static int WARNING
          The last (sexagesimal) parsing has minutes or seconds equal to 60.
 
Fields inherited from class cds.astro.Astroformat
DATE, DATE_alpha, DATE_COMP, DATE_DIFF, DATE_DMY, DATE_DY, DATE_MDY, DATE_MY, DATE_MYD, DATE_YD, DATE_YM, DATE_YMD, DECIMAL, EFORMAT, FACTOR, month_list, nulls, SEXA1d, SEXA1h, SEXA1o, SEXA2, SEXA2c, SEXA2d, SEXA2h, SEXA2o, SEXA3, SEXA3c, SEXA3d, SEXA3h, SEXA3o, SIGN_EDIT, TRUNCATE, ZERO_FILL
 
Constructor Summary
Parsing(java.lang.String s)
          Create a Parsing unit from a string
Parsing(java.lang.String s, int offset)
          Create a Parsing unit from a string + offset
 
Method Summary
 void advance(int n)
          Move (forward / backward) in the Parsing piece
 char currentChar()
          Get the current char
 int decimals()
          Retrieves the number of decimals of the last parsed number.
 int digits()
          Retrieves the number of significant digits of the last parsed number.
 java.lang.String form()
          Retrieves the 'format' of the last parsed number as a string.
 int format()
          Retrieves the 'format' of the last parsed number.
 java.lang.String getMessage()
          About the last parsed number: get the error message
 void gobbleSpaces()
          Skip the spaces in the Parsing unit
 boolean inError()
          About the last parsed number: was it in error ?
 boolean isAngle()
          About the last parsed number: was it expressed as angle (° or d)
 boolean isDate()
          About the last parsed number: was it expressed as a date ?
 boolean isDays()
          About the last parsed number: does it represent days (date_diff) ?
 boolean isSexa()
          About the last parsed number: was it sexagesimal ?
 boolean isTime()
          About the last parsed number: was it expressed as time (hms) ?
 int lookup(char[] tSymbol)
          Try to match a Character from a list.
 int lookup(java.lang.String[] tSymbol)
          Try to match a Symbol in the current text.
 int lookup(java.lang.String[] tSymbol, int len)
          Try to match a Symbol in a defined piece of the text.
 int lookupIgnoreCase(java.lang.String[] tSymbol)
          Try to match a Symbol in the current text, case insensitive.
 boolean match(char c)
          Verify we're starting by a specific character
 boolean match(java.lang.String text)
          Verify we're starting by a specific string.
 int matchingBracket()
          From current position assumed to contain a parenthesis or bracket, return the location of the corresponding parenthesis.
 int matchingQuote()
          From current position assumed to contain a Quote (' " or `) return location of the matching quote.
 int parseArray(double[] vec)
          Interpret an array (several numbers).
 double parseComplex(java.lang.String pic)
          Interpret some Complex number (date, sexagesimal).
 int parseDate()
          Interpret a Date.
 double parseDecimal()
          Interpret a real number as (+/-)num.decimals.
 double parseDouble()
          Interpret a real number as (+/-)numE+/-pow.
 double parseFactor()
          Interpret a real number as (+/-)numx10+pow OR (+/-)numE+pow.
 int parseInt()
          Try to match an Integer Number.
 long parseLong()
          Try to match a Long Integer Number.
 boolean parseNaN()
          Try to match one of the possible NaN representations.
 int parsePositiveInt()
          Try to match a Positive Integer Number.
 double parseSexa()
          Interpret a sexagesimal number.
 int parseWithError(double[] vec)
          Interpret a number + Error.
 void set(int n)
          Force the position within the Parsing piece
 void set(java.lang.String text)
          Install a new text in the Parsing
 int status()
          Retrieves the status (OK, WARNING, ERROR) of last (sexagesimal) parsing.
 java.lang.String toString()
          View the Parsing as a String
 java.lang.String toString(int len)
          View a part of the Parsing as a String.
 
Methods inherited from class cds.astro.Astroformat
acceptAsNaN, explain, explainComplex, isAngle, isDate, isDays, isSexa, isTime, setNaN
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

a

public char[] a
The text as an array of chars


length

public int length
The length of the array, can be shorter than the actual size


pos

public int pos
The current position in the array


error_message

public java.lang.String error_message
Error message, if any (only in parseComplex)


OK

public static final int OK
The status of the last parsing operation is OK

See Also:
Constant Field Values

WARNING

public static final int WARNING
The last (sexagesimal) parsing has minutes or seconds equal to 60.

See Also:
Constant Field Values

ERROR

public static final int ERROR
The last (sexagesimal) parsing has a component greater than 60

See Also:
Constant Field Values

DEBUG

public static int DEBUG
Debugging level

Constructor Detail

Parsing

public Parsing(java.lang.String s)
Create a Parsing unit from a string

Parameters:
s - the text to parse

Parsing

public Parsing(java.lang.String s,
               int offset)
Create a Parsing unit from a string + offset

Parameters:
s - the text to parse
Method Detail

set

public final void set(int n)
Force the position within the Parsing piece

Parameters:
n - the position -- if necessary, adjusted between 0 and length

set

public final void set(java.lang.String text)
Install a new text in the Parsing

Parameters:
text - the string to parse

advance

public final void advance(int n)
Move (forward / backward) in the Parsing piece

Parameters:
n - the value of the step

currentChar

public final char currentChar()
Get the current char

Returns:
the current character in the parsing buffer.

gobbleSpaces

public final void gobbleSpaces()
Skip the spaces in the Parsing unit


lookup

public final int lookup(char[] tSymbol)
Try to match a Character from a list.

Parameters:
tSymbol - table of Symbols
Returns:
the index in table of Symbols (-1 if not found)

match

public final boolean match(char c)
Verify we're starting by a specific character

Parameters:
c - the character to match, possibly preceded by blanks.
Returns:
true (position changed)/false

match

public final boolean match(java.lang.String text)
Verify we're starting by a specific string. Note that an, empty string will always match...

Parameters:
text - the string that should be there.
Returns:
true (position changed)/false

lookup

public final int lookup(java.lang.String[] tSymbol,
                        int len)
Try to match a Symbol in a defined piece of the text. The matching is successful when a symbol of the table with the specified length is matched.

Parameters:
tSymbol - table of Symbols
len - exact length of text to match
Returns:
the index in table of Symbols (-1 if not found)

lookup

public final int lookup(java.lang.String[] tSymbol)
Try to match a Symbol in the current text. The matching is successful with the first symbol of the list that coincides -- and the position is incremented.

Parameters:
tSymbol - a table of Symbols
Returns:
the index in table of Symbols (-1 if not found)

lookupIgnoreCase

public final int lookupIgnoreCase(java.lang.String[] tSymbol)
Try to match a Symbol in the current text, case insensitive.

Parameters:
tSymbol - table of Symbols
Returns:
the index in table of Symbols (-1 if not found)

matchingBracket

public final int matchingBracket()
From current position assumed to contain a parenthesis or bracket, return the location of the corresponding parenthesis. No mouvement in the Parsing structure (pos does not change)

Returns:
the position / -1 if not found

matchingQuote

public final int matchingQuote()
From current position assumed to contain a Quote (' " or `) return location of the matching quote. The next character identical to the current one is searched. No quote escaping allowed. No mouvement in the Parsing structure (pos does not change) Added in V1.3

Returns:
the position / -1 if not found.

parseNaN

public final boolean parseNaN()
Try to match one of the possible NaN representations. No change is made in pos when no NULL representation could be matched.

Returns:
true (a NaN representation found) / false

parsePositiveInt

public final int parsePositiveInt()
Try to match a Positive Integer Number. No change is made in pos when no number could be matched;

Returns:
the integer read -- 0 by default, 0x80000000 for NaN

parseInt

public final int parseInt()
Try to match an Integer Number. No change is made in pos when no number could be matched;

Returns:
the integer read -- 0 by default, 0x80000000 for NaN

parseLong

public final long parseLong()
Try to match a Long Integer Number. No change is made in pos when no number could be matched;

Returns:
the integer read -- 0 by default, 0x80000000 for NaN

parseDecimal

public final double parseDecimal()
Interpret a real number as (+/-)num.decimals. No change occurs in pos when no number could be matched, which can be tested to verify that a number was actually matched.

Returns:
the double which could be interpreted (NaN if no match)

parseDouble

public final double parseDouble()
Interpret a real number as (+/-)numE+/-pow. No change occurs in pos when no number could be matched, which can be tested to verify that a number was actually matched.

Returns:
the double which could be interpreted (NaN if no match)

parseFactor

public final double parseFactor()
Interpret a real number as (+/-)numx10+pow OR (+/-)numE+pow. No change occurs in the pos when no number could be matched, which can be tested to verify that a number was actually matched.

Returns:
the double which could be interpreted -- 1 by default.

parseSexa

public final double parseSexa()
Interpret a sexagesimal number. no change occurs in the pos when no number could be matched, which can be tested to verify that a number was actually matched. An error is stored when a component is outside [0,60[. The exponent is accepted.

Returns:
the double which could be interpreted -- NaN by default.

parseDate

public final int parseDate()
Interpret a Date. No change occurs in the pos when date (Y-M-D) could not be matched. On return, indications about format.

Returns:
a date in MJD (Modified Julian Date = JD-2400000.5, or number of days elapsed since 17 Nov 1858 UTC).

parseComplex

public final double parseComplex(java.lang.String pic)
                          throws java.text.ParseException
Interpret some Complex number (date, sexagesimal). On return, a date / angle / time

Parameters:
pic - "picture" which specifies the format of the input. Letters are Y y (years) M (month) D (day) h (hour) m (minutes) s (seconds) d (degrees) f (fractions), and punctuations like : (colon) / (slash), etc. Y and y have slightly different meanings when the century is omitted (2-digit years): YY have their offset in 1900, while yy assumes dates between 1950 and 2049: e.g. 01 means 1901 with Y and 2001 with y

The text must follow exactly the "picture" when the components are specified with several letters. For instance, a picture YYYY-MM-DD indicates that a conforming text must be made of 4 digits representing the year, followed by a dash, 2 digits representing the month, a dash, and 2 digits representing the day -- while Y-M-D accept a variable number of digits in each of the components: "2006-7-5" would conform to the latter "picture", but not to the "YYYY-MM-DD" one.

Returns:
a date, time or angle. Date is expressed in MJD (days elapsed since 17 Nov 1858 UTC), time in days or hours, angles in degrees. The functions Astroformat.isDate(int), Astroformat.isTime(int), Astroformat.isDate(int), Astroformat.isDays(int) specify the actual interpretation done in the parsing.
Throws:
java.text.ParseException - for invalid picture or non-conforming data. The error_message of the exception starts by:
"++++" when upper/lower case were mixed;
"****" for terminal error, and contains ((pic)) for invalid arg

decimals

public final int decimals()
Retrieves the number of decimals of the last parsed number. For a sexagesimal representation, the minutes and seconds count each for 2 decimals, i.e. 00:01:02 has 4 decimals.

Returns:
number of decimals, -1 if no decimal point exists.

digits

public final int digits()
Retrieves the number of significant digits of the last parsed number.

Returns:
number of significant digits -- 0 meaning an emtpy (NaN) number.

format

public final int format()
Retrieves the 'format' of the last parsed number.

Returns:
One of DECIMAL, EFORMAT, FACTOR, SEXA2, SEXA3, SEXA2c, SEXA3c, SEXA2d, SAX2h, SEXA3d, SEXA3h, DATE possibly combined with ZERO_FILL, SIGN_EDIT
See Also:
Astroformat

form

public final java.lang.String form()
Retrieves the 'format' of the last parsed number as a string.

Returns:
A printable variant of format.

isSexa

public final boolean isSexa()
About the last parsed number: was it sexagesimal ?

Returns:
true if was expressed in sexagesimal

isDate

public final boolean isDate()
About the last parsed number: was it expressed as a date ?

Returns:
true if parsed number contained 'h' and/or 'm' 's'

isDays

public final boolean isDays()
About the last parsed number: does it represent days (date_diff) ?

Returns:
true if parsed number expressed time in days.

isTime

public final boolean isTime()
About the last parsed number: was it expressed as time (hms) ?

Returns:
true if parsed number contained 'h' and/or 'm' 's'

isAngle

public final boolean isAngle()
About the last parsed number: was it expressed as angle (° or d)

Returns:
true if parsed number contained '°' and/or ' "

inError

public final boolean inError()
About the last parsed number: was it in error ?

Returns:
true if parsed number contained '°' and/or ' "

status

public final int status()
Retrieves the status (OK, WARNING, ERROR) of last (sexagesimal) parsing.

Returns:
One of OK, WARNING, ERROR

getMessage

public final java.lang.String getMessage()
About the last parsed number: get the error message

Returns:
The error message, null when no error.

parseArray

public int parseArray(double[] vec)
Interpret an array (several numbers).

Parameters:
vec - Array containing on output the values parsed.
Returns:
number of numbers found.

parseWithError

public int parseWithError(double[] vec)
Interpret a number + Error. Text to interpret. May be "300+23-25" or "300[23]" or "300+/-23" or "300+/-23"

Parameters:
vec - Array containing value PositiveError NegativeError
Returns:
number of numbers found (0, 1, 2 or 3). When 0, the position was not changed.

toString

public final java.lang.String toString()
View the Parsing as a String

Overrides:
toString in class java.lang.Object
Returns:
the not-yet-parsed part of the string.

toString

public final java.lang.String toString(int len)
View a part of the Parsing as a String. Added in V1.3

Parameters:
len - Length to keep (truncated if necessary)
Returns:
the not-yet-parsed part of the string.


Copyright © 2009 UDS/CNRS