001/**
002 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003 *
004 * Copyright (c) 2006 Sun Microsystems Inc. All Rights Reserved
005 *
006 * The contents of this file are subject to the terms
007 * of the Common Development and Distribution License
008 * (the License). You may not use this file except in
009 * compliance with the License.
010 *
011 * You can obtain a copy of the License at
012 * https://opensso.dev.java.net/public/CDDLv1.0.html or
013 * opensso/legal/CDDLv1.0.txt
014 * See the License for the specific language governing
015 * permission and limitations under the License.
016 *
017 * When distributing Covered Code, include this CDDL
018 * Header Notice in each file and include the License file
019 * at opensso/legal/CDDLv1.0.txt.
020 * If applicable, add the following below the CDDL Header,
021 * with the fields enclosed by brackets [] replaced by
022 * your own identifying information:
023 * "Portions Copyrighted [year] [name of copyright owner]"
024 *
025 * $Id: IDPEntries.java,v 1.2 2008/06/25 05:46:47 qcheng Exp $
026 *
027 */
028
029
030package com.sun.identity.federation.message.common;
031
032import java.util.List;
033import java.util.Collections;
034import java.util.Iterator;
035import java.util.ArrayList;
036
037import org.w3c.dom.Element;
038import org.w3c.dom.Node;
039import org.w3c.dom.NodeList;
040
041import com.sun.identity.saml.common.SAMLUtils;
042import com.sun.identity.federation.common.FSUtils;
043import com.sun.identity.federation.common.IFSConstants;
044
045/**
046 * This class defines methods to set/retrieve multiple Identity Providers.
047 *
048 * @supported.all.api
049 */
050
051public class IDPEntries {
052    private List idpEntryList = null;
053    private List otherElements = null;
054    
055    /**
056     * Default Constructor.
057     */
058    public IDPEntries() {
059    }
060    
061    
062    /**
063     * Constructor creates <code>IDPEntries</code> object.
064     *
065     * @param idpEntries list of identity providers.
066     */
067    public IDPEntries(List idpEntries) {
068        this.idpEntryList = idpEntries;
069    }
070    
071    /**
072     * Returns the list of Identity Providers.
073     *
074     * @return  list of Identity Providers.
075     * @see #setIDPEntryList(List)
076     */
077    public List getIDPEntryList() {
078        return idpEntryList;
079    }
080    
081    /**
082     * Sets the list of Identity Providers.
083     *
084     * @param idpEntryList the list of Identity Providers.
085     * @see #getIDPEntryList
086     */
087    public void setIDPEntryList(List idpEntryList) {
088        this.idpEntryList = idpEntryList;
089    }
090    
091    /**
092     * Returns the string representation of this object.
093     * This method translates the response to an XML document string.
094     *
095     * @return An XML String representing the response. NOTE: this is a
096     *         complete SAML response xml string with ResponseID,
097     *         MajorVersion, etc.
098     */
099    
100    public String toXMLString() throws FSMsgException {
101        return toXMLString(true, true);
102    }
103    
104    /**
105     * Returns a String representation of this object.
106     *
107     * @param includeNS : Determines whether or not the namespace qualifier
108     *        is prepended to the Element when converted
109     * @param declareNS : Determines whether or not the namespace is declared
110     *        within the Element.
111     * @return a string containing the valid XML for this element
112     * @throws FSMsgException if there is an error converting
113     *         this object ot a string.
114     */
115    
116    public String toXMLString(boolean includeNS, boolean declareNS)
117    throws FSMsgException {
118        return toXMLString(includeNS, declareNS, false);
119    }
120    
121    /**
122     * Returns a String representation of this object.
123     *
124     * @param includeNS Determines whether or not the namespace qualifier
125     *        is prepended to the Element when converted
126     * @param declareNS Determines whether or not the namespace is declared
127     *        within the Element.
128     * @param includeHeader Determines whether the output include the xml
129     *        declaration header.
130     * @return a string containing the valid XML for this element
131     * @throws FSMsgException if there is an error converting
132     *         this object ot a string.
133     */
134    public String toXMLString(boolean includeNS,boolean declareNS,
135            boolean includeHeader) throws FSMsgException {
136        StringBuffer xml = new StringBuffer(300);
137        if (includeHeader) {
138            xml.append("<?xml version=\"1.0\" encoding=\"").
139                    append(IFSConstants.DEFAULT_ENCODING).append("\" ?>\n");
140        }
141        String prefix = "";
142        String uri = "";
143        if (includeNS) {
144            prefix = IFSConstants.LIB_PREFIX;
145        }
146        if (declareNS) {
147            uri = IFSConstants.LIB_NAMESPACE_STRING;
148        }
149        
150        xml.append("<").append(prefix).append("IDPEntries").append(uri).
151                append(">\n");
152        
153        
154        if((idpEntryList != null) && (idpEntryList != Collections.EMPTY_LIST)){
155            Iterator i = idpEntryList.iterator();
156            
157            while (i.hasNext()) {
158                IDPEntry entry = (IDPEntry)i.next();
159                xml.append(entry.toXMLString(true, false));
160            }
161        }
162        xml.append("</").append(prefix).append("IDPEntries>\n");
163        
164        return xml.toString();
165    }
166    /**
167     * Constructor creates <code>IDPEntries</code> object from
168     * a Document Element.
169     *
170     * @param root the Document Element object.
171     * @throws FSMsgException on error.
172     */
173    public IDPEntries(Element root) throws FSMsgException {
174        if (root == null) {
175            SAMLUtils.debug.message("IDPEntries.parseXML: null input.");
176            throw new FSMsgException("nullInput",null);
177        }
178        String tag = null;
179        if (((tag = root.getLocalName()) == null) ||
180                (!tag.equals("IDPEntries"))) {
181            FSUtils.debug.message("IDPEntries.parseXML: wrong input.");
182            throw new FSMsgException("wrongInput",null);
183        }
184        NodeList nl = root.getChildNodes();
185        Node child;
186        String childName;
187        int length = nl.getLength();
188        for (int i = 0; i < length; i++) {
189            child = nl.item(i);
190            if ((childName = child.getLocalName()) != null) {
191                if (childName.equals("IDPEntry")) {
192                    if ((idpEntryList == null) ||
193                            (idpEntryList == Collections.EMPTY_LIST)) {
194                        idpEntryList = new ArrayList();
195                    }
196                    idpEntryList.add(new IDPEntry((Element)child));
197                }else{
198                }
199            }
200        }
201    }
202}