KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > core > comp > renderer > html > NamingHelper


1 /*
2  * Copyright (C) 2003 Christian Cryder [christianc@granitepeaks.com]
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * $Id: NamingHelper.java,v 1.6 2004/02/01 05:16:27 christianc Exp $
19  */

20 package org.enhydra.barracuda.core.comp.renderer.html;
21
22 import java.util.*;
23
24 import org.apache.log4j.*;
25 import org.w3c.dom.*;
26 import org.w3c.dom.html.*;
27
28
29 /**
30  * Simple helper class to assist with determining an elements name.
31  * Created csc_100201.2
32  */

33 public class NamingHelper {
34
35     protected static final Logger logger = Logger.getLogger(NamingHelper.class.getName());
36     
37     /**
38      * Simple helper method to get a name for an element.
39      * If the name is not set, a unique name will be generated,
40      * and the name attribute in the element will be set accordingly.
41      * Only works for HTML elements that actually support the name
42      * attribute (otherwise just returns "")
43      *
44      * @param el a target element
45      * @return the String value of the name of the passed in Element
46      */

47     public static String JavaDoc getName(Element el) {
48         String JavaDoc name = "";
49         if (el instanceof HTMLAnchorElement ||
50             el instanceof HTMLAppletElement ||
51             el instanceof HTMLButtonElement ||
52             el instanceof HTMLFormElement ||
53             el instanceof HTMLFrameElement ||
54             el instanceof HTMLIFrameElement ||
55             el instanceof HTMLInputElement ||
56             el instanceof HTMLMapElement ||
57             el instanceof HTMLMetaElement ||
58             el instanceof HTMLObjectElement ||
59             el instanceof HTMLParamElement ||
60             el instanceof HTMLSelectElement ||
61             el instanceof HTMLTextAreaElement) {
62
63             name = el.getAttribute("name");
64             if (name==null || name.equals("")) {
65                 name = "el_"+new Object JavaDoc().hashCode();
66                 el.setAttribute("name", name);
67             }
68         }
69         return name;
70     }
71     
72 }
73
Popular Tags