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.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Deque;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Logger;
import javax.xml.namespace.QName;
import org.w3c.dom.Attr;
import org.apache.cxf.common.i18n.Message;
import org.apache.cxf.common.logging.LogUtils;
import org.apache.cxf.common.xmlschema.SchemaCollection;
import org.apache.cxf.common.xmlschema.XmlSchemaUtils;
import org.apache.cxf.databinding.source.mime.MimeAttribute;
import org.apache.cxf.wsdl.WSDLConstants;
import org.apache.ws.commons.schema.XmlSchema;
import org.apache.ws.commons.schema.XmlSchemaAll;
import org.apache.ws.commons.schema.XmlSchemaAnnotated;
import org.apache.ws.commons.schema.XmlSchemaAny;
import org.apache.ws.commons.schema.XmlSchemaAnyAttribute;
import org.apache.ws.commons.schema.XmlSchemaAttribute;
import org.apache.ws.commons.schema.XmlSchemaChoice;
import org.apache.ws.commons.schema.XmlSchemaComplexContentExtension;
import org.apache.ws.commons.schema.XmlSchemaComplexType;
import org.apache.ws.commons.schema.XmlSchemaContent;
import org.apache.ws.commons.schema.XmlSchemaContentModel;
import org.apache.ws.commons.schema.XmlSchemaElement;
import org.apache.ws.commons.schema.XmlSchemaGroup;
import org.apache.ws.commons.schema.XmlSchemaGroupRef;
import org.apache.ws.commons.schema.XmlSchemaObject;
import org.apache.ws.commons.schema.XmlSchemaParticle;
import org.apache.ws.commons.schema.XmlSchemaSequence;
import org.apache.ws.commons.schema.XmlSchemaSequenceMember;
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 XmlSchemaSequence EMPTY_SEQUENCE = new XmlSchemaSequence();
private static final XmlSchemaChoice EMPTY_CHOICE = new XmlSchemaChoice();
private static final XmlSchemaAll EMPTY_ALL = new XmlSchemaAll();
private static final Logger LOG = LogUtils.getL7dLogger(JavascriptUtils.class);
private static final String NL = "\n";
private static final Map DEFAULT_VALUE_FOR_SIMPLE_TYPE = new HashMap<>();
static {
DEFAULT_VALUE_FOR_SIMPLE_TYPE.put("int", "0");
DEFAULT_VALUE_FOR_SIMPLE_TYPE.put("unsignedInt", "0");
DEFAULT_VALUE_FOR_SIMPLE_TYPE.put("long", "0");
DEFAULT_VALUE_FOR_SIMPLE_TYPE.put("unsignedLong", "0");
DEFAULT_VALUE_FOR_SIMPLE_TYPE.put("float", "0.0");
DEFAULT_VALUE_FOR_SIMPLE_TYPE.put("double", "0.0");
}
private static final Set NON_STRINGS_SIMPLE_TYPES = new HashSet<>(
Arrays.asList("int", "long", "unsignedInt", "unsignedLong", "float", "double"));
private static final Set INT_TYPES = new HashSet<>(
Arrays.asList("int", "long", "unsignedInt", "unsignedLong"));
private static final Set FLOAT_TYPES = new HashSet<>(Arrays.asList("float", "double"));
private static int anyTypePrefixCounter;
private final StringBuilder code;
private final Deque prefixStack = new ArrayDeque<>();
private String xmlStringAccumulatorVariable;
public JavascriptUtils(StringBuilder code) {
this.code = code;
prefixStack.push(" ");
}
public String getDefaultValueForSimpleType(XmlSchemaType type) {
String val = DEFAULT_VALUE_FOR_SIMPLE_TYPE.get(type.getName());
if (val == null) { // ints and such return the appropriate 0.
return "''";
}
return val;
}
public boolean isStringSimpleType(QName typeName) {
return !(WSDLConstants.NS_SCHEMA_XSD.equals(typeName.getNamespaceURI()) && NON_STRINGS_SIMPLE_TYPES
.contains(typeName.getLocalPart()));
}
public void setXmlStringAccumulator(String variableName) {
xmlStringAccumulatorVariable = variableName;
}
public void startXmlStringAccumulator(String variableName) {
xmlStringAccumulatorVariable = variableName;
code.append(prefix());
code.append("var ").append(variableName).append(" = '';").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).append(" = ").append(xmlStringAccumulatorVariable).append(" + '");
code.append(escapeStringQuotes(value));
code.append("';").append(NL);
}
public void appendExpression(String value) {
code.append(prefix());
code.append(xmlStringAccumulatorVariable).append(" = ").append(xmlStringAccumulatorVariable).append(" + ");
code.append(value);
code.append(';').append(NL);
}
private String prefix() {
return prefixStack.peek();
}
public void appendLine(String line) {
code.append(prefix());
code.append(line).append(NL);
}
public void startIf(String test) {
code.append(prefix());
code.append("if (").append(test).append(") {").append(NL);
prefixStack.push(prefix() + " ");
}
public void startBlock() {
code.append(prefix());
code.append('{').append(NL);
prefixStack.push(prefix() + " ");
}
public void appendElse() {
prefixStack.pop();
code.append(prefix());
code.append("} else {").append(NL);
prefixStack.push(prefix() + " ");
}
public void endBlock() {
prefixStack.pop();
code.append(prefix());
code.append('}').append(NL);
}
public void startFor(String start, String test, String increment) {
code.append(prefix());
code.append("for (").append(start).append(';').append(test).append(';').append(increment).append(") {")
.append(NL);
prefixStack.push(prefix() + " ");
}
public void startForIn(String var, String collection) {
code.append(prefix());
code.append("for (var ").append(var).append(" in ").append(collection).append(") {").append(NL);
prefixStack.push(prefix() + " ");
}
public void startWhile(String test) {
code.append(prefix());
code.append("while (").append(test).append(") {").append(NL);
prefixStack.push(prefix() + " ");
}
public void startDo() {
code.append(prefix());
code.append("do {").append(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 (INT_TYPES.contains(name)) {
return "parseInt(" + value + ")";
} else if (FLOAT_TYPES.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