001 /* This file is part of the project "Hilbert II" - http://www.qedeq.org
002 *
003 * Copyright 2000-2013, Michael Meyling <mime@qedeq.org>.
004 *
005 * "Hilbert II" is free software; you can redistribute
006 * it and/or modify it under the terms of the GNU General Public
007 * License as published by the Free Software Foundation; either
008 * version 2 of the License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013 * GNU General Public License for more details.
014 */
015
016 package org.qedeq.base.io;
017
018
019 /**
020 * Wraps a text output stream.
021 *
022 * @author Michael Meyling
023 */
024 public class SubTextInput extends TextInput {
025
026 /** Absolute start of this input relative to base {@link SubTextInput}. */
027 private final int absoluteStart;
028
029 /** Absolute end of this input relative to base {@link SubTextInput}. */
030 private final int absoluteEnd;
031
032 /**
033 * Constructor.
034 *
035 * @param source Text input.
036 */
037 public SubTextInput(final String source) {
038 super(source);
039 this.absoluteStart = 0;
040 this.absoluteEnd = source.length();
041 }
042
043 /**
044 * Constructor.
045 *
046 * @param original Parent input.
047 * @param absoluteStart Input part starts here.
048 * @param absoluteEnd Input part ends here.
049 */
050 public SubTextInput(final SubTextInput original, final int absoluteStart, final int absoluteEnd) {
051 super(original.getAbsoluteSubstring(absoluteStart, absoluteEnd));
052 this.absoluteStart = absoluteStart;
053 this.absoluteEnd = absoluteEnd;
054 }
055
056 /**
057 * Get the absolute start position of the current {@link SubTextInput}.
058 *
059 * @return Absolute start position.
060 */
061 public int getAbsoluteStart() {
062 return absoluteStart;
063 }
064
065 /**
066 * Get the absolute end position of the current {@link SubTextInput}.
067 *
068 * @return Absolute end position.
069 */
070 public int getAbsoluteEnd() {
071 return absoluteEnd;
072 }
073
074 /**
075 * Get the absolute position of the current position.
076 *
077 * @return Absolute position of current position.
078 */
079 public int getAbsolutePosition() {
080 return getAbsoluteStart() + getPosition();
081 }
082
083 /**
084 * Set the current position by calculating the relative position
085 * from the given absolute position.
086 *
087 * @param absolutePosition This should be the absolute position.
088 */
089 public void setAbsolutePosition(final int absolutePosition) {
090 setPosition(absolutePosition - getAbsoluteStart());
091 }
092
093 /**
094 * Get sub string of source. The given parameters have values for the underlying original
095 * SubTextInput at the base.
096 *
097 * @param absoluteFrom Absolute start of sub string.
098 * @param absoluteTo Absolute end of sub string.
099 * @return Sub string.
100 */
101 public String getAbsoluteSubstring(final int absoluteFrom, final int absoluteTo) {
102 return getSubstring(absoluteFrom - getAbsoluteStart(), absoluteTo - getAbsoluteStart());
103 }
104
105 /**
106 * Get sub string of source as a new {@link SubTextInput}. The given parameters have
107 * values for the underlying original SubTextInput at the base.
108 *
109 * @param absoluteFrom Absolute start of sub string.
110 * @param absoluteTo Absolute end of sub string.
111 * @return Sub string.
112 */
113 public SubTextInput getSubTextInput(final int absoluteFrom, final int absoluteTo) {
114 return new SubTextInput(this, absoluteFrom, absoluteTo);
115 }
116
117 }
|