1 /* This file is part of the project "Hilbert II" - http://www.qedeq.org" target="alexandria_uri">http://www.qedeq.org 2 * 3 * Copyright 2000-2014, Michael Meyling <mime@qedeq.org>. 4 * 5 * "Hilbert II" is free software; you can redistribute 6 * it and/or modify it under the terms of the GNU General Public 7 * License as published by the Free Software Foundation; either 8 * version 2 of the License, or (at your option) any later version. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 */ 15 16 package org.qedeq.base.io; 17 18 19 /** 20 * Wraps a text output stream. 21 * 22 * @author Michael Meyling 23 */ 24 public class SubTextInput extends TextInput { 25 26 /** Absolute start of this input relative to base {@link SubTextInput}. */ 27 private final int absoluteStart; 28 29 /** Absolute end of this input relative to base {@link SubTextInput}. */ 30 private final int absoluteEnd; 31 32 /** 33 * Constructor. 34 * 35 * @param source Text input. 36 */ 37 public SubTextInput(final String source) { 38 super(source); 39 this.absoluteStart = 0; 40 this.absoluteEnd = source.length(); 41 } 42 43 /** 44 * Constructor. 45 * 46 * @param original Parent input. 47 * @param absoluteStart Input part starts here. 48 * @param absoluteEnd Input part ends here. 49 */ 50 public SubTextInput(final SubTextInput original, final int absoluteStart, final int absoluteEnd) { 51 super(original.getAbsoluteSubstring(absoluteStart, absoluteEnd)); 52 this.absoluteStart = absoluteStart; 53 this.absoluteEnd = absoluteEnd; 54 } 55 56 /** 57 * Get the absolute start position of the current {@link SubTextInput}. 58 * 59 * @return Absolute start position. 60 */ 61 public int getAbsoluteStart() { 62 return absoluteStart; 63 } 64 65 /** 66 * Get the absolute end position of the current {@link SubTextInput}. 67 * 68 * @return Absolute end position. 69 */ 70 public int getAbsoluteEnd() { 71 return absoluteEnd; 72 } 73 74 /** 75 * Get the absolute position of the current position. 76 * 77 * @return Absolute position of current position. 78 */ 79 public int getAbsolutePosition() { 80 return getAbsoluteStart() + getPosition(); 81 } 82 83 /** 84 * Set the current position by calculating the relative position 85 * from the given absolute position. 86 * 87 * @param absolutePosition This should be the absolute position. 88 */ 89 public void setAbsolutePosition(final int absolutePosition) { 90 setPosition(absolutePosition - getAbsoluteStart()); 91 } 92 93 /** 94 * Get sub string of source. The given parameters have values for the underlying original 95 * SubTextInput at the base. 96 * 97 * @param absoluteFrom Absolute start of sub string. 98 * @param absoluteTo Absolute end of sub string. 99 * @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 }