Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.cxf.javascript;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.Stack;
import javax.xml.namespace.QName;
import org.w3c.dom.Attr;
import org.apache.cxf.common.WSDLConstants;
import org.apache.cxf.common.xmlschema.SchemaCollection;
import org.apache.cxf.common.xmlschema.XmlSchemaConstants;
import org.apache.cxf.common.xmlschema.XmlSchemaUtils;
import org.apache.cxf.databinding.source.mime.MimeAttribute;
import org.apache.ws.commons.schema.XmlSchemaComplexType;
import org.apache.ws.commons.schema.XmlSchemaElement;
import org.apache.ws.commons.schema.XmlSchemaObject;
import org.apache.ws.commons.schema.XmlSchemaSimpleContent;
import org.apache.ws.commons.schema.XmlSchemaSimpleContentExtension;
import org.apache.ws.commons.schema.XmlSchemaSimpleType;
import org.apache.ws.commons.schema.XmlSchemaType;
import org.apache.ws.commons.schema.constants.Constants;
/**
* A set of functions that assist in JavaScript generation. This includes
* functions for appending strings of JavaScript to a buffer as well as some
* type utilities.
*/
public class JavascriptUtils {
private static final String NL = "\n";
private static int anyTypePrefixCounter;
private StringBuilder code;
private Stack prefixStack;
private String xmlStringAccumulatorVariable;
private Map defaultValueForSimpleType;
private Set nonStringSimpleTypes;
private Set intTypes;
private Set floatTypes;
public JavascriptUtils(StringBuilder code) {
this.code = code;
defaultValueForSimpleType = new HashMap();
defaultValueForSimpleType.put("int", "0");
defaultValueForSimpleType.put("unsignedInt", "0");
defaultValueForSimpleType.put("long", "0");
defaultValueForSimpleType.put("unsignedLong", "0");
defaultValueForSimpleType.put("float", "0.0");
defaultValueForSimpleType.put("double", "0.0");
nonStringSimpleTypes = new HashSet();
nonStringSimpleTypes.add("int");
nonStringSimpleTypes.add("long");
nonStringSimpleTypes.add("unsignedInt");
nonStringSimpleTypes.add("unsignedLong");
nonStringSimpleTypes.add("float");
nonStringSimpleTypes.add("double");
intTypes = new HashSet();
intTypes.add("int");
intTypes.add("long");
intTypes.add("unsignedInt");
intTypes.add("unsignedLong");
floatTypes = new HashSet();
floatTypes.add("float");
floatTypes.add("double");
prefixStack = new Stack();
prefixStack.push(" ");
}
public String getDefaultValueForSimpleType(XmlSchemaType type) {
String val = defaultValueForSimpleType.get(type.getName());
if (val == null) { // ints and such return the appropriate 0.
return "''";
} else {
return val;
}
}
public boolean isStringSimpleType(QName typeName) {
return !(WSDLConstants.NS_SCHEMA_XSD.equals(typeName.getNamespaceURI()) && nonStringSimpleTypes
.contains(typeName.getLocalPart()));
}
public void setXmlStringAccumulator(String variableName) {
xmlStringAccumulatorVariable = variableName;
}
public void startXmlStringAccumulator(String variableName) {
xmlStringAccumulatorVariable = variableName;
code.append(prefix());
code.append("var ");
code.append(variableName);
code.append(" = '';" + NL);
}
public static String protectSingleQuotes(String value) {
return value.replaceAll("'", "\\'");
}
public String escapeStringQuotes(String data) {
return data.replace("'", "\\'");
}
/**
* emit javascript to append a value to the accumulator.
*
* @param value
*/
public void appendString(String value) {
code.append(prefix());
code.append(xmlStringAccumulatorVariable + " = " + xmlStringAccumulatorVariable + " + '");
code.append(escapeStringQuotes(value));
code.append("';" + NL);
}
public void appendExpression(String value) {
code.append(prefix());
code.append(xmlStringAccumulatorVariable + " = " + xmlStringAccumulatorVariable + " + ");
code.append(value);
code.append(";" + NL);
}
private String prefix() {
return prefixStack.peek();
}
public void appendLine(String line) {
code.append(prefix());
code.append(line);
code.append(NL);
}
public void startIf(String test) {
code.append(prefix());
code.append("if (" + test + ") {" + NL);
prefixStack.push(prefix() + " ");
}
public void startBlock() {
code.append(prefix());
code.append("{" + NL);
prefixStack.push(prefix() + " ");
}
public void appendElse() {
prefixStack.pop();
code.append(prefix());
code.append("} else {" + NL);
prefixStack.push(prefix() + " ");
}
public void endBlock() {
prefixStack.pop();
code.append(prefix());
code.append("}" + NL);
}
public void startFor(String start, String test, String increment) {
code.append(prefix());
code.append("for (" + start + ";" + test + ";" + increment + ") {" + NL);
prefixStack.push(prefix() + " ");
}
public void startForIn(String var, String collection) {
code.append(prefix());
code.append("for (var " + var + " in " + collection + ") {" + NL);
prefixStack.push(prefix() + " ");
}
public void startWhile(String test) {
code.append(prefix());
code.append("while (" + test + ") {" + NL);
prefixStack.push(prefix() + " ");
}
public void startDo() {
code.append(prefix());
code.append("do {" + NL);
prefixStack.push(prefix() + " ");
}
// Given a js variable and a simple type object, correctly set the variables
// simple type
public String javascriptParseExpression(XmlSchemaType type, String value) {
if (!(type instanceof XmlSchemaSimpleType)) {
return value;
}
String name = type.getName();
if (intTypes.contains(name)) {
return "parseInt(" + value + ")";
} else if (floatTypes.contains(name)) {
return "parseFloat(" + value + ")";
} else if ("boolean".equals(name)) {
return "(" + value + " == 'true')";
} else {
return value;
}
}
public static String javaScriptNameToken(String token) {
return token;
}
/**
* We really don't want to take the attitude that 'all base64Binary elements are candidates for MTOM'.
* So we look for clues.
* @param schemaObject
* @return
*/
private boolean treatAsMtom(XmlSchemaObject schemaObject) {
if (schemaObject == null) {
return false;
}
Map