/* * [JavaCC.java] * * Summary: Expands HTML JavaCC macro, formats data about JavaCC grammars in a table. * * 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.8 2011-02-05 initial version. */ package com.mindprod.htmlmacros.macro; import com.mindprod.common18.BigDate; import com.mindprod.fastcat.FastCat; import com.mindprod.htmlmacros.support.BuildImage; import com.mindprod.htmlmacros.support.ImageAlignment; import java.util.regex.Pattern; import static com.mindprod.htmlmacros.macro.Global.configuration; import static java.lang.System.*; /** * Expands HTML JavaCC macro, formats data about JavaCC grammars in a table. *

* * @author Roedy Green, Canadian Mind Products * @version 1.8 2011-02-05 initial version. * @noinspection WeakerAccess * @since 2011-02-05 */ public final class JavaCC extends Macro { /** how to use the macro */ /** * how to use the macro */ private static final String USAGE = "\nJavaCC macro needs grammar url cache email author releasedDate version desc"; private static final Pattern SPLIT_ON_COMMA = Pattern.compile( "\\s*,\\s*" ); /** * Generate macro expansion for javacc macro. * * @param parms . * @param quiet true if should suppress output * @param verbose true if should provide extra output * * @return expanded text in the form of a table entry. */ @SuppressWarnings( { "UnusedAssignment" } ) public final String expandMacro( String[] parms, final boolean quiet, final boolean verbose ) { if ( !quiet ) { out.print( "J" ); } if ( parms.length != 8 ) { throw new IllegalArgumentException( USAGE ); } final String grammar = parms[ 0 ]; if ( grammar.equals( "grammar" ) || grammar.equals( "-" ) || grammar.length() == 0 ) { throw new IllegalArgumentException( "JavaCC grammar field cannot be empty" ); } String url = parms[ 1 ]; if ( url.equals( "url" ) || url.equals( "-" ) || url.length() == 0 ) { url = null; } else if ( !url.startsWith( "http" ) ) { throw new IllegalArgumentException( "JavaCC url must start with http: " + url ); } String cache = parms[ 2 ]; if ( cache.equals( "cache" ) || cache.equals( "-" ) || cache.length() == 0 ) { cache = null; } String email = parms[ 3 ]; if ( email.equals( "email" ) || email.equals( "mailto" ) || email.equals( "-" ) || email.length() == 0 ) { email = null; } else { email = email.toLowerCase(); } String author = parms[ 4 ]; if ( author.equals( "author" ) || author.equals( "-" ) ) { author = ""; } String released = parms[ 5 ]; if ( released.equals( "released" ) || released.equals( "date" ) || released.equals( "-" ) ) { released = ""; } else if ( !BigDate.isValid( released ) ) { throw new IllegalArgumentException( "JavaCC macro: invalid release date: " + released ); } String version = parms[ 6 ]; if ( version.equals( "version" ) || version.equals( "-" ) ) { version = ""; } final String desc = parms[ 7 ]; final FastCat sb = new FastCat( 21 + 6 * 6 ); sb.append( "" ); sb.append( grammar ); sb.append( "" ); if ( url != null ) { String cssClass = Global.assignCSSClasses.assignCSSClass( url, fileBeingDistributed ); if ( cssClass != null ) { sb.append( "download" ); } if ( cache != null ) { if ( url != null ) { sb.append( configuration.getBr() ); } sb.append( "download" ); } sb.append( "" ); if ( email != null ) { final String[] emails = SPLIT_ON_COMMA.split( email ); final String[] authors = SPLIT_ON_COMMA.split( author ); if ( emails.length != authors.length ) { throw new IllegalArgumentException( "JavaCC unequal number of email addresses and authors" ); } for ( int i = 0; i < emails.length; i++ ) { if ( i != 0 ) { sb.append( configuration.getBr() ); } if ( emails[ i ].indexOf( '@' ) >= 0 ) { // mailto style sb.append( "" ); sb.append( authors[ i ] ); sb.append( "" ); } else { // masker style sb.append( authors[ i ] ); sb.append( " " ); sb.append( BuildImage.buildImgTag( "mailto/" + email + ".png", "email " + authors[ i ], ImageAlignment.none, "mailto", fileBeingDistributed ) ); } } } else { sb.append( author ); } sb.append( "" ); sb.append( released ); sb.append( "" ); sb.append( version ); sb.append( "" ); sb.append( desc ); sb.append( "\n" ); return sb.toString(); } } // end Reunion