001    package org.apache.ojb.broker.metadata.torque;
002    
003    /* Copyright 2002-2005 The Apache Software Foundation
004     *
005     * Licensed under the Apache License, Version 2.0 (the "License");
006     * you may not use this file except in compliance with the License.
007     * You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    import org.apache.ojb.broker.metadata.IndexDescriptor;
019    
020    import java.util.Vector;
021    
022    public class TorqueIndexGenerator {
023    
024        public void generateIndices(Vector indexDescriptors, StringBuffer buffer) {
025            int numIndexes = indexDescriptors.size();
026            for (int i = 0; i < numIndexes; i++) {
027                IndexDescriptor index = (IndexDescriptor) indexDescriptors.get(i);
028                String indexTag = getIndexTag(index);
029    
030                buffer.append("        <");
031                buffer.append(indexTag);
032                if (index.getName() != null) {
033                    buffer.append(" name=\"");
034                    buffer.append(index.getName());
035                    buffer.append("\"");
036                }
037                buffer.append(">\n");
038    
039                generateStringVector(index.getIndexColumns(), indexTag + "-column", "name", "            ", buffer);
040                buffer.append("        </");
041                buffer.append(indexTag);
042                buffer.append(">\n");
043            }
044        }
045    
046        private String getIndexTag(IndexDescriptor indexDescriptor) {
047            if (indexDescriptor.isUnique()) {
048                return "unique";
049            } else {
050                return "index";
051            }
052        }
053    
054        private void generateStringVector(Vector strings, String element, String attribute, String indentation, StringBuffer buffer) {
055            for (int i = 0; i < strings.size(); i++) {
056                buffer.append(indentation);
057                buffer.append("<");
058                buffer.append(element);
059                buffer.append(" ");
060                buffer.append(attribute);
061                buffer.append("=\"");
062                buffer.append(strings.get(i));
063                buffer.append("\"/>\n");
064            }
065        }
066    
067    }