001: package org.makumba.providers.datadefinition.makumba;
002:
003: import java.io.File;
004: import java.net.URISyntaxException;
005: import java.util.Vector;
006:
007: import org.makumba.DataDefinition;
008: import org.makumba.FieldDefinition;
009: import org.makumba.providers.DataDefinitionProviderInterface;
010:
011: /**
012: * This class is the Makumba implementation of a data definition provider, based on MDD files. TODO refactor together
013: * with RecordInfo to build objects (and not use static methods)
014: *
015: * @author Manuel Gay
016: * @version $Id: MakumbaDataDefinitionFactory.java 2035 2007-11-07 20:32:24Z manuel_gay $
017: */
018: public class MakumbaDataDefinitionFactory implements
019: DataDefinitionProviderInterface {
020:
021: /**
022: * {@inheritdoc} The type a.b.C will generate a lookup for the file CLASSPATH/a/b/C.mdd and then for
023: * CLASSPATH/dataDefinitions/a/b/C.mdd
024: */
025: public DataDefinition getDataDefinition(String typeName) {
026: return RecordInfo
027: .getRecordInfo(typeName.replaceAll("__", "->"));
028: }
029:
030: /**
031: * {@inheritdoc}
032: */
033: public DataDefinition getVirtualDataDefinition(String name) {
034: return new RecordInfo(name.replaceAll("__", "->"));
035: }
036:
037: /**
038: * {@inheritdoc}
039: */
040: public FieldDefinition makeFieldDefinition(String name,
041: String definition) {
042: return FieldInfo.getFieldInfo(name.replaceAll("__", "->"),
043: definition.replaceAll("__", "->"), true);
044: }
045:
046: /**
047: * {@inheritdoc}
048: */
049: public FieldDefinition makeFieldOfType(String name, String type) {
050: return FieldInfo.getFieldInfo(name.replaceAll("__", "->"), type
051: .replaceAll("__", "->"), false);
052: }
053:
054: /**
055: * {@inheritdoc}
056: */
057: public FieldDefinition makeFieldOfType(String name, String type,
058: String description) {
059: return FieldInfo.getFieldInfo(name.replaceAll("__", "->"), type
060: .replaceAll("__", "->"), false, description);
061: }
062:
063: /**
064: * {@inheritdoc}
065: */
066: public FieldDefinition makeFieldWithName(String name,
067: FieldDefinition type) {
068: return FieldInfo.getFieldInfo(name, type, false);
069: }
070:
071: /**
072: * {@inheritdoc}
073: */
074: public FieldDefinition makeFieldWithName(String name,
075: FieldDefinition type, String description) {
076: return FieldInfo.getFieldInfo(name, type, false, description);
077: }
078:
079: public Vector getDataDefinitionsInLocation(String location) {
080: return mddsInDirectory(location);
081: }
082:
083: /**
084: * Discover mdds in a directory in classpath.
085: *
086: * @return filenames as Vector of Strings.
087: */
088: private java.util.Vector mddsInDirectory(String dirInClasspath) {
089: java.util.Vector mdds = new java.util.Vector();
090: try {
091: java.net.URL u = org.makumba.commons.ClassResource
092: .get(dirInClasspath);
093: // we need to create the file path with this methode. rather than u.getFile(), as that method would keep
094: // e.g. %20 for spaces in the path, which fails on windows.
095: java.io.File dir = new File(u.toURI());
096: fillMdds(dir.toString().length() + 1, dir, mdds);
097: } catch (URISyntaxException e) {
098: e.printStackTrace();
099: }
100: return mdds;
101: }
102:
103: private void fillMdds(int baselength, java.io.File dir,
104: java.util.Vector<String> mdds) {
105: if (dir.isDirectory()) {
106: String[] list = dir.list();
107: for (int i = 0; i < list.length; i++) {
108: String s = list[i];
109: if (s.endsWith(".mdd")) {
110: s = dir.toString() + java.io.File.separatorChar + s;
111: s = s.substring(baselength, s.length() - 4); // cut off the ".mdd"
112: s = s.replace(java.io.File.separatorChar, '.');
113: mdds.add(s);
114: } else {
115: java.io.File f = new java.io.File(dir, s);
116: if (f.isDirectory())
117: fillMdds(baselength, f, mdds);
118: }
119: }
120: }
121: }
122:
123: public MakumbaDataDefinitionFactory() {
124:
125: }
126:
127: }
|