Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c3e4177894492c7d78e462e9729bb41dc7c2ab9d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/**
 * <copyright> 
 *
 * Copyright (c) 2006 IBM Corporation and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors: 
 *   IBM - Initial API and implementation
 *
 * </copyright>
 *
 * $Id: ValidatorGeneratorUtil.java,v 1.1 2006/11/10 23:04:27 davidms Exp $
 */
package org.eclipse.emf.examples.generator.validator;

import java.util.Iterator;
import java.util.List;

import org.eclipse.emf.codegen.ecore.genmodel.GenClass;
import org.eclipse.emf.codegen.ecore.genmodel.GenOperation;
import org.eclipse.emf.codegen.ecore.genmodel.GenPackage;
import org.eclipse.emf.codegen.ecore.genmodel.GenTypedElement;
import org.eclipse.emf.codegen.util.ImportManager;
import org.eclipse.emf.ecore.ETypedElement;

public class ValidatorGeneratorUtil
{
  private ValidatorGeneratorUtil()
  {
  }

  public final static String TEMPLATE_LOCATION = ValidatorGeneratorPlugin.INSTANCE.getBaseURL().toString() + "templates";

  public final static String CLASSPATH_VARIABLE_NAME = "EMF_EXAMPLES_GENERATOR_VALIDATOR";

  public static String getValidationPackageName(GenPackage genPackage)
  {
    String basePackage = genPackage.getInterfacePackageName();
    return basePackage.length() > 0 ? basePackage + ".validation" : "validation";
  }

  public static String getValidatorInterfaceName(GenClass genClass)
  {
    return genClass.getInterfaceName() + "Validator";
  }

  public static String getQualifiedValidatorInterfaceName(GenClass genClass)
  {
    return getValidationPackageName(genClass.getGenPackage()) + "." + getValidatorInterfaceName(genClass);
  }

  public static String getImportedValidatorInterfaceName(GenClass genClass)
  {
    return genClass.getGenModel().getImportedName(getQualifiedValidatorInterfaceName(genClass));
  }

  protected static String getImportedName(String qualifiedName, ImportManager importManager)
  {
    int index = qualifiedName.indexOf("$");
    importManager.addImport(index == -1 ? qualifiedName : qualifiedName.substring(0, index));
    return importManager.getImportedName(qualifiedName);
  }

  public static String getSuperTypesExpression(GenClass genClass)
  {
    List extendsList = genClass.getBaseGenClasses();
    if (!extendsList.isEmpty())
    {
      StringBuffer result = new StringBuffer();
      result.append(" -> ");
      for (Iterator i = extendsList.iterator(); i.hasNext(); )
      {
        result.append(((GenClass)i.next()).getName());
        if (i.hasNext())
        {
          result.append(", ");
        }
      }
      return result.toString();
    }
    return "";
  }

  public static String getTypeExpression(GenTypedElement genTypedElement)
  {
    StringBuffer result = new StringBuffer();
    result.append(genTypedElement.getTypeGenClassifier().getName());

    ETypedElement eTypedElement = (ETypedElement)genTypedElement.getEcoreModelElement();
    int lowerBound = eTypedElement.getLowerBound();
    int upperBound = eTypedElement.getUpperBound();

    if (lowerBound != 0 || upperBound != 1)
    {
      result.append("<<");
      result.append(lowerBound);
      result.append("..");

      if (upperBound == ETypedElement.UNSPECIFIED_MULTIPLICITY)
      {
        result.append('?');
      }
      else if (upperBound == ETypedElement.UNBOUNDED_MULTIPLICITY)
      {
        result.append('*');
      }
      else
      {
        result.append(upperBound);
      }
      result.append(">>");
    }
    return result.toString();
  }

  public static String getParameterExpression(GenOperation genOperation)
  {
    StringBuffer result = new StringBuffer();
    result.append('(');

    for (Iterator i = genOperation.getGenParameters().iterator(); i.hasNext(); )
    {
      result.append(getTypeExpression(genOperation));
      if (i.hasNext())
      {
        result.append(", ");
      }
    }
    result.append(')');
    return result.toString();
  }

  
}

Back to the top