Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 1bdbaa1a3f45128e300883698da5557a17e76242 (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
/**
 * Copyright (c) 2004 - 2011 Eike Stepper (Berlin, Germany) 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:
 *    Eike Stepper - initial API and implementation
 */
package org.eclipse.emf.cdo.server.internal.db.mapping;

import org.eclipse.emf.cdo.server.db.mapping.ITypeMapping;
import org.eclipse.emf.cdo.server.internal.db.messages.Messages;

import org.eclipse.net4j.db.DBType;

import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.EPackage;

import java.text.MessageFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Stefan Winkler
 */
public class TypeMappingUtil
{
  private static final Pattern FACTORY_DESCRIPTOR_PATTERN = Pattern.compile("(.+);(.+)#(.+)->(.+)");

  /**
   * Utility class - no instantiation.
   */
  private TypeMappingUtil()
  {
  }

  public static ITypeMapping.Descriptor createDescriptor(String id, EClassifier eClassifier, DBType dbType)
  {
    String factoryType = createFactoryType(id, eClassifier, dbType);
    return new TypeMappingDescriptor(id, factoryType, eClassifier, dbType);
  }

  public static String createFactoryType(String id, EClassifier eClassifier, DBType dbType)
  {
    StringBuilder builder = new StringBuilder();

    // id
    builder.append(id);
    builder.append(";");

    // classifier
    builder.append(eClassifier.getEPackage().getNsURI());
    builder.append("#");
    builder.append(eClassifier.getName());
    builder.append("->");

    // dbtype
    builder.append(dbType.getKeyword());

    return builder.toString();
  }

  public static ITypeMapping.Descriptor descriptorFromFactoryType(String factoryType) throws FactoryTypeParserException
  {
    Matcher matcher = FACTORY_DESCRIPTOR_PATTERN.matcher(factoryType);

    if (!matcher.matches())
    {
      throw new FactoryTypeParserException(MessageFormat.format(Messages.getString("FactoryTypeParserException.1"),
          factoryType));
    }

    String id = matcher.group(1);
    String packageUri = matcher.group(2);
    String classifierName = matcher.group(3);
    String typeKeyword = matcher.group(4);

    EPackage ePackage = EPackage.Registry.INSTANCE.getEPackage(packageUri);
    if (ePackage == null)
    {
      throw new FactoryTypeParserException(MessageFormat.format(Messages.getString("FactoryTypeParserException.2"),
          packageUri, factoryType));
    }

    EClassifier eClassifier = ePackage.getEClassifier(classifierName);
    if (eClassifier == null)
    {
      throw new FactoryTypeParserException(MessageFormat.format(Messages.getString("FactoryTypeParserException.3"),
          classifierName, factoryType));
    }

    DBType dbType = DBType.getTypeByKeyword(typeKeyword);
    if (dbType == null)
    {
      throw new FactoryTypeParserException(MessageFormat.format(Messages.getString("FactoryTypeParserException.4"),
          dbType, factoryType));
    }

    return new TypeMappingDescriptor(id, factoryType, eClassifier, dbType);
  }

  public static class FactoryTypeParserException extends Exception
  {
    private static final long serialVersionUID = 1L;

    public FactoryTypeParserException(String desc)
    {
      super(desc);
    }
  }
}

Back to the top