/** * Checks if the struct member with the given name should be represented in the XML schema as an attribute instead * of an nested element. Currently this is necessary for the {@code id} and {@code href} attribute. * * @param name the name to check * @return {@code true} if the attribute with the given name should be represented as an XML attribute, * {@code false} otherwise */ public boolean isRepresentedAsAttribute(Name name) { return HREF.equals(name) || ID.equals(name) || REL.equals(name); } }
public MemberInvolvementTree getMemberInvolvemnetTree(Name name) { for (MemberInvolvementTree tree : memberInvolvementTrees) { if (tree.getName().equals(name)) { return tree; } } return null; } public boolean isMandatory() {
private Parameter getParameter(Name name, List<Parameter> parameters) { for (Parameter parameter : parameters) { if (parameter.getName().equals(name)) { return parameter; } } throw new IllegalStateException("A parameter by name " + name + " was expected to be found."); }
/** * Remove tree with provided root name from member-involvement-trees list. */ public void removeMemeberInvolvementTree(Name name) { memberInvolvementTrees.removeIf(s -> s.getName().equals(name)); }
public MemberInvolvementTree getNode(Name name) { for (MemberInvolvementTree node : nodes) { if (node.getName().equals(name)) { return node; } } return null; }
/** * Determine whether the current expression represents a mandatory * (vs optional) attribute. */ private boolean isMandatory(MethodExpression expression) { return expression.getMethod().equals(MANDATORY) ? true : false; }
/** * The purpose of this method is to remove 'mandatory' or 'optional' * methods from the Expression, if they exist. * * For example, the output of this method for the expression: * mandatory(disk().format()) would be disk().format(). The output * for disk().format() would be disk().format() (unchanged) */ private Expression removePrefix(MethodExpression expression) { return expression.getMethod().equals(MANDATORY) || expression.getMethod().equals(OPTIONAL) ? expression.getParameters().get(0) : expression; }
/** * Return true if this method has any 'action' (e.g: install) as its base. * Methods which have a base method represent 'signatures' of the same method. */ public boolean isActionSignature(Method method) { return method.getBase()!=null && !method.getBase().getName().equals(ADD) && !method.getBase().getName().equals(UPDATE); }
private void generateHelperCode(Method method) { boolean base = baseMethods.containsKey(method); Name name = method.getName(); //for the following cases - no validation required //(signatures are handled in the context of the base method). if (JaxrsGeneratorUtils.GET.equals(name) || JaxrsGeneratorUtils.LIST.equals(name) || JaxrsGeneratorUtils.REMOVE.equals(name) || jaxrsGeneratorUtils.isAddSignature(method) || jaxrsGeneratorUtils.isUpdateSignature(method) || jaxrsGeneratorUtils.isActionSignature(method)) { return; //do nothing. } if (JaxrsGeneratorUtils.ADD.equals(name) || JaxrsGeneratorUtils.UPDATE.equals(name)) { if (base) { generateSignatureDetection(method); } else { generateValidation(method); } } else {//other options exhausted, this must be an action. if (base) { generateActionSignatureDetection(method); } else { generateActionValidation(method); } } }
/** * Return true if this method has ADD as its base. * Methods which have a base method represent 'signatures' of the same method. */ public boolean isAddSignature(Method method) { return method.getBase()!=null && method.getBase().getName().equals(ADD); }
/** * Return true if this method has UPDATE as its base. * Methods which have a base method represent 'signatures' of the same method. */ public boolean isUpdateSignature(Method method) { return method.getBase()!=null && method.getBase().getName().equals(UPDATE); }
/** * Find the member-involvement-tree with the provided name and return it. * If no such tree exist, create it and add it to the Parameter object, * then return it. */ private MemberInvolvementTree getMemberTree(Parameter parameter, Name name) { for (MemberInvolvementTree tree : parameter.getMemberInvolvementTrees()) { if (tree.getName().equals(name)) { return tree; } } //if none found, create a new one, add it to the Parameter and return it MemberInvolvementTree tree = new MemberInvolvementTree(name); parameter.getMemberInvolvementTrees().add(tree); return tree; }
private void generateMethod(Method method, JavaClassName helperClassName, Map<Method, Set<Method>> baseMethods) { Name name = method.getName(); if (JaxrsGeneratorUtils.ADD.equals(name)) { generateAddMethod(method, helperClassName, baseMethods); } else if (JaxrsGeneratorUtils.GET.equals(name)) { generateGetMethod(method); } else if (JaxrsGeneratorUtils.LIST.equals(name)) { generateListMethod(method); } else if (JaxrsGeneratorUtils.REMOVE.equals(name)) { generateRemoveMethod(method); } else if (JaxrsGeneratorUtils.UPDATE.equals(name)) { generateUpdateMethod(method, helperClassName, baseMethods); } else if (jaxrsGeneratorUtils.isAddSignature(method)) { generateAddSignature(method); } else if (jaxrsGeneratorUtils.isUpdateSignature(method)) { generateUpdateSignature(method); } else if (jaxrsGeneratorUtils.isActionSignature(method)) { generateActionSignature(method); } else {//other options exhausted; must be an action generateActionMethod(method, helperClassName, baseMethods); } }
/** * This method inserts a collection element into the stack. A collection element is an array * of the form: ...[COLLECTION]... The way that such an element is handled is: * 1) The array index ([COLLECTION]) is discarded. * 2) The stacking process continues for the rest of the expression. * (Note: the information that an element is a collection will be available later * by examining the element-type, that's why [COLLECTION] can be safely discarded). */ private void stackCollectionExpression(Stack<Name> stack, MethodExpression expression) { if (expression.getTarget() instanceof ArrayExpression) { ArrayExpression arrayExpression = (ArrayExpression)expression.getTarget(); if (!(arrayExpression.getIndex() instanceof FieldExpression) || !((FieldExpression)arrayExpression.getIndex()).getField().equals(COLLECTION)) { throw new IllegalArgumentException("The only valid array expression in live documentation is [COLLECTION]"); } else { //A valid collection expression stackExpressionElements(stack, (MethodExpression)arrayExpression.getArray(), true); } } else { throw new IllegalArgumentException("Can't handle " + expression.getClass().getSimpleName() + " in this context; only MethodExpression or ArrayExpression"); } }
/** * Analyze a single 'live documentation' expression. Each expression represents an attribute. * The attribute is added to the relevant Parameter. */ private void analyzeExpression(MethodExpression expression, List<Parameter> parameters) { if (expression.getMethod().equals(OR)) { analyzeOrExpression(expression, parameters); } else { boolean mandatory = isMandatory(expression); expression = getMethodExpression(removePrefix(expression)); analyzeExpression(expression, parameters, mandatory); } }
public boolean isContained(Method method, Method method2) { if (method.getName().equals(method2.getName())) { return false; //same method } if (method.getBase()==null || method2.getBase()==null) { return false; //no parent, containment irrelevant } if (!method.getBase().getName().equals(method2.getBase().getName())) { return false; //different parents, containment irrelevant. } return isParametersContained(method.getMandatoryParameters(), method2.getMandatoryParameters()) && isAttributesContained(method.getMandatoryAttributes(), method2.getMandatoryAttributes()); }