org.jpedal.objects.javascript.defaultactions.JpedalDefaultJavascript Maven / Gradle / Ivy
Show all versions of OpenViewerFX Show documentation
/*
* ===========================================
* Java Pdf Extraction Decoding Access Library
* ===========================================
*
* Project Info: http://www.idrsolutions.com
* Help section for developers at http://www.idrsolutions.com/support/
*
* (C) Copyright 1997-2016 IDRsolutions and Contributors.
*
* This file is part of JPedal/JPDF2HTML5
*
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* ---------------
* JpedalDefaultJavascript.java
* ---------------
*/
package org.jpedal.objects.javascript.defaultactions;
import java.awt.Toolkit;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.jpedal.utils.NumberUtils;
import org.jpedal.utils.StringUtils;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;
/** JS
* this whole class has methods for javascript actions
* and should not be accessed by anything else
*/
@SuppressWarnings("ALL")
public class JpedalDefaultJavascript{
/** added to make the testing be more predictable */
public static boolean testingStopAlertBoxes;
public static boolean testingSetStaticDate;
private static final boolean DebugDefaultJavascript = false;
private static final String format[] = {"'D':yyyyMMddHHmmssZ","'D':yyyyMMddHHmmssZ'Z","yyyy/MM/dd HH:mm:ss"};
// private String eventValue = null;
/** store the scope for this javascript object so we can create our javascript strings to return */
private final Scriptable scope;
/** store the context so that we can create the javascript strings */
private final Context context;
/** added for js so that the js can figure out which type of viewer we implement*/
public static final String viewerType = "Exchange-Pro";
/** added for js so that the js can figure out which version of viewer we implement*/
public static final int viewerVersion = 10;
/*JS if 1 or more then calculations are allowed if 0 or less they are not */
public static int calculate = 1;
//If true, the background color and hover color for form fields are shown.
public boolean runtimeHighlight;
public JpedalDefaultJavascript(final Scriptable scopeItem, final Context contextItem){
if(DebugDefaultJavascript) {
System.out.println("JpedalDefaultJavascript constructed");
}
scope = scopeItem;
context = contextItem;
}
/** sorts the date into the string format specified
*
* longMonth = "mmmm", shortMonth = "mmm", lead0NumberedMonth = "mm",
* nonLead0NumberedMonth = "m", longDay = "dddd", shortDay= "ddd", lead0NumberedDay = "dd",
* nonLead0NumberedDay = "d", longYear = "yyyy", shortYear = "yy", lead024Hour = "HH",
* nonLead024Hour = "H", lead012Hour = "hh", nonLead012Hour ="h", lead0Mins = "MM", nonLead0Mins = "M",
* lead0Secs = "ss", nonLead0Secs = "s", amPm = "tt", singleDigitAmPm = "t", escapeChar = "\";
*
*
returns a javascript string.
*/
public Scriptable printd(final String format, final org.mozilla.javascript.Scriptable obj) {
if(DebugDefaultJavascript) {
System.out.println("JpedalDefaultJavascript.printd(String,Scriptable)");
}
// get the Date class from the Scriptable value passed in as
// Rhino does not allow NativeDate to be used externally, even though this is what is passed here.
Date jsDate = null;
if ( org.mozilla.javascript.NativeJavaObject.canConvert(obj, Date.class ) ) {
jsDate = (java.util.Date) org.mozilla.javascript.Context.jsToJava( obj, Date.class );
}
if ( jsDate == null ) {
org.mozilla.javascript.Context.throwAsScriptRuntimeEx( new RuntimeException( "Not a Date()" ));
}
Calendar date = Calendar.getInstance();
date.setTime(jsDate);
//jsDate = null;//delete the variable so it does not get used
/** added to make the testing be more predictable */
if(testingSetStaticDate){
final Calendar yesterday = Calendar.getInstance();
yesterday.roll(Calendar.DAY_OF_YEAR, false);//roll back one day
if(date.get(Calendar.YEAR)>yesterday.get(Calendar.YEAR) ||
(date.get(Calendar.YEAR)==yesterday.get(Calendar.YEAR) &&
date.get(Calendar.DAY_OF_YEAR)>yesterday.get(Calendar.DAY_OF_YEAR))){
date = Calendar.getInstance();
date.set(2010,10,10,10,10,10);//year,month,date,hrs,mins,secs
}
// Date currentDate = new Date(System.currentTimeMillis());
// currentDate.setDate(currentDate.getDate()-1);
//
// if(date.after(currentDate)){
// date = new Date(110,10,10,10,10,10);//year,month,date,hrs,mins,secs
// }
}
// replace the javascript format string with a java format string to produce the required output.
//we need to do this as some chars represent different types of data in each
final char[] formatVal = format.toCharArray();
final StringBuffer retVal = new StringBuffer();
for(int i=0; i < formatVal.length; i++){
if(formatVal[i] == 'm'){//replace JS month with java month
retVal.append('M');
}else if(formatVal[i] == 't'){
if(formatVal[i+1] == 't'){//if tt replace with char for AM/PM
retVal.append('a');
i++;
}else {// single t special case, replace with either A/P
if(date.get(Calendar.HOUR_OF_DAY)>12) {
retVal.append("'P'");
} else {
retVal.append("'A'");
}
}
}else if(formatVal[i] == 'M'){// replace JS min for java min
retVal.append('m');
}else if(formatVal[i] == 'd' && formatVal[i+1] == 'd' && formatVal[i+2] == 'd'){
//if word for day is needed then change to java word of day
if(formatVal[i+3] == 'd'){//long JS day replaced with long java day
retVal.append("EEEE");
i+=3;
}else {// short JS day replaced with short java day
retVal.append("EEE");
i+=2;
}
}else if(formatVal[i] == '\\'){
retVal.append('\'');
retVal.append(formatVal[++i]);
retVal.append('\'');
}else {
retVal.append(formatVal[i]);
}
}
// create the formatter object with the required format
final SimpleDateFormat df = new SimpleDateFormat(new String(retVal));
final String newDate = df.format(date.getTime());
if(DebugDefaultJavascript) {
System.out.println("returning String=" + newDate);
}
return context.newObject(scope, "String", new Object[] { newDate});
}
/** sorts the date into a prerequisite format based on the index.
*
* currently does not work corretly with javascript method calls,
* if this method was commented in, it would be called, even for calls to printd(String,Scritable)
*
returns a javascript string.
*/
public Scriptable printd(final int index, final org.mozilla.javascript.Scriptable obj) {
if(DebugDefaultJavascript) {
System.out.println("JpedalDefaultJavascript.printd(int,Scriptable)");
}
Calendar date = null;
if ( org.mozilla.javascript.NativeJavaObject.canConvert(obj, Calendar.class ) ) {
date = (java.util.Calendar) org.mozilla.javascript.Context.jsToJava( obj, Calendar.class );
}
if ( date == null ) {
org.mozilla.javascript.Context.throwAsScriptRuntimeEx( new RuntimeException( "Not a Date()" ));
}
/** added to make the testing be more predictable */
if(testingSetStaticDate){
final Calendar yesterday = Calendar.getInstance();
yesterday.roll(Calendar.DATE, false);//roll back one day
if(date.get(Calendar.YEAR)>yesterday.get(Calendar.YEAR) ||
(date.get(Calendar.YEAR)==yesterday.get(Calendar.YEAR) &&
date.get(Calendar.DAY_OF_YEAR)>yesterday.get(Calendar.DAY_OF_YEAR))){
date = Calendar.getInstance();
date.set(2010,10,10,10,10,10);//year,month,date,hrs,mins,secs
}
// Date currentDate = new Date(System.currentTimeMillis());
// currentDate.setDate(currentDate.getDate()-1);
// if(date.after(currentDate)){
// date = new Date(110,10,10,10,10,10);//year,month,date,hrs,mins,secs
// }
}
final SimpleDateFormat df = new SimpleDateFormat(format[index]);
return context.newObject(scope, "String", new Object[] { df.format(date)});
}
/**
* sorts the arguments passed in into a string defined by the template given in cFormat, %(char) says
* what type it should be in.
* there can be as many arguments (comma-seperated) as there are % tags in the format string.
*
* see page 720 of 'Javascript for Acrobat API reference'
*/
public Scriptable printf(final String cFormat, final String[] args){
/*
cFormat - The format string to use.
arguments - The optional arguments(s) that contain the data to be inserted in place of the % tags
specified in the first parameter, the format string. The number of optional arguments must be
the same as the number of % tags.
*/
final StringBuilder buf = new StringBuilder();
final char PERCENT = '%';
int tokenCount = 0;
int stringLoct = 0;
int tokenPos = cFormat.indexOf(PERCENT);
String obj;
while(tokenPos!=-1){
buf.append(cFormat.substring(stringLoct, tokenPos));
//store the new start position in 'stringLoct'
int end2Pos;
final char[] endTokens = {'d','f','s','x'};
int c=0;
stringLoct = cFormat.indexOf(endTokens[c++],tokenPos);
for(; c0) {
returnString.append('+');
} else {
returnString.append('-');
}
break;
case ' '://cFlags
// space � If the first character is not a sign, a space will be prefixed.
if(value>0) {
returnString.append(' ');
} else {
returnString.append('-');
}
break;
case '0'://cFlags
// 0 � Specifies padding to the field with leading zeros.
padd = true;
break;
case '#'://cFlags
// # � Specifies an alternate output form. For f, the output will always have a decimal point.
floatDecimal = true;
break;
case '.':
// nPrecision - A period character (.) followed by a number that specifies the number of digits
// after the decimal point for float conversions.
decimalPoints = Integer.parseInt(String.valueOf(tokArray[++i]));
break;
case 'd'://cConvChar
// d � Integer (truncating if necessary)
sValue.append((int)value);
if(padd){
final int stringlen = returnString.length()+sValue.length();
if(stringlen crackURL(final String cURL){
if(!cURL.startsWith("file") && !cURL.startsWith("http") && !cURL.startsWith("https")){
return null;
}
final Map propertiesMap = new HashMap();
final int index1 = cURL.indexOf("://");
propertiesMap.put("cScheme", cURL.substring(0,index1));
if(cURL.contains("@")){
//http://user:password@host/path
final int userInd = cURL.indexOf(':',index1+3);
final int atInd = cURL.indexOf('@');
final int index2 = cURL.indexOf('/',atInd+1);
propertiesMap.put("cHost", cURL.substring(atInd+1,index2));
propertiesMap.put("cUser", cURL.substring(index1+3,userInd));
propertiesMap.put("cPassword", cURL.substring(userInd+1,atInd));
propertiesMap.put("cPath", cURL.substring(index2+1));
}else {
//http://host[:port]/directory/file
final int splitInd = cURL.indexOf(':',index1);
final int index2 = cURL.indexOf('/',splitInd);
propertiesMap.put("cHost", cURL.substring(index1+3,splitInd));
propertiesMap.put("nPort", cURL.substring(splitInd+1,index2));
propertiesMap.put("cPath", cURL.substring(index2+1));
}
return propertiesMap;
}
/**
* formats the given value into the required format
*/
public static double z(final String format, final double value) {
final double val1 = value * 100;
final double val2 = Math.round(val1);
return val2/100;
}
/** tells the system to beep at the user */
public static void beep(final int type){
switch(type) {
default:
Toolkit.getDefaultToolkit().beep();
}
}
}