001/*
002 * jPOS Project [http://jpos.org]
003 * Copyright (C) 2000-2026 jPOS Software SRL
004 *
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU Affero General Public License as
007 * published by the Free Software Foundation, either version 3 of the
008 * License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 * GNU Affero General Public License for more details.
014 *
015 * You should have received a copy of the GNU Affero General Public License
016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017 */
018
019package org.jpos.iso.validator;
020
021import org.jpos.iso.*;
022import org.jpos.util.LogSource;
023import org.jpos.util.Loggeable;
024import org.jpos.util.Logger;
025
026import java.io.PrintStream;
027import java.util.Iterator;
028import java.util.Vector;
029import java.util.Map;
030
031/**
032 * Parse ISOComponents and put the errors into a list.
033 * <p>Title: jPOS</p>
034 * <p>Description: Java Framework for Financial Systems</p>
035 * <p>Copyright: Copyright (c) 2000 jPOS.org.  All rights reserved.</p>
036 * <p>Company: www.jPOS.org</p>
037 * @author Jose Eduardo Leon
038 * @version 1.0
039 */
040@SuppressWarnings("unchecked")
041public class VErrorParser implements LogSource, Loggeable  {
042    /** Default constructor; no instance state to initialise. */
043    public VErrorParser() {}
044
045    /**
046     * Parse an ISOComponent and get an error vector.
047     * @param c Component to parse.
048     * @return error vector.
049     */
050    public Vector getVErrors( ISOComponent c ) {
051        Vector v = new Vector();
052        _getErr( c, v, "" );
053        _errors = v;
054        return _errors;
055    }
056
057    /**
058     * Returns an XML rendering of the parsed error list.
059     *
060     * @return the XML string (currently a stub; always empty)
061     */
062    public String parseXMLErrorList(){
063        /** @todo !!!!!!!! */
064        return "";
065    }
066
067    public void setLogger(Logger logger, String realm) {
068        this.logger = logger;
069        this.realm = realm;
070    }
071    public String getRealm() {
072        return realm;
073    }
074    public Logger getLogger() {
075        return logger;
076    }
077
078    /**
079     * Parse error list, and get an dump
080     * the xml string representing the list.
081     * <pre>{@code
082     * Ex:
083     * <isomsg>
084     *   <field id="2">
085     *     <error description="Invalid Len Error" reject-code="101"/>
086     *   </field>
087     *   <field id="48">
088     *     <field id="0">
089     *       <field id="1">
090     *         <error description="Invalid Value Error" reject-code="102"/>
091     *       </field>
092     *     </field>
093     *   </field>
094     *   <error description="Field Expected Error" reject-code="999">
095     * </isomsg>
096     * }</pre>
097     * @param p output stream
098     * @param indent indent character
099     */
100    public void dump(PrintStream p, String indent) {
101        /** @todo !!!!!!!!! */
102    }
103
104    /**
105     * Free errors memory.
106     */
107    public void resetErrors(){
108        _errors = null;
109    }
110
111    /**
112     * Recursive method to get the errors.
113     */
114    private void _getErr ( ISOComponent c, Vector list, String id ) {
115        if ( c instanceof ISOVField ){
116            Iterator iter = ((ISOVField)c).errorListIterator();
117            while (iter.hasNext()) {
118                ISOVError error = (ISOVError)iter.next();
119                error.setId( id );
120                list.add( error );
121            }
122        }
123        else if ( c instanceof ISOMsg ){
124            if ( c instanceof ISOVMsg ){
125                /** Msg level error **/
126                Iterator iter = ((ISOVMsg)c).errorListIterator();
127                while (iter.hasNext()) {
128                    ISOVError error = (ISOVError)iter.next();
129                    error.setId( id );
130                    list.add( error );
131                }
132            }
133            /** recursively in childs **/
134            Map fields = c.getChildren();
135            int max = c.getMaxField();
136            for (int i = 0; i <= max ; i++)
137                if ((c=(ISOComponent) fields.get (i)) != null )
138                    _getErr( c, list, id +  Integer.toString(i) + " " );
139        }
140    }
141
142    /** Logger receiving parser diagnostic events. */
143    protected Logger logger = null;
144    /** Logger realm associated with this parser. */
145    protected String realm=null;
146    private Vector _errors = null;
147}