Package ket.math.purpose

Source Code of ket.math.purpose.Text

/*
* Copyright (C) 2011  Alasdair C. Hamilton
*
* This program 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 3 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.  If not, see <http://www.gnu.org/licenses/>
*/

package ket.math.purpose;

import java.util.*;
import java.awt.Color;
import java.util.regex.*;

import ket.display.ColourScheme;
import ket.display.ColourSchemeDecorator;
import ket.display.box.Box;
import ket.display.box.BoxText;
import ket.display.box.BoxWord;

import ket.math.Argument;
import ket.math.Purpose;
import ket.math.Symbol;

/**
* This represents plain text, which is often written in quotes.
*/
public class Text extends Value implements VariableState {
  public static final char PREFIX_CHAR = '$';

  String string;

  /**
   * Create a new string value where quotes are not yet added to string,
   * but are optionally specified by a separate boolean flag.
   * @param string This is the value without quotes that this object wraps.
   */
  public Text(String string) {
    assert string!=null;
    this.string = string;
  }

  public String toLatex() {
    return " \\mbox{ " + getUnquotedString() + " } ";
  }

  @Override
  public String toHTML() {
    return getQuotedString(); //?
  }

  public String getHTMLSubscript() {
    return getUnquotedString().replaceAll(" ", "&nbsp;");
  }

  @Override
  public String getValue() {
    return getUnquotedString();
  }

  @Override
  public String toString() {
    return " " + getQuotedString() + " ";
  }

  @Override
  public Box toBox(Argument argument, long settings, ColourScheme colourScheme) {
    // Represent blanks strings with nice unicode empty quotes, ''.
    String value = string.equals("") ? "\u0091\u0092" : string;
    return new BoxText(argument, value, settings);
  }

  @Override
  public String toVerboseString() {
    return getQuotedString();
  }

  @Override
  public boolean matchTypeChar(char type) {
    return type==Purpose.DEFAULT_PREFIX_CHAR || type==PREFIX_CHAR;
  }

  Pattern UPPER_CASE = Pattern.compile("[A-Z]");

  @Override
  public boolean matches(String pattern) {
    Matcher upperCaseMatcher = UPPER_CASE.matcher(pattern);
    if ( ! upperCaseMatcher.find() ) {
      // Smart case: only ignore case if no capital letters are in the search string.
      pattern = pattern.toLowerCase();
      return getQuotedString().toLowerCase().equals(pattern) ||
        getUnquotedString().toLowerCase().equals(pattern) ||
        getUnquotedString().toLowerCase().contains(pattern);
    } else {
      return getQuotedString().equals(pattern) ||
        getUnquotedString().equals(pattern) ||
        getUnquotedString().contains(pattern);
    }
  }

  @Override
  public boolean equals(Purpose purpose) {
    /*-
    if (purpose instanceof Text) {
      Text text = (Text) purpose;
      return this.getUnquotedString().equals(text.getUnquotedString());
    } else {
      return false;
    }
    */
    if (purpose==null) return false;
    return this.getText().equals(purpose.getText());
  }

  public String getUnquotedString() {
    return string;
  }

  public String getQuotedString() {
    return "\"" + string + "\"";
  }
 
  /**
   * Return the hash code of the value of this object.
   */
  public int hashCode() {
    return string.hashCode();
  }


  // TODO: This has no WORD token so shares TEXT with them.
  public static final Set<Symbol> MATCHES = new HashSet<Symbol>(Arrays.asList(new Symbol[]{Symbol.RE_TOKEN, Symbol.RE_WORD}));

  @Override
  public Set<Symbol> getMatchSymbols() {
    return MATCHES;
  }

  @Override
  public boolean isVariable() {
    return false;
  }

  public boolean isText() {
    return true;
  }

  public String getText() {
    return string;
  }
}
TOP

Related Classes of ket.math.purpose.Text

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.