001/*-------------------------------------------------------------------------+ 002| | 003| Copyright 2005-2011 the ConQAT Project | 004| | 005| Licensed under the Apache License, Version 2.0 (the "License"); | 006| you may not use this file except in compliance with the License. | 007| You may obtain a copy of the License at | 008| | 009| http://www.apache.org/licenses/LICENSE-2.0 | 010| | 011| Unless required by applicable law or agreed to in writing, software | 012| distributed under the License is distributed on an "AS IS" BASIS, | 013| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 014| See the License for the specific language governing permissions and | 015| limitations under the License. | 016+-------------------------------------------------------------------------*/ 017package org.conqat.lib.commons.string; 018 019import java.util.ArrayList; 020import java.util.List; 021 022import org.conqat.lib.commons.region.LineBasedRegion; 023import org.conqat.lib.commons.region.OffsetBasedRegion; 024 025/** 026 * Utilities to convert character-based regions to line-based regions 027 */ 028public class LineOffsetUtils { 029 030 /** 031 * Converts the given regions from offset-based to line-based using the 032 * given text. 033 */ 034 public static List<LineBasedRegion> convertToLineRegions(List<OffsetBasedRegion> regions, String text) { 035 LineOffsetConverter lineOffsetConverter = new LineOffsetConverter(text); 036 List<LineBasedRegion> result = new ArrayList<>(); 037 for (OffsetBasedRegion region : regions) { 038 result.add(convertToLineRegion(region, lineOffsetConverter)); 039 } 040 return result; 041 } 042 043 /** Creates a new line-based region from a offset-based region. */ 044 public static LineBasedRegion convertToLineRegion(OffsetBasedRegion region, 045 LineOffsetConverter lineOffsetConverter) { 046 int startLine = lineOffsetConverter.getLine(region.getStart()); 047 int endLine = lineOffsetConverter.getLine(region.getEnd()); 048 return new LineBasedRegion(startLine, endLine); 049 } 050 051}