/* * [PathItem.java] * * Summary: Path Script Items. * * Copyright: (c) 2011-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 1.0 2011-11-13 initial version */ package com.mindprod.sortsrs; import com.mindprod.fastcat.FastCat; /** * Path Script Items. *

* represents a [Path] comment | data item. * * @author Roedy Green, Canadian Mind Products * @version 1.0 2011-11-13 initial version * @since 2011-11-13 */ class PathItem extends Item { /** * wildcard path of files to search */ final String path; /** * may be empty or null */ private final String pathComment; /** * Constructor * * @param path wild card of a set of files to search * @param pathComment comment oun past the end of the [Path], possibly null or empty. */ private PathItem( String path, String pathComment ) { this.section = 2; this.path = canonise( path ); this.pathComment = canonise( pathComment ); } /** * displays where in raw script had trouble * * @param pathMarker [Path] comment * @param path path string * * @return combined string for error message. */ private static String where( final String pathMarker, final String path ) { final FastCat sb = new FastCat( 5 ); sb.append( " | " ); sb.append( pathMarker ); sb.append( " | " ); sb.append( path ); return sb.toString(); } /** * compose a 2-line Path item, with lead \r n but no trailing \r\n( * * @param stripComments true if should strip out comments * * @return chars as they appear is reconstructed script. starting with \r\n, but not not ending with it. */ String combine( final boolean stripComments ) { final FastCat sb = new FastCat( 4 ); sb.append( "\r\n[Path]" ); if ( !stripComments ) { sb.append( pathComment ); } sb.append( "\r\n" ); sb.append( path ); return sb.toString(); // without trailing \r\n } /** * create new PathItem from raw pair of lines in the script * * @param pathMarker0 [Path] comment / without lead \r\n * @param path1 path String without trail \r\n line * * @return PathItem built of fields parsed from two lines. */ public static PathItem create( final String pathMarker0, final String path1 ) { assert pathMarker0.startsWith( "[Path" ) : "invalid pathMarker"; final String pathComment; // comment might contain stray ] int pathCommentStartAt = pathMarker0.indexOf( ']' ); if ( pathCommentStartAt < 0 ) { throw new IllegalArgumentException( "Error [08]: Script corrupt. Search options malformed " + where( pathMarker0, path1 ) ); } else { pathComment = pathMarker0.substring( pathCommentStartAt + 1 ); } return new PathItem( path1, pathComment ); } /** * show all just the path data, not the comments * * @return search/options/replace */ public String toString() { // will deal with null, leave out comments. final FastCat sb = new FastCat( 3 ); sb.append( "-Path- p{" ); sb.append( path ); sb.append( "}" ); return sb.toString(); } }