001/*
002  GRANITE DATA SERVICES
003  Copyright (C) 2011 GRANITE DATA SERVICES S.A.S.
004
005  This file is part of Granite Data Services.
006
007  Granite Data Services is free software; you can redistribute it and/or modify
008  it under the terms of the GNU Library General Public License as published by
009  the Free Software Foundation; either version 2 of the License, or (at your
010  option) any later version.
011
012  Granite Data Services is distributed in the hope that it will be useful, but
013  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
015  for more details.
016
017  You should have received a copy of the GNU Library General Public License
018  along with this library; if not, see <http://www.gnu.org/licenses/>.
019*/
020
021package org.granite.generator;
022
023import java.io.IOException;
024import java.util.ArrayList;
025import java.util.Collection;
026
027import org.granite.generator.exception.GenerationException;
028import org.granite.generator.exception.TransformerNotFoundException;
029
030/**
031 * @author Franck WOLFF
032 */
033public class Generator extends ArrayList<Transformer<?, ?, ?>> {
034
035        ///////////////////////////////////////////////////////////////////////////
036        // Fields.
037
038        private static final long serialVersionUID = 1L;
039
040        public static final String VERSION = "3.0.0";
041        
042        private Configuration config = null;
043
044        ///////////////////////////////////////////////////////////////////////////
045        // Constructors.
046        
047        public Generator() {
048        }
049        
050        public Generator(Configuration config) {
051                this.config = config;
052        }
053
054        ///////////////////////////////////////////////////////////////////////////
055        // Getters/Setters.
056        
057        public Configuration getConfig() {
058                return config;
059        }
060
061        public void setConfig(Configuration config) {
062                this.config = config;
063        }
064
065        ///////////////////////////////////////////////////////////////////////////
066        // Generation method.
067        
068        public Output<?>[] generate(Input<?> input) throws IOException, GenerationException {
069                updateTransformers();
070                
071                Transformer<?, ?, ?> transformer = null;
072                
073                for (Transformer<?, ?, ?> t : this) {
074                        if (t.accept(input)) {
075                                transformer = t;
076                                break;
077                        }
078                }
079                
080                if (transformer == null)
081                        throw new TransformerNotFoundException(input);
082                
083                return transformer.generate(input);
084        }
085
086        ///////////////////////////////////////////////////////////////////////////
087        // Utilities.
088
089        protected void updateTransformers() {
090                for (Transformer<?, ?, ?> transformer : this)
091                        transformer.setConfig(config);
092        }
093        
094        protected boolean checkAdd(Transformer<?, ?, ?> transformer) {
095                if (transformer == null)
096                        throw new NullPointerException("A transformer cannot be null");
097                return !contains(transformer);
098        }
099
100        ///////////////////////////////////////////////////////////////////////////
101        // ArrayList overridden methods (check for null & duplicates).
102
103        @Override
104        public void add(int index, Transformer<?, ?, ?> transformer) {
105                if (checkAdd(transformer))
106                        super.add(index, transformer);
107        }
108
109        @Override
110        public boolean add(Transformer<?, ?, ?> transformer) {
111                if (checkAdd(transformer))
112                        return super.add(transformer);
113                return false;
114        }
115
116        @Override
117        public boolean addAll(Collection<? extends Transformer<?, ?, ?>> transformers) {
118                boolean changed = false;
119                for (Transformer<?, ?, ?> transformer : transformers) {
120                        if (checkAdd(transformer)) {
121                                super.add(transformer);
122                                changed = true;
123                        }
124                }
125                return changed;
126        }
127
128        @Override
129        public boolean addAll(int index, Collection<? extends Transformer<?, ?, ?>> transformers) {
130                boolean changed = false;
131                for (Transformer<?, ?, ?> transformer : transformers) {
132                        if (checkAdd(transformer)) {
133                                super.add(index++, transformer);
134                                changed = true;
135                        }
136                }
137                return changed;
138        }
139
140        @Override
141        public Transformer<?, ?, ?> set(int index, Transformer<?, ?, ?> transformer) {
142                if (checkAdd(transformer))
143                        return super.set(index, transformer);
144                return null;
145        }
146}