01: /**
02: * Objective Database Abstraction Layer (ODAL)
03: * Copyright (c) 2004, The ODAL Development Group
04: * All rights reserved.
05: * For definition of the ODAL Development Group please refer to LICENCE.txt file
06: *
07: * Distributable under LGPL license.
08: * See terms of license at gnu.org.
09: */package com.completex.objective.components.persistency.meta.impl;
10:
11: import com.completex.objective.components.persistency.meta.MetaModel;
12: import com.completex.objective.components.persistency.meta.ModelLoader;
13:
14: import java.io.File;
15: import java.io.FileNotFoundException;
16: import java.io.FileReader;
17: import java.io.IOException;
18: import java.io.Reader;
19: import java.util.Map;
20:
21: /**
22: * @author Gennady Krizhevsky
23: */
24: public abstract class FileModelLoaderImpl implements ModelLoader {
25:
26: private String modeDescriptorFilePath;
27: private Reader reader;
28: private String filterPattern;
29: private boolean debug;
30:
31: protected FileModelLoaderImpl(String descriptorPath,
32: String filterPattern) {
33: this .modeDescriptorFilePath = descriptorPath;
34: this .filterPattern = filterPattern;
35: }
36:
37: public FileModelLoaderImpl(Reader reader, String filterPattern) {
38: this .reader = reader;
39: this .filterPattern = filterPattern;
40: }
41:
42: public MetaModel load() throws Exception {
43: return this .load(new MetaModel(filterPattern));
44: }
45:
46: public MetaModel load(MetaModel model) throws Exception {
47: if (model == null) {
48: model = new MetaModel(filterPattern);
49: }
50: try {
51:
52: if (modeDescriptorFilePath != null) {
53: // 1st check if file exists - if not - exit:
54: String fileName = modeDescriptorFilePath;
55: File file = new File(fileName);
56: if (!file.exists() || file.isDirectory()) {
57: info("FileModelLoaderImpl::load(): persistency descriptor file ["
58: + fileName
59: + "] does not exist - exiting load()");
60: return null;
61: } else {
62: info("FileModelLoaderImpl::load(): loading persistency descriptor from file ["
63: + fileName + "]");
64: }
65: reader = new FileReader(file);
66: } else if (reader == null) {
67: throw new RuntimeException(
68: "Both modeDescriptorFilePath & reader are not set");
69: }
70:
71: Map modelMap = readModelMap(reader);
72: info("modelMap = " + modelMap);
73:
74: model = toModel(model, modelMap);
75: info("model = " + model);
76:
77: } catch (FileNotFoundException e) {
78: throw new RuntimeException(
79: "Cannot transformRead from file "
80: + modeDescriptorFilePath, e);
81: }
82: return model;
83: }
84:
85: abstract protected MetaModel toModel(MetaModel model, Map modelMap);
86:
87: abstract protected Map readModelMap(Reader reader)
88: throws IOException;
89:
90: private void info(String s) {
91: if (debug) {
92: System.out.println(s);
93: }
94: }
95:
96: }
|