
net.time4j.format.ParseLog Maven / Gradle / Ivy
/*
* -----------------------------------------------------------------------
* Copyright © 2013-2014 Meno Hochschild,
* -----------------------------------------------------------------------
* This file (ParseLog.java) is part of project Time4J.
*
* Time4J is free software: You can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 2.1 of the License, or
* (at your option) any later version.
*
* Time4J 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Time4J. If not, see .
* -----------------------------------------------------------------------
*/
package net.time4j.format;
import net.time4j.engine.ChronoEntity;
/**
* Represents a log for the current status and error informations during
* parsing.
*
* Note: This class is not thread-safe. Therefore a new instance
* is to be created per thread (usually per parsing process).
*
* @author Meno Hochschild
* @concurrency
*/
/*[deutsch]
* Zeichnet den Status und Fehlermeldungen beim Parsen auf.
*
* Hinweis: Diese Klasse ist nicht thread-safe, deshalb ist
* pro Thread jeweils eine neue Instanz zu erzeugen (in der Regel pro
* Parse-Vorgang).
*
* @author Meno Hochschild
* @concurrency
*/
public class ParseLog {
//~ Instanzvariablen --------------------------------------------------
private int position;
private int errorIndex;
private String errorMessage;
private ParsedValues rawValues;
private Boolean daylightSaving;
private boolean warning;
//~ Konstruktoren -----------------------------------------------------
/**
* Standard constructor with start position at begin of text.
*/
/*[deutsch]
* Standard-Konstruktor mit der Startposition am Textanfang.
*/
public ParseLog() {
this(0);
}
/**
* Creates a new instance with given start position.
*
* @param offset start position where parsing of text begins
* @throws IllegalArgumentException if the start position is negative
*/
/*[deutsch]
* Konstruiert eine neue Instanz mit der angegebenen Startposition.
*
* @param offset start position where parsing of text begins
* @throws IllegalArgumentException if the start position is negative
*/
public ParseLog(int offset) {
super();
if (offset < 0) {
throw new IllegalArgumentException("Undefined: " + offset);
}
this.position = offset;
this.errorIndex = -1;
this.errorMessage = "";
this.rawValues = null;
this.daylightSaving = null;
this.warning = false;
}
//~ Methoden ----------------------------------------------------------
/**
* Returns the current position of the parser.
*
* @return int ({@code >= 0})
*/
/*[deutsch]
* Gibt die aktuelle Position des Parsers wieder.
*
* @return int ({@code >= 0})
*/
public int getPosition() {
return this.position;
}
/**
* Queries if an error has occurred.
*
* @return boolean
*/
/*[deutsch]
* Ermittelt, ob ein Fehler aufgetreten ist.
*
* @return boolean
*/
public boolean isError() {
return (this.errorIndex != -1);
}
/**
* Returns the position of error in text.
*
* @return int ({@code >= 0} in case of error else {@code -1})
*/
/*[deutsch]
* Gibt die fehlerhafte Stelle im Text an.
*
* @return int ({@code >= 0} in case of error else {@code -1})
*/
public int getErrorIndex() {
return this.errorIndex;
}
/**
* Returns an error message.
*
* @return String (empty if there is no error)
*/
/*[deutsch]
* Gibt eine Fehlerbeschreibung an.
*
* @return String (empty if there is no error)
*/
public String getErrorMessage() {
return this.errorMessage;
}
/**
* Yields the parsed raw data as chronological entity.
*
* @return parsed values as mutable serializable map-like entity
*/
/*[deutsch]
* Liefert die interpretierten Rohdaten.
*
* @return parsed values as mutable serializable map-like entity
*/
public ChronoEntity> getRawValues() {
if (this.rawValues == null) {
this.rawValues = new ParsedValues();
}
return this.rawValues;
}
/**
* Debugging support.
*/
/*[deutsch]
* Debugging-Unterstützung.
*/
@Override
public String toString() {
StringBuilder sb = new StringBuilder(128);
sb.append("[position=");
sb.append(this.position);
sb.append(", error-index=");
sb.append(this.errorIndex);
sb.append(", error-message=\"");
sb.append(this.errorMessage);
sb.append('\"');
if (this.warning) {
sb.append(", warning-active");
}
if (this.rawValues != null) {
sb.append(", raw-values=");
sb.append(this.rawValues);
}
if (this.daylightSaving != null) {
sb.append(", daylight-saving=");
sb.append(this.daylightSaving);
}
sb.append(']');
return sb.toString();
}
/**
* Sets the current position of the parser to given new position.
*
* @param position new parse position ({@code >= 0})
* @throws IllegalArgumentException if given position is negative
*/
/*[deutsch]
* Setzt die aktuelle Position des Parsers neu.
*
* @param position new parse position ({@code >= 0})
* @throws IllegalArgumentException if given position is negative
*/
public void setPosition(int position) {
if (position < 0) {
throw new IllegalArgumentException("Undefined: " + position);
}
this.position = position;
}
/**
* Sets an error information.
*
* @param errorIndex error index in parsed text
* @param errorMessage error message maybe empty
* @throws IllegalArgumentException if given error index is negative
* @since 2.0
*/
/*[deutsch]
* Setzt eine Fehlerinformation.
*
* @param errorIndex error index in parsed text
* @param errorMessage error message maybe empty
* @throws IllegalArgumentException if given error index is negative
* @since 2.0
*/
public void setError(
int errorIndex,
String errorMessage
) {
if (errorIndex >= 0) {
this.errorMessage = (
((errorMessage == null) || errorMessage.isEmpty())
? ("Error occurred at position: " + errorIndex)
: errorMessage);
} else {
throw new IllegalArgumentException("Undefined: " + errorIndex);
}
this.errorIndex = errorIndex;
}
/**
* Sets a warning to indicate if the current formatter should try
* to use default values for chronological elements which could not
* be parsed.
*
* If there is no error present then an unspecific error message
* will be created, too. Only customized {@code ChronoParser}-objects
* might need to call this method.
*
* @since 2.0
* @see ChronoParser
*/
/*[deutsch]
* Setzt eine Warnung, um anzuzeigen, daß der aktuelle
* Formatierer versuchen sollte, Standardwerte für chronologische
* Elemente zu verwenden, die nicht interpretiert werden konnten.
*
* Wenn kein Fehler gesetzt ist, dann wird automatisch eine
* unspezifizierte Fehlermeldung generiert. Nur spezielle
* {@code ChronoParser}-Objekte rufen diese Methode bei Bedarf auf.
*
* @since 2.0
* @see ChronoParser
*/
public void setWarning() {
if (!this.isError()) {
this.errorMessage = "Warning state active.";
this.errorIndex = this.getPosition();
}
this.warning = true;
}
/**
* Reuses this instance for next parse process.
*/
/*[deutsch]
* Bereitet diese Instanz auf die Wiederverwendung für einen
* neuen Interpretierungsvorgang vor.
*/
public void reset() {
this.position = 0;
this.clearError();
this.clearWarning();
this.rawValues = null;
this.daylightSaving = null;
}
/**
* Interne Methode.
*
* @return parsed values, initially {@code null}
*/
ParsedValues getRawValues0() {
return this.rawValues;
}
/**
* Löscht eine eventuell vorhandene Fehlerinformation.
*/
void clearError() {
this.errorIndex = -1;
this.errorMessage = "";
}
/**
* Setzt die interpretierten Rohdaten.
*
* @param rawValues parsed values
*/
void setRawValues(ParsedValues rawValues) {
this.rawValues = rawValues;
}
/**
* Ein Zeitzonenname wurde als daylightSaving erkannt.
*/
void setDaylightSaving(boolean dst) {
this.daylightSaving = Boolean.valueOf(dst);
}
/**
* Wurde eine Sommer- oder Winterzeitform als Zeitzonenname gelesen?
*
* @return {@code Boolean.TRUE} wenn Sommerzeit, {@code Boolean.FALSE}
* wenn Winterzeit (Normalzeit), sonst {@code null}
*/
Boolean getDSTInfo() {
return this.daylightSaving;
}
/**
* Ermittelt, ob eine Warnung gesetzt wurde.
*
* @return boolean
*/
boolean isWarning() {
return this.warning;
}
/**
* Entfernt eine Warnung.
*/
void clearWarning() {
this.warning = false;
}
}