001    package org.apache.turbine.util;
002    
003    
004    /*
005     * Licensed to the Apache Software Foundation (ASF) under one
006     * or more contributor license agreements.  See the NOTICE file
007     * distributed with this work for additional information
008     * regarding copyright ownership.  The ASF licenses this file
009     * to you under the Apache License, Version 2.0 (the
010     * "License"); you may not use this file except in compliance
011     * with the License.  You may obtain a copy of the License at
012     *
013     *   http://www.apache.org/licenses/LICENSE-2.0
014     *
015     * Unless required by applicable law or agreed to in writing,
016     * software distributed under the License is distributed on an
017     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
018     * KIND, either express or implied.  See the License for the
019     * specific language governing permissions and limitations
020     * under the License.
021     */
022    
023    
024    import java.util.Vector;
025    
026    /**
027     * A message class for holding information about a message that
028     * relates to a specific form and field.  Used together with
029     * FormMessages class.
030     *
031     * @author <a href="mailto:neeme@one.lv">Neeme Praks</a>
032     * @version $Id: FormMessage.java 1073174 2011-02-21 22:18:45Z tv $
033     */
034    public class FormMessage
035    {
036        private String message;
037        private String formName;
038        private final Vector<String> fieldNames;
039    
040        /**
041         * Constructor.
042         */
043        public FormMessage()
044        {
045            fieldNames = new Vector<String>();
046        }
047    
048        /**
049         * Constructor.
050         *
051         * @param formName A String with the form name.
052         */
053        public FormMessage(String formName)
054        {
055            this();
056            setFormName(formName);
057        }
058    
059        /**
060         * Constructor.
061         *
062         * @param formName A String with the form name.
063         * @param fieldName A String with the field name.
064         */
065        public FormMessage(String formName,
066                           String fieldName)
067        {
068            this(formName);
069            setFieldName(fieldName);
070        }
071    
072        /**
073         * Constructor.
074         *
075         * @param formName A String with the form name.
076         * @param fieldName A String with the field name.
077         * @param message A String with the message.
078         */
079        public FormMessage(String formName,
080                           String fieldName,
081                           String message)
082        {
083            this(formName, fieldName);
084            setMessage(message);
085        }
086    
087        /**
088         * Return the message.
089         *
090         * @return A String with the message.
091         */
092        public String getMessage()
093        {
094            return message;
095        }
096    
097        /**
098         * Return the form name.
099         *
100         * @return A String with the form name.
101         */
102        public String getFormName()
103        {
104            return formName;
105        }
106    
107        /**
108         * Return the field names.
109         *
110         * @return A String[] with the field names.
111         */
112        public String[] getFieldNames()
113        {
114            String[] result = new String[fieldNames.size()];
115            fieldNames.copyInto(result);
116            return result;
117        }
118    
119        /**
120         * Set the message.
121         *
122         * @param message A String with the message.
123         */
124        public void setMessage(String message)
125        {
126            this.message = message;
127        }
128    
129        /**
130         * Set the form name.
131         *
132         * @param formName A String with the form name.
133         */
134        public void setFormName(String formName)
135        {
136            this.formName = formName;
137        }
138    
139        /**
140         * Adds one field name.
141         *
142         * @param fieldName A String with the field name.
143         */
144        public void setFieldName(String fieldName)
145        {
146            fieldNames.addElement(fieldName);
147        }
148    
149        /**
150         * Write out the contents of the message in a friendly manner.
151         *
152         */
153        @Override
154        public String toString()
155        {
156            StringBuffer sb = new StringBuffer("formName:" + getFormName() + ", fieldNames:");
157            for (int i = 0; i< getFieldNames().length; i++){
158                sb.append(getFieldNames()[i] + " ");
159            }
160            sb.append(", message:" + getMessage());
161    
162            return sb.toString();
163        }
164    }