KeyValue.java

  1. /* Copyright 2002-2013 CS Systèmes d'Information
  2.  * Licensed to CS Systèmes d'Information (CS) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * CS licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *   http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.orekit.files.ccsds;

  18. import java.util.regex.Matcher;
  19. import java.util.regex.Pattern;

  20. import org.orekit.errors.OrekitException;
  21. import org.orekit.errors.OrekitMessages;

  22. /** Holder for key-value pair.
  23.  * <p>
  24.  * The syntax for key-value lines in CCSDS files is:
  25.  * </p>
  26.  * <pre>
  27.  * KEY = value [unit]
  28.  * </pre>
  29.  * <p>
  30.  * The "[unit]" part (with the square brackets included) is optional.
  31.  * The COMMENT keyword is an exception and does not have an '=' but directly
  32.  * the value as free form text. The META_START, META_STOP, COVARIANCE_START
  33.  * and COVARIANCE_STOP keywords are other exception and do not have anything
  34.  * else following them on the line.
  35.  * </p>
  36.  * @author Luc Maisonobe
  37.  * @since 6.1
  38.  */
  39. class KeyValue {

  40.     /** Regular expression for splitting lines. */
  41.     private final Pattern PATTERN =
  42.             Pattern.compile("\\p{Space}*([A-Z][A-Z_0-9]*)\\p{Space}*=?\\p{Space}*(.*?)\\p{Space}*(?:\\[.*\\])?");

  43.     /** Regular expression for user defined keywords. */
  44.     private final Pattern USER_DEFINED_KEYWORDS =
  45.             Pattern.compile("USER_DEFINED_[A-Z][A-Z_]*");

  46.     /** Line from which pair is extracted. */
  47.     private final String line;

  48.     /** Number of the line from which pair is extracted. */
  49.     private final int lineNumber;

  50.     /** Name of the file. */
  51.     private final String fileName;

  52.     /** Keyword enum corresponding to parsed key. */
  53.     private final Keyword keyword;

  54.     /** Key part of the pair. */
  55.     private final String key;

  56.     /** Value part of the line. */
  57.     private final String value;

  58.     /** Build a pair by splitting a key-value line.
  59.      * <p>
  60.      * The splitting is very basic and only extracts words using a regular
  61.      * expression ignoring the '=' sign and the optional unit. No attempt
  62.      * is made to recognize the special keywords. The key and value parts
  63.      * may be empty if not matched, and the keyword may be null.
  64.      * </p>
  65.      * @param line to split
  66.      * @param lineNumber number of the line in the CCSDS data message
  67.      * @param fileName name of the file
  68.      */
  69.     public KeyValue(final String line, final int lineNumber, final String fileName) {

  70.         this.line       = line;
  71.         this.lineNumber = lineNumber;
  72.         this.fileName   = fileName;

  73.         final Matcher matcher = PATTERN.matcher(line);
  74.         if (matcher.matches()) {
  75.             key   = matcher.group(1);
  76.             value = matcher.group(2);
  77.             Keyword recognized;
  78.             try {
  79.                 recognized = Keyword.valueOf(key);
  80.             } catch (IllegalArgumentException iae) {
  81.                 if (USER_DEFINED_KEYWORDS.matcher(key).matches()) {
  82.                     recognized = Keyword.USER_DEFINED_X;
  83.                 } else {
  84.                     recognized = null;
  85.                 }
  86.             }
  87.             keyword = recognized;
  88.         } else {
  89.             key     = "";
  90.             value   = key;
  91.             keyword = null;
  92.         }
  93.     }

  94.     /** Keyword corresponding to the parsed key.
  95.      * @return keyword corresponding to the parsed key
  96.      * (null if not recognized)
  97.      */
  98.     public Keyword getKeyword() {
  99.         return keyword;
  100.     }

  101.     /** Get the key.
  102.      * @return key
  103.      */
  104.     public String getKey() {
  105.         return key;
  106.     }

  107.     /** Get the value.
  108.      * @return value
  109.      */
  110.     public String getValue() {
  111.         return value;
  112.     }

  113.     /** Get the value as a double number.
  114.      * @return value
  115.      * @exception OrekitException if value is not a number
  116.      */
  117.     public double getDoubleValue() throws OrekitException {
  118.         try {
  119.             return Double.parseDouble(value);
  120.         } catch (NumberFormatException nfe) {
  121.             throw new OrekitException(OrekitMessages.UNABLE_TO_PARSE_LINE_IN_FILE,
  122.                                       lineNumber, fileName, line);
  123.         }
  124.     }

  125. }