When trying to use a java source code as template for Velocity, it crashes at this line of the template:
/* #see panama.form.Validator#validate(java.lang.Object) */
with this Exception:
Exception in thread "main" org.apache.velocity.exception.ParseErrorException: Lexical error, Encountered: "l" (108), after : "." at *unset*[line 23, column 53]
at org.apache.velocity.runtime.RuntimeInstance.evaluate(RuntimeInstance.java:1301)
at org.apache.velocity.runtime.RuntimeInstance.evaluate(RuntimeInstance.java:1265)
at org.apache.velocity.app.VelocityEngine.evaluate(VelocityEngine.java:199)
Apparently it takes the #validate for a macro and crashes when it tries to parse the arguments for the macro. Is there anything one could do about this?
I'm using Velocity 1.7.
Edit
I know I could escape the # characters in the template files, but there are quite a number of them which also might change now and then, so I would prefer a way that would not require manual changes on the files.
First option
Try this solution from here: Escaping VTL Directives
VTL directives can be escaped with the backslash character ("\") in a manner similar to valid VTL references.
## #include( "a.txt" ) renders as <contents of a.txt>
#include( "a.txt" )
## \#include( "a.txt" ) renders as #include( "a.txt" )
\#include( "a.txt" )
## \\#include ( "a.txt" ) renders as \<contents of a.txt>
\\#include ( "a.txt" )
Second option
You have this tool [EscapeTool][2].
Tool for working with escaping in Velocity templates.
It provides methods to escape outputs for Java, JavaScript, HTML, XML and SQL. Also provides methods to render VTL characters that otherwise needs escaping.
Third option:
You may also try this workaround, I didn't use it but it should work:
You can at the beginning read your template as a String and then pre-parse it. For example replace all # with \#, or add to the beginning of file
#set( $H = '#' )
$H$H
see this answer: How to escape a # in velocity And then from that pre-parsed String create Template by using this answer: How to use String as Velocity Template?
Related
The environment is Java 8, Saxon 9.8 processor, XSL Stylesheet Version 3, running from Eclipse.
Given the following xslt command in the stylesheet:
<xsl:variable name="output"
select="fn:replace($inputstring,
'^.*exec\s+sp_prepexec.+?N'([^#](?:[^'']|'''')+)''.*$', '$1', 'ism;j')" />
Produces the stacktrace:
net.sf.saxon.trans.XPathException: Invalid character '^' in expression
at net.sf.saxon.expr.parser.XPathParser.grumble(XPathParser.java:281)
at net.sf.saxon.expr.parser.XPathParser.grumble(XPathParser.java:238)
at net.sf.saxon.expr.parser.XPathParser.grumble(XPathParser.java:225)
at net.sf.saxon.expr.parser.XPathParser.nextToken(XPathParser.java:196)
at net.sf.saxon.expr.parser.XPathParser.parseDynamicFunctionCall(XPathParser.java:2358)
at net.sf.saxon.expr.parser.XPathParser.parseStepExpression(XPathParser.java:1974)
...
at org.eclipse.wst.xsl.jaxp.debug.invoker.internal.Main.main(Main.java:72)
I did not find any clue why a caret won't be allowed in that expression - can you support me debugging this?
I was wondering if escaping is a problem, in the code line above I doubled single apostrophes in the expression, also tried it with ', but it is always the same error message.
Given from the flags I assume that Saxon would use the Java regex parser for this, but the returned stack trace does not show that.
This is an example of the input string I want to process:
declare #p1 int
set #p1=328
exec sp_prepexec #p1 output,N'#P1 int,#P2 char(1),#P3 char(1)',N'SELECT "Tbl1009"."RUN_NO" "Col1111","Tbl1009"."DEP_ID" "Col1114" FROM "run" "Tbl1009" WHERE #P1="Tbl1009"."RUN_ID" AND ("Tbl1009"."Profile_ID"=(1) AND #P2=''N'' OR "Tbl1009"."Profile_ID"=(5) AND #P3=''Y'') AND ("Tbl1009"."Profile_ID"=(1) OR "Tbl1009"."Profile_ID"=(5))',150,'N','N'
select #p1
and the required output:
SELECT "Tbl1009"."RUN_NO" "Col1111","Tbl1009"."DEP_ID" "Col1114" FROM "run" "Tbl1009" WHERE #P1="Tbl1009"."RUN_ID" AND ("Tbl1009"."Profile_ID"=(1) AND #P2=''N'' OR "Tbl1009"."Profile_ID"=(5) AND #P3=''Y'') AND ("Tbl1009"."Profile_ID"=(1) OR "Tbl1009"."Profile_ID"=(5))
#WillBarnwell has the correct diagnosis but the wrong solution. The problem with the ' isn't that it is special in regular expressions, the problem is that it is special in XPath, so you need to use XPath-level escaping, and the way to do that is to write it as two apostrophes. This can get pretty bewildering so the best thing is often to move the regex to a variable defined with content:
<xsl:variable name="regex" as="xs:string"
>^.*exec\s+sp_prepexec.+?N'([^#](?:[^']|'')+)'.*$</xsl:variable>
<xsl:variable name="output"
select="fn:replace($inputstring, $regex, '$1', 'ism;j')" />
(Check that carefully because I'm not sure I have fully understood your intent).
syntax error, your regex is ending at the first unescaped single quote and is being interpreted as ^.*exec\s+sp_prepexec.+?N this is then followed by ([^ with ^ being the first illegal character. Notice that the error is originating from the XML parser, not the regex engine.
Escaping your single quotes with \' is not the way to solve this, as #Michael-Kay shows it is define your regex in a variable.
I have written a ‘Pig Script’ which is processing Sequence files given as input.
It is working fine but there is one problem mentioned below.
I have repetitive statements in my pig script, as shown below:
Filtered_Data _1= FILTER BagName BY ($0 matches 'RegEx-1');
Filtered_Data_2 = FILTER BagName BY ($0 matches 'RegEx-2');
Filtered_Data_3 = FILTER BagName BY ($0 matches 'RegEx-3');
So on…
Question :
So is there any way by which I can have above statement written once and
then loop through all possible “RegEx” and substitute in Pig script.
For Example:
Filtered_Data _X = FILTER BagName BY ($0 matches 'RegEx'); ( have this statement once )
( loop through all possible RegEx and substitute value in the statement )
Right now I am calling Pig script from a shell script, so any way from shell script will be also be welcome or even Java wrapper...
Thanks in advance.
Happy Pigging!!!!
Apache common-cli has a example on its web site for ls command:
options.addOption( "a", "all", false, "do not hide entries starting with ." );
options.addOption( "A", "almost-all", false, "do not list implied . and .." );
options.addOption( "b", "escape", false, "print octal escapes for nongraphic " + "characters" );
options.addOption( OptionBuilder.withLongOpt( "block-size" )
.withDescription( "use SIZE-byte blocks" )
.hasArg()
.withArgName("SIZE")
.create() );
This shows help like this:
-a, --all do not hide entries starting with .
-A, --almost-all do not list implied . and ..
-b, --escape print octal escapes for nongraphic characters
--block-size=SIZE use SIZE-byte blocks
When I write this code, it shows --block-size <SIZE>. I want to show something like this: -z,--block-size=SIZE(not just
long option).
what is the difference of PosixParser and GnuParser? I changed
them in the code, I didn't observed any difference.
When I provide wrong option for example h it doesn't throw any ParseException. The program starts and finishes normally.
The block size option in the example has only a long format, that's why there is no short option shown. If you add a short alias you'll get the result you expect
PosixParser and GnuParser are deprecated in the latest version of Commons CLI. A new unified parser is available as DefaultParser. The posix parser had the ability to parse concatenated short options, something like tar -zxvf foo.tar.gz.
Extra options are either handled as arguments to the application or trigger an exception, depending of the value of the stopAtNonOption parser parameter.
I have a java file that I want to post online. I am using php to format the file.
Does anyone know the regex to turn the comments blue?
INPUT:
/*****
*This is the part
*I want to turn blue
*for my class
*******************/
class MyClass{
String s;
}
Thanks.
Naiive version:
$formatted = preg_replace('|(/\*.*?\*/)|m', '<span class="blue">$1</span>', $java_code_here);
... not tested, YMMV, etc...
In general, you won't be able to parse specific parts of a Java file using only regular expressions - Java is not a regular language. If your file has additional structure (such as "it always begins with a comment followed by a newline, followed by a class definition"), you can generate a regular expression for such a case. For instance, you'd match /\*+(.*?)\*+/$, where . is assumed to match multiple lines, and $ matches the end of a line.
In general, to make a regex work, you first define what patterns you want to find (rigorously, but in spoken language), and then translate that to standard regular expression notation.
Good luck.
A regex that can parse simple quotes should be able to find comments in C/C++ style languages.
I assume Java is of that type.
This is a Perl faq sample by someone else, although I added the part about // style comments (with or without line continuation) and reformated.
It basically does a global search and replace. Data is replaced verbatim if non a comment, otherwise replace the comment with your color formatting tags.
You should be able to adapt this to php, and it is expanded for clarity (maybe too much clarity though).
s{
## Comments, group 1:
(
/\* ## Start of /* ... */ comment
[^*]*\*+ ## Non-* followed by 1-or-more *'s
(?:
[^/*][^*]*\*+
)* ## 0-or-more things which don't start with /
## but do end with '*'
/ ## End of /* ... */ comment
|
// ## Start of // ... comment
(?:
[^\\] ## Any Non-Continuation character ^\
| ## OR
\\\n? ## Any Continuation character followed by 0-1 newline \n
)*? ## To be done 0-many times, stopping at the first end of comment
\n ## End of // comment
)
| ## OR, various things which aren't comments, group 2:
(
" (?: \\. | [^"\\] )* " ## Double quoted text
|
' (?: \\. | [^'\\] )* ' ## Single quoted text
|
. ## Any other char
[^/"'\\]* ## Chars which doesn't start a comment, string, escape
) ## or continuation (escape + newline)
}
{defined $2 ? $2 : "<some color>$1</some color>"}gxse;
I would linke to call java app from PHP:
exec('LC_ALL=en_US.utf-8 java -jar /test.jar' . $filepath . ');
But always there are unsupported characters in the file path.
For example: # & ; ? * [SPACE]..., after change them to # \& ... it will be ok.
But a full list of these characters could not be find.
Any ideas to solve this problem?
Take a look at escapeshellarg() and escapeshellcmd().
They will take care of all necessary sanitation for you.
If $filepath comes from the outside (e.g. from user input), running escapeshellarg() is mandatory to prevent injections.
My problem resolved.
Useful url:
http://bugs.php.net/bug.php?id=44945