Skip to main content
summaryrefslogtreecommitdiffstats
blob: 18b3b62960a3d871f6d299c8300f617e380d1e2b (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
/*
 * Copyright (c) 2004 - 2012 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:
 *    Martin Taal - initial API and implementation
 *    Eike Stepper - maintenance
 */
package org.eclipse.emf.cdo.server.hibernate.internal.teneo;

import org.eclipse.emf.cdo.server.hibernate.IHibernateMappingProvider;

import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import java.util.Properties;

/**
 * Reads the hibernate mapping file from one or more resource locations and adds them to the configuration.
 * 
 * @author Eike Stepper
 * @since 3.0
 */
public class TeneoHibernateMappingProviderFactory implements IHibernateMappingProvider.Factory
{
  public static final String TYPE = "teneo"; //$NON-NLS-1$

  private static final String PROPERTY_TAG = "property"; //$NON-NLS-1$

  private static final String EXTENSION_TAG = "extension"; //$NON-NLS-1$

  private static final String NAME_ATTR = "name"; //$NON-NLS-1$

  private static final String VALUE_ATTR = "value"; //$NON-NLS-1$

  public TeneoHibernateMappingProviderFactory()
  {
  }

  public String getType()
  {
    return TYPE;
  }

  public TeneoHibernateMappingProvider create(Element config)
  {

    final Properties properties = new Properties();
    final NodeList propertyNodes = config.getElementsByTagName(PROPERTY_TAG);
    for (int i = 0; i < propertyNodes.getLength(); i++)
    {
      final Element propertyElement = (Element)propertyNodes.item(i);
      properties.setProperty(propertyElement.getAttribute(NAME_ATTR), propertyElement.getAttribute(VALUE_ATTR));
    }

    final TeneoHibernateMappingProvider mappingProvider = new TeneoHibernateMappingProvider();
    mappingProvider.setMappingProviderProperties(properties);
    collectExtensions(config, mappingProvider);
    return mappingProvider;
  }

  private void collectExtensions(Element config, TeneoHibernateMappingProvider mappingProvider)
  {
    final NodeList extensionNodes = config.getElementsByTagName(EXTENSION_TAG);
    for (int i = 0; i < extensionNodes.getLength(); i++)
    {
      final Element extensionElement = (Element)extensionNodes.item(i);
      final String nameAttrValue = extensionElement.getAttribute(NAME_ATTR);
      final String valueAttrValue = extensionElement.getAttribute(VALUE_ATTR);
      if (nameAttrValue == null || valueAttrValue == null)
      {
        throw new IllegalArgumentException(
            "Extension element has incorrect format, both the name and value attribute should be present"); //$NON-NLS-1$
      }

      mappingProvider.putExtension(nameAttrValue, valueAttrValue);
    }
  }
}

Back to the top