use of com.webcohesion.enunciate.EnunciateException in project enunciate by stoicflame.
the class CXMLClientModule method readResource.
/**
* Reads a resource into string form.
*
* @param resource The resource to read.
* @return The string form of the resource.
*/
protected String readResource(String resource, Map<String, Object> model, NameForTypeDefinitionMethod nameForTypeDefinition) {
Method exampleResource = findExampleResourceMethod();
if (exampleResource != null) {
TypeDefinition typeDefinition = findRequestElement(exampleResource);
if (typeDefinition != null) {
model.put("input_element_name", nameForTypeDefinition.calculateName(typeDefinition));
}
typeDefinition = findResponseElement(exampleResource);
if (typeDefinition != null) {
model.put("output_element_name", nameForTypeDefinition.calculateName(typeDefinition));
}
}
URL res = CXMLClientModule.class.getResource(resource);
try {
return processTemplate(res, model);
} catch (TemplateException e) {
throw new EnunciateException(e);
} catch (IOException e) {
throw new EnunciateException(e);
}
}
use of com.webcohesion.enunciate.EnunciateException in project enunciate by stoicflame.
the class JavaScriptClientModule method call.
@Override
public void call(EnunciateContext context) {
if ((this.jacksonModule == null || this.jacksonModule.getJacksonContext() == null || this.jacksonModule.getJacksonContext().getTypeDefinitions().isEmpty()) && (this.jackson1Module == null || this.jackson1Module.getJacksonContext() == null || this.jackson1Module.getJacksonContext().getTypeDefinitions().isEmpty())) {
info("No Jackson JSON data types: JavaScript client will not be generated.");
return;
}
detectAccessorNamingErrors();
Map<String, String> packageToNamespaceConversions = getPackageToNamespaceConversions();
List<DecoratedTypeElement> schemaTypes = new ArrayList<DecoratedTypeElement>();
ExtensionDepthComparator comparator = new ExtensionDepthComparator();
EnunciateJacksonContext jacksonContext = null;
EnunciateJackson1Context jackson1Context = null;
if (this.jacksonModule != null) {
jacksonContext = this.jacksonModule.getJacksonContext();
for (TypeDefinition typeDefinition : jacksonContext.getTypeDefinitions()) {
String pckg = typeDefinition.getPackage().getQualifiedName().toString();
if (!packageToNamespaceConversions.containsKey(pckg)) {
packageToNamespaceConversions.put(pckg, packageToNamespace(pckg));
}
int position = Collections.binarySearch(schemaTypes, typeDefinition, comparator);
if (position < 0) {
position = -position - 1;
}
schemaTypes.add(position, typeDefinition);
}
}
if (this.jackson1Module != null) {
jackson1Context = this.jackson1Module.getJacksonContext();
for (com.webcohesion.enunciate.modules.jackson1.model.TypeDefinition typeDefinition : jackson1Context.getTypeDefinitions()) {
String pckg = typeDefinition.getPackage().getQualifiedName().toString();
if (!packageToNamespaceConversions.containsKey(pckg)) {
packageToNamespaceConversions.put(pckg, packageToNamespace(pckg));
}
schemaTypes.add(typeDefinition);
}
}
File srcDir = getSourceDir();
Map<String, Object> model = new HashMap<String, Object>();
model.put("globalName", this.config.getString("[@global]", "javascriptClient"));
model.put("schemaTypes", schemaTypes);
model.put("namespaceFor", new ClientPackageForMethod(packageToNamespaceConversions, this.context));
ClientClassnameForMethod classnameFor = new ClientClassnameForMethod(packageToNamespaceConversions, jacksonContext, jackson1Context);
model.put("classnameFor", classnameFor);
model.put("typeNameFor", new TypeNameForMethod(packageToNamespaceConversions, jacksonContext, jackson1Context));
model.put("simpleNameFor", new SimpleNameWithParamsMethod(classnameFor));
model.put("jsFileName", getSourceFileName());
model.put("file", new FileDirective(srcDir, this.enunciate.getLogger()));
model.put("generatedCodeLicense", this.enunciate.getConfiguration().readGeneratedCodeLicenseFile());
Set<String> facetIncludes = new TreeSet<String>(this.enunciate.getConfiguration().getFacetIncludes());
facetIncludes.addAll(getFacetIncludes());
Set<String> facetExcludes = new TreeSet<String>(this.enunciate.getConfiguration().getFacetExcludes());
facetExcludes.addAll(getFacetExcludes());
FacetFilter facetFilter = new FacetFilter(facetIncludes, facetExcludes);
model.put("isFacetExcluded", new IsFacetExcludedMethod(facetFilter));
if (!isUpToDateWithSources(srcDir)) {
debug("Generating the JavaScript data classes...");
URL apiTemplate = getTemplateURL("api.fmt");
try {
processTemplate(apiTemplate, model);
} catch (IOException e) {
throw new EnunciateException(e);
} catch (TemplateException e) {
throw new EnunciateException(e);
}
} else {
info("Skipping JavaScript code generation because everything appears up-to-date.");
}
File packageDir = getPackageDir();
packageDir.mkdirs();
File bundle = new File(packageDir, getBundleFileName());
boolean anyFiles = bundle.exists();
if (!isUpToDateWithSources(packageDir)) {
try {
anyFiles = enunciate.zip(bundle, srcDir);
} catch (IOException e) {
throw new EnunciateException(e);
}
}
if (anyFiles) {
ClientLibraryArtifact artifactBundle = new ClientLibraryArtifact(getName(), "js.client.library", "JavaScript Client Library");
artifactBundle.setPlatform("JavaScript");
FileArtifact sourceScript = new FileArtifact(getName(), "javascript.client", bundle);
// binaries and sources are the same thing in js
sourceScript.setArtifactType(ArtifactType.binaries);
sourceScript.setPublic(false);
// read in the description from file
String description = readResource("library_description.fmt", model);
artifactBundle.setDescription(description);
artifactBundle.addArtifact(sourceScript);
this.enunciate.addArtifact(artifactBundle);
}
}
use of com.webcohesion.enunciate.EnunciateException in project enunciate by stoicflame.
the class JavaScriptClientModule method readResource.
/**
* Reads a resource into string form.
*
* @param resource The resource to read.
* @return The string form of the resource.
*/
protected String readResource(String resource, Map<String, Object> model) {
model.put("sample_resource", findExampleResourceMethod());
URL res = JavaScriptClientModule.class.getResource(resource);
try {
return processTemplate(res, model);
} catch (TemplateException e) {
throw new EnunciateException(e);
} catch (IOException e) {
throw new EnunciateException(e);
}
}
use of com.webcohesion.enunciate.EnunciateException in project enunciate by stoicflame.
the class QNameEnumTypeDefinition method loadEnumValues.
@Override
protected List<EnumValue> loadEnumValues() {
String namespace = getPackage().getNamespace();
XmlQNameEnum xmlQNameEnum = getAnnotation(XmlQNameEnum.class);
if (xmlQNameEnum != null && !"##default".equals(xmlQNameEnum.namespace())) {
namespace = xmlQNameEnum.namespace();
}
if (namespace == null) {
namespace = "";
}
List<VariableElement> enumConstants = enumValues();
List<EnumValue> enumValues = new ArrayList<EnumValue>();
HashSet<QName> enumValueValues = new HashSet<QName>(enumConstants.size());
VariableElement unknownQNameConstant = null;
for (VariableElement enumConstant : enumConstants) {
if (isIgnored(enumConstant)) {
continue;
}
XmlUnknownQNameEnumValue unknownQNameEnumValue = enumConstant.getAnnotation(XmlUnknownQNameEnumValue.class);
if (unknownQNameEnumValue != null) {
if (unknownQNameConstant != null) {
throw new EnunciateException(getQualifiedName() + ": no more than two constants can be annotated with @XmlUnknownQNameEnumValue.");
}
unknownQNameConstant = enumConstant;
continue;
}
String ns = namespace;
String localPart = enumConstant.getSimpleName().toString();
XmlQNameEnumValue enumValueInfo = enumConstant.getAnnotation(XmlQNameEnumValue.class);
if (enumValueInfo != null) {
if (enumValueInfo.exclude()) {
continue;
}
if (!"##default".equals(enumValueInfo.namespace())) {
ns = enumValueInfo.namespace();
}
if (!"##default".equals(enumValueInfo.localPart())) {
localPart = enumValueInfo.localPart();
}
}
QName qname = new QName(ns, localPart);
if (!enumValueValues.add(qname)) {
throw new EnunciateException(getQualifiedName() + ": duplicate qname enum value: " + qname);
}
enumValues.add(new EnumValue(this, enumConstant, enumConstant.getSimpleName().toString(), qname));
}
if (unknownQNameConstant != null) {
// enter the unknown qname constant as a null qname.
enumValues.add(new EnumValue(this, unknownQNameConstant, unknownQNameConstant.getSimpleName().toString(), null));
}
return enumValues;
}
use of com.webcohesion.enunciate.EnunciateException in project enunciate by stoicflame.
the class ElementComparator method compare.
// Inherited.
public int compare(Element accessor1, Element accessor2) {
if (isSameId(accessor1, accessor2)) {
// if the elements have the same identifier.
return 0;
}
// they're not the same, so now determine relative order:
String propertyName1 = accessor1.getSimpleName().toString();
String propertyName2 = accessor2.getSimpleName().toString();
if (this.propOrder != null) {
// apply the specified property order
int propertyIndex1 = find(this.propOrder, propertyName1);
int propertyIndex2 = find(this.propOrder, propertyName2);
if (propertyIndex1 < 0) {
throw new EnunciateException("Property '" + propertyName1 + "' isn't included in the specified property order.");
}
if (propertyIndex2 < 0) {
throw new EnunciateException("Property '" + propertyName2 + "' isn't included in the specified property order.");
}
return propertyIndex1 - propertyIndex2;
} else if (accessOrder == XmlAccessOrder.ALPHABETICAL) {
return propertyName1.compareTo(propertyName2);
}
// If no order is specified, it's undefined. We'll put it in source order.
SourcePosition position1 = this.env.findSourcePosition(accessor1);
SourcePosition position2 = this.env.findSourcePosition(accessor2);
if (position1 != null && position2 != null) {
return position1.compareTo(position2);
} else {
// don't have source positions... just provide a random sort order.
return accessor1.hashCode() - accessor2.hashCode();
}
}
Aggregations