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: Parameter.java,v 1.2 2008/06/25 05:47:08 qcheng Exp $
026 *
027 */
028
029
030package com.sun.identity.liberty.ws.authnsvc.protocol;
031
032import org.w3c.dom.Document;
033import org.w3c.dom.Element;
034
035import com.sun.identity.shared.xml.XMLUtils;
036import com.sun.identity.liberty.ws.authnsvc.AuthnSvcConstants;
037import com.sun.identity.liberty.ws.authnsvc.AuthnSvcException;
038
039/**
040 * The <code>Parameter</code> class represents 'Parameter' element in
041 * 'Transform' element in 'PasswordTransforms' element defined in
042 * Authentication Service schema.
043 * @supported.all.api
044 */
045public class Parameter {
046
047    /**
048     * Parameter name 'length'
049     */
050    public static final String NAME_LENGTH = "length";
051
052    /**
053     * Parameter name 'allowed'
054     */
055    public static final String NAME_ALLOWED = "allowed";
056
057    private String name = null;
058    private String value = null;
059
060    /**
061     * Constructor takes the value of 'name' attribute and value
062     * of 'Transform' element.
063     * @param name value of 'name' attribute
064     * @param value value of 'Transform' element
065     */
066    public Parameter(String name, String value) {
067        this.name = name;
068        this.value = value;
069    }
070
071    /**
072     * Constructor takes a <code>org.w3c.dom.Element</code>.
073     * @param element a Parameter element
074     * @exception AuthnSvcException if an error occurs while parsing
075     *                      the Parameter element
076     */
077    Parameter(Element element) throws AuthnSvcException
078    {
079        name = XMLUtils.getNodeAttributeValue(element,
080                                              AuthnSvcConstants.ATTR_NAME);
081        if (name == null || name.length() == 0) {
082            throw new AuthnSvcException("missingNamePM");
083        }
084
085        value = XMLUtils.getElementValue(element);
086    }
087
088    /**
089     * Returns value of 'name' attribute.
090     * @return value of 'name' attribute
091     * @see #setName(String)
092     */
093    public String getName()
094    {
095        return name;
096    }
097
098    /**
099     * Returns value of 'Parameter' element.
100     * @return value of 'Parameter' element
101     * @see #setValue(String)
102     */
103    public String getValue()
104    {
105        return value;
106    }
107
108    /**
109     * Sets value of 'name' attribute.
110     * @param name value of 'name' attribute
111     * @see #getName()
112     */
113    public void setName(String name)
114    {
115        this.name = name;
116    }
117
118    /**
119     * Sets value of 'Parameter' element.
120     * @param value value of 'Parameter' element
121     * @see #getValue()
122     */
123    public void setValue(String value)
124    {
125        this.value = value;
126    }
127
128    /**
129     * Converts this to <code>org.w3c.dom.Element</code> and add to
130     * parent Transform Element.
131     * @param tfE parent Transform Element
132     */
133    void addToParent(Element tfE) throws AuthnSvcException
134    {
135        if (name == null || name.length() == 0) {
136            throw new AuthnSvcException("missingNamePM");
137        }
138
139        Document doc = tfE.getOwnerDocument();
140        Element pmE = doc.createElementNS(AuthnSvcConstants.NS_AUTHN_SVC,
141                                          AuthnSvcConstants.PTAG_PARAMETER);
142        tfE.appendChild(pmE);
143
144        pmE.setAttributeNS(null, AuthnSvcConstants.ATTR_NAME, name);
145
146        if (value != null) {
147            pmE.appendChild(doc.createTextNode(value));
148        }
149    }
150}