| 1 |
/* |
| 2 |
* splitindex.java |
| 3 |
* Copyright (c) Markus Kohm, 2002-2009 |
| 4 |
* |
| 5 |
* $Id: splitindex.java 4 2016-02-18 10:13:32Z mjk $ |
| 6 |
* |
| 7 |
* This file is part of the SplitIndex bundle. |
| 8 |
* |
| 9 |
* This work may be distributed and/or modified under the conditions of |
| 10 |
* the LaTeX Project Public License, version 1.3c of the license. |
| 11 |
* The latest version of this license is in |
| 12 |
* http://www.latex-project.org/lppl.txt |
| 13 |
* and version 1.3c or later is part of all distributions of LaTeX |
| 14 |
* version 2005/12/01 or later and of this work. |
| 15 |
* |
| 16 |
* This work has the LPPL maintenance status "author-maintained". |
| 17 |
* |
| 18 |
* The Current Maintainer and author of this work is Markus Kohm. |
| 19 |
* |
| 20 |
* The list of all files belongig to the SplitIndex bundle is given in |
| 21 |
* in the file `manifest.txt'. Files generated by means of unpacking the |
| 22 |
* distribution (using, for example, the docstrip program) or by means |
| 23 |
* of compiling them from a source file, for example, from splitindex.c |
| 24 |
* or splitindex.java may be distributed at the distributor's discretion. |
| 25 |
* However if they are distributed then a copy of the SplitIndex bundle |
| 26 |
* must be distributed together with them. |
| 27 |
* |
| 28 |
* The list of derived (unpacked or compiled) files belongig to the |
| 29 |
* distribution and covered by LPPL is defined by the unpacking scripts |
| 30 |
* (with extension .ins) and the installation script (with name |
| 31 |
* install.sh) which are part of the distribution. |
| 32 |
* |
| 33 |
* Two often ignorred clauses from LPPL 1.3c you should not ignore: |
| 34 |
* ---------------------------------------------------------------- |
| 35 |
* 2. You may distribute a complete, unmodified copy of the Work as you |
| 36 |
* received it. Distribution of only part of the Work is considered |
| 37 |
* modification of the Work, and no right to distribute such a Derived |
| 38 |
* Work may be assumed under the terms of this clause. |
| 39 |
* 3. You may distribute a Compiled Work that has been generated from a |
| 40 |
* complete, unmodified copy of the Work as distributed under Clause 2 |
| 41 |
* above, as long as that Compiled Work is distributed in such a way that |
| 42 |
* the recipients may install the Compiled Work on their system exactly |
| 43 |
* as it would have been installed if they generated a Compiled Work |
| 44 |
* directly from the Work. |
| 45 |
*/ |
| 46 |
|
| 47 |
/* |
| 48 |
I know, I should never write an application in a single static class |
| 49 |
like I do here. But I wanted the program at each language to be one |
| 50 |
file. |
| 51 |
*/ |
| 52 |
|
| 53 |
import java.util.*; |
| 54 |
import java.util.regex.*; |
| 55 |
import java.io.*; |
| 56 |
|
| 57 |
public final class splitindex { |
| 58 |
private static ArrayList MakeIndexArgs = new ArrayList(); |
| 59 |
// private static String Identify = "^(.*)\\\\UseIndex *\\{([^\\}]*)\\}(.*)$"; |
| 60 |
private static String Identify = "^(\\\\indexentry)\\[([^\\]]*)\\](.*)$"; |
| 61 |
private static String ResultIs = "$1$3"; |
| 62 |
private static String SuffixIs = "-$2"; |
| 63 |
private static int Verbose = 0; |
| 64 |
private static String IDX = null; |
| 65 |
private static String Jobname = null; |
| 66 |
private static HashMap IDXwriters = new HashMap(); |
| 67 |
|
| 68 |
private static void ShowHelp() { |
| 69 |
String nl = System.getProperty( "line.separator" ); |
| 70 |
ShowVersion(); |
| 71 |
System.out.println(); |
| 72 |
ShowUsage(); |
| 73 |
System.out.println( |
| 74 |
"Split a single raw index file into multiple raw index files." +nl+ |
| 75 |
"Example: java splitindex foo.idx." +nl+ |
| 76 |
nl+ |
| 77 |
"Options:" +nl+ |
| 78 |
" -h, --help " + |
| 79 |
"\tshow this help and terminate" +nl+ |
| 80 |
" -m, --makeindex PROGNAME" +nl+ |
| 81 |
"\t\t\tcall PROGNAME instead of default `makeindex'." +nl+ |
| 82 |
" -i, --identify EXPRESSION" +nl+ |
| 83 |
"\t\t\tuse regular EXPRESSION to match entries" +nl+ |
| 84 |
"\t\t\t(see also option --resultis and --suffixis)." +nl+ |
| 85 |
"\t\t\tDefault is \'" + Identify + "\'." +nl+ |
| 86 |
" -r, --resultis PATTERN" +nl+ |
| 87 |
"\t\t\tcreate line to be written from PATTERN after matching" +nl+ |
| 88 |
"\t\t\tlines (see also option --identify)." +nl+ |
| 89 |
"\t\t\tDefault is \'" + ResultIs + "\'." +nl+ |
| 90 |
" -s, --suffixis PATTERN" +nl+ |
| 91 |
"\t\t\tcreate suffix to be used from PATTERN after matching" +nl+ |
| 92 |
"\t\t\tlines (see also option --identify)." +nl+ |
| 93 |
"\t\t\tDefault is \'" + SuffixIs +"\'." +nl+ |
| 94 |
" -v, --verbose " + |
| 95 |
"\tbe more verbose" +nl+ |
| 96 |
"\t\t\t(can be used multiple to increase verbosity)" +nl+ |
| 97 |
" --version " + |
| 98 |
"\tshow version and terminate" |
| 99 |
); |
| 100 |
System.exit( 0 ); |
| 101 |
} |
| 102 |
|
| 103 |
private static void ShowVersion() { |
| 104 |
System.out.println( "splitindex.pl 0.1" ); |
| 105 |
System.out.println( "Copyright (c) 2002 Markus Kohm <[email protected]>" ); |
| 106 |
} |
| 107 |
|
| 108 |
private static void ShowUsage( PrintStream out ) { |
| 109 |
out.println( "Usage: java splitindex [OPTION]... RAWINDEXFILE [MAKEINDEXOPTION]..." ); |
| 110 |
} |
| 111 |
|
| 112 |
private static void ShowUsage() { |
| 113 |
ShowUsage( System.out ); |
| 114 |
} |
| 115 |
|
| 116 |
private static void UsageError( String msg ) { |
| 117 |
System.err.println( msg ); |
| 118 |
System.err.println( "Try `java splitindex --help' for more information." ); |
| 119 |
System.exit( 1 ); |
| 120 |
} |
| 121 |
|
| 122 |
private static void ScanArguments(String[] args) { |
| 123 |
String MakeIndex = "makeindex"; |
| 124 |
for (int i = 0; i < args.length; i++) { |
| 125 |
if ( args[i].charAt(0) == '-' ) { |
| 126 |
// Option |
| 127 |
if ( args[i].charAt(1) == '-' ) { |
| 128 |
// Long Option |
| 129 |
if ( args[i].substring(2).equals( "help" ) ) { |
| 130 |
ShowHelp(); |
| 131 |
System.exit(0); |
| 132 |
} else if ( args[i].substring(2).equals( "version" ) ) { |
| 133 |
ShowVersion(); |
| 134 |
System.exit(0); |
| 135 |
} else if ( args[i].substring(2).equals( "verbose" ) ) { |
| 136 |
Verbose++; |
| 137 |
} else if ( args[i].substring(2).equals( "makeindex" ) ) { |
| 138 |
if ( ++i >= args.length ) { |
| 139 |
UsageError( "Option makeindex requires an argument" ); |
| 140 |
} |
| 141 |
MakeIndex = args[i]; |
| 142 |
} else if ( ( args[i].length() >= 11 ) && |
| 143 |
( args[i].substring(2,11).equals( "makeindex=" ) ) ) { |
| 144 |
MakeIndex = args[i].substring(12); |
| 145 |
} else if ( args[i].substring(2).equals( "identify" ) ) { |
| 146 |
if ( ++i >= args.length ) { |
| 147 |
UsageError( "Option identify requires an argument" ); |
| 148 |
} |
| 149 |
Identify = args[i]; |
| 150 |
} else if ( ( args[i].length() >= 10 ) && |
| 151 |
( args[i].substring(2,10).equals( "identify=" ) ) ) { |
| 152 |
Identify = args[i].substring(11); |
| 153 |
} else if ( args[i].substring(2).equals( "resultis" ) ) { |
| 154 |
if ( ++i >= args.length ) { |
| 155 |
UsageError( "Option resultis requires an argument" ); |
| 156 |
} |
| 157 |
ResultIs = args[i]; |
| 158 |
} else if ( ( args[i].length() >= 10 ) && |
| 159 |
( args[i].substring(2,10).equals( "resultis=" ) ) ) { |
| 160 |
ResultIs = args[i].substring(11); |
| 161 |
} else if ( args[i].substring(2).equals( "suffixis" ) ) { |
| 162 |
if ( ++i >= args.length ) { |
| 163 |
UsageError( "Option suffixis requires an argument" ); |
| 164 |
} |
| 165 |
SuffixIs = args[i]; |
| 166 |
} else if ( ( args[i].length() >= 10 ) && |
| 167 |
( args[i].substring(2,10).equals( "suffixis=" ) ) ) { |
| 168 |
SuffixIs = args[i].substring(11); |
| 169 |
} else if ( args[i].equals( "--" ) ) { |
| 170 |
while ( ++i < args.length ) { |
| 171 |
MakeIndexArgs.add( args[i] ); |
| 172 |
} |
| 173 |
} else { |
| 174 |
UsageError( "Unknown option " + args[i].substring(2) ); |
| 175 |
} |
| 176 |
} else { |
| 177 |
// Short Option |
| 178 |
for ( int n = 1; n < args[i].length(); ) { |
| 179 |
switch( args[i].charAt(n++) ) { |
| 180 |
case 'h': |
| 181 |
ShowHelp(); |
| 182 |
System.exit(0); |
| 183 |
case 'm': |
| 184 |
if ( n >= args[i].length() ) { |
| 185 |
if ( ++i >= args.length ) { |
| 186 |
UsageError( "Option makeindex requires an argument" ); |
| 187 |
} else { |
| 188 |
MakeIndex = args[i]; |
| 189 |
n = args[i].length(); |
| 190 |
} |
| 191 |
} else { |
| 192 |
MakeIndex = args[i].substring( n ); |
| 193 |
n = args[i].length(); |
| 194 |
} |
| 195 |
break; |
| 196 |
case 'i': |
| 197 |
if ( n >= args[i].length() ) { |
| 198 |
if ( ++i >= args.length ) { |
| 199 |
UsageError( "Option identify requires an argument" ); |
| 200 |
} else { |
| 201 |
Identify = args[i]; |
| 202 |
n = args[i].length(); |
| 203 |
} |
| 204 |
} else { |
| 205 |
Identify = args[i].substring( n ); |
| 206 |
n = args[i].length(); |
| 207 |
} |
| 208 |
break; |
| 209 |
case 'r': |
| 210 |
if ( n >= args[i].length() ) { |
| 211 |
if ( ++i >= args.length ) { |
| 212 |
UsageError( "Option resultis requires an argument" ); |
| 213 |
} else { |
| 214 |
ResultIs = args[i]; |
| 215 |
n = args[i].length(); |
| 216 |
} |
| 217 |
} else { |
| 218 |
ResultIs = args[i].substring( n ); |
| 219 |
n = args[i].length(); |
| 220 |
} |
| 221 |
break; |
| 222 |
case 's': |
| 223 |
if ( n >= args[i].length() ) { |
| 224 |
if ( ++i >= args.length ) { |
| 225 |
UsageError( "Option suffixis requires an argument" ); |
| 226 |
} else { |
| 227 |
SuffixIs = args[i]; |
| 228 |
n = args[i].length(); |
| 229 |
} |
| 230 |
} else { |
| 231 |
SuffixIs = args[i].substring( n ); |
| 232 |
n = args[i].length(); |
| 233 |
} |
| 234 |
break; |
| 235 |
case 'v': |
| 236 |
Verbose++; |
| 237 |
break; |
| 238 |
} |
| 239 |
} |
| 240 |
} |
| 241 |
} else { |
| 242 |
MakeIndexArgs.add( args[i] ); |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
// Args --> IDX + Stringarray |
| 247 |
if ( MakeIndexArgs.isEmpty() ) { |
| 248 |
UsageError( "missing raw index file" ); |
| 249 |
} else { |
| 250 |
IDX = (String)MakeIndexArgs.get(0); |
| 251 |
MakeIndexArgs.remove(0); |
| 252 |
MakeIndexArgs.add( 0, MakeIndex ); |
| 253 |
} |
| 254 |
|
| 255 |
if ( IDX.endsWith( ".idx" ) ) { |
| 256 |
Jobname = IDX.substring( 0, IDX.length() - 4 ); |
| 257 |
} else { |
| 258 |
Jobname = IDX; |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
public static void main(String[] args) { |
| 263 |
ScanArguments(args); |
| 264 |
|
| 265 |
if ( Verbose > 0 ) { |
| 266 |
ShowVersion(); |
| 267 |
System.out.println(); |
| 268 |
if ( Verbose > 9 ) { |
| 269 |
System.out.println( "Identify: \"" + Identify + "\"" ); |
| 270 |
System.out.println( "ResultIs: \"" + ResultIs + "\"" ); |
| 271 |
System.out.println( "SuffixIs: \"" + SuffixIs + "\"" ); |
| 272 |
System.out.println( "IDX: \"" + IDX + "\"" ); |
| 273 |
System.out.println( "Jobname: \"" + Jobname + "\"" ); |
| 274 |
System.out.print( "MakeIndex:" ); |
| 275 |
for ( int i = 0; i < MakeIndexArgs.size(); i++ ) |
| 276 |
System.out.print( " \"" + (String)MakeIndexArgs.get(i) + "\"" ); |
| 277 |
System.out.println(); |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
ProcessIDXFile(); |
| 282 |
} |
| 283 |
|
| 284 |
private static void ProcessIDXFile() { |
| 285 |
File fIDX = new File( IDX ); |
| 286 |
LineNumberReader rIDX = null; |
| 287 |
boolean error = false; |
| 288 |
if ( !fIDX.canRead() ) { |
| 289 |
if ( IDX.equals( Jobname ) ) { |
| 290 |
IDX = IDX.concat( ".idx" ); |
| 291 |
fIDX = new File( IDX ); |
| 292 |
if ( !fIDX.canRead() ) { |
| 293 |
System.err.println( "Can read neither file " + Jobname + |
| 294 |
" nor file " + IDX ); |
| 295 |
System.exit( 1 ); |
| 296 |
} |
| 297 |
} else { |
| 298 |
System.err.println( "Can't read file " + Jobname ); |
| 299 |
System.exit( 1 ); |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
try { |
| 304 |
FileReader reader = new FileReader( fIDX ); |
| 305 |
rIDX = new LineNumberReader( reader ); |
| 306 |
} catch ( FileNotFoundException ex ) { |
| 307 |
System.err.println( ex.getMessage() ); |
| 308 |
System.exit( 1 ); |
| 309 |
} |
| 310 |
|
| 311 |
try { |
| 312 |
Pattern search = Pattern.compile( Identify ); |
| 313 |
while ( rIDX.ready() ) { |
| 314 |
String line = rIDX.readLine(); |
| 315 |
Matcher match = search.matcher( line ); |
| 316 |
String suffix, result; |
| 317 |
|
| 318 |
try { |
| 319 |
if ( match.find() ) { |
| 320 |
suffix = match.replaceFirst( SuffixIs ); |
| 321 |
result = match.replaceFirst( ResultIs ); |
| 322 |
} else { |
| 323 |
result = line; |
| 324 |
suffix = SuffixIs.replaceFirst( "\\$\\d", "idx" ); |
| 325 |
} |
| 326 |
WriteToIndex( Jobname + suffix + ".idx", result ); |
| 327 |
} catch ( Exception ex ) { |
| 328 |
System.err.println( ex.getMessage() ); |
| 329 |
error = true; |
| 330 |
break; |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
if ( ! CloseAllIndex() ) |
| 335 |
error = true; |
| 336 |
else if ( ! CallAllMakeIndex() ) |
| 337 |
error = true; |
| 338 |
} catch ( IOException ex ) { |
| 339 |
System.err.println( ex.getMessage() ); |
| 340 |
} |
| 341 |
|
| 342 |
try { |
| 343 |
rIDX.close(); |
| 344 |
} catch ( IOException ex ) { |
| 345 |
System.err.println( ex.getMessage() ); |
| 346 |
System.exit( 1 ); |
| 347 |
} |
| 348 |
|
| 349 |
if ( error ) |
| 350 |
System.exit( 1 ); |
| 351 |
|
| 352 |
} |
| 353 |
|
| 354 |
static void WriteToIndex( String Name, String line ) |
| 355 |
throws FileNotFoundException, SecurityException { |
| 356 |
PrintWriter fOut; |
| 357 |
if ( ( fOut = (PrintWriter)IDXwriters.get( Name ) ) == null ) { |
| 358 |
if ( Verbose > 1 ) { |
| 359 |
System.out.println( "New index file " + Name ); |
| 360 |
} |
| 361 |
fOut = new PrintWriter( new FileOutputStream( Name ) ); |
| 362 |
IDXwriters.put( Name, fOut ); |
| 363 |
} |
| 364 |
fOut.println( line ); |
| 365 |
} |
| 366 |
|
| 367 |
static boolean CloseAllIndex() { |
| 368 |
Iterator all = IDXwriters.entrySet().iterator(); |
| 369 |
boolean retVal = true; |
| 370 |
while ( all.hasNext() ) { |
| 371 |
Map.Entry entry = (Map.Entry)all.next(); |
| 372 |
PrintWriter writer = (PrintWriter)entry.getValue(); |
| 373 |
if ( Verbose > 1 ) { |
| 374 |
System.out.println( "Close " + entry.getKey() ); |
| 375 |
} |
| 376 |
writer.close(); |
| 377 |
if ( writer.checkError() ) { |
| 378 |
System.err.println( "Error writing " + entry.getKey() ); |
| 379 |
retVal = false; |
| 380 |
} |
| 381 |
} |
| 382 |
return retVal; |
| 383 |
} |
| 384 |
|
| 385 |
static boolean CallAllMakeIndex() { |
| 386 |
Iterator all = IDXwriters.entrySet().iterator(); |
| 387 |
boolean retVal = true; |
| 388 |
ArrayList processes = new ArrayList(); |
| 389 |
while ( all.hasNext() ) { |
| 390 |
Map.Entry entry = (Map.Entry)all.next(); |
| 391 |
String name = (String)entry.getKey(); |
| 392 |
try { |
| 393 |
MakeIndexArgs.add(name); |
| 394 |
String Args[] = new String[MakeIndexArgs.size()]; |
| 395 |
Args = (String[])MakeIndexArgs.toArray( Args ); |
| 396 |
MakeIndexArgs.remove(MakeIndexArgs.size()-1); |
| 397 |
if ( Verbose > 1 ) { |
| 398 |
System.out.print( "MakeIndex:" ); |
| 399 |
for ( int i = 0; i < Args.length; i++ ) |
| 400 |
System.out.print( " \"" + Args[i] + "\"" ); |
| 401 |
System.out.println(); |
| 402 |
} |
| 403 |
processes.add(Runtime.getRuntime().exec( Args )); |
| 404 |
} catch ( Exception ex ) { |
| 405 |
System.err.println( ex.getMessage() ); |
| 406 |
retVal = false; |
| 407 |
} |
| 408 |
} |
| 409 |
|
| 410 |
for ( int i = 0; i < processes.size(); i++ ) { |
| 411 |
Process p = (Process)processes.get( i ); |
| 412 |
InputStream out = p.getInputStream(); |
| 413 |
byte[] buffer = new byte[1024]; |
| 414 |
for ( boolean oncemore = true; oncemore; ) { |
| 415 |
oncemore = false; |
| 416 |
try { |
| 417 |
while ( out.available() > 0 ) { |
| 418 |
out.read( buffer ); |
| 419 |
System.out.print( new String(buffer) ); |
| 420 |
} |
| 421 |
for ( out = p.getErrorStream(); |
| 422 |
out.available() > 0; ) { |
| 423 |
out.read( buffer ); |
| 424 |
System.err.print( new String( buffer ) ); |
| 425 |
} |
| 426 |
} catch ( Exception ex ) { |
| 427 |
System.err.println( ex.getMessage() ); |
| 428 |
retVal = false; |
| 429 |
} |
| 430 |
try { |
| 431 |
if ( p.exitValue() != 0 ) |
| 432 |
retVal = false; |
| 433 |
} catch ( IllegalThreadStateException ex ) { |
| 434 |
oncemore = true; |
| 435 |
} |
| 436 |
} |
| 437 |
} |
| 438 |
|
| 439 |
return retVal; |
| 440 |
} |
| 441 |
} |