KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mr > core > util > xml > sax > NonNSAttributes


1 package org.mr.core.util.xml.sax;
2
3 import org.xml.sax.Attributes JavaDoc;
4 import org.xml.sax.helpers.AttributesImpl JavaDoc;
5
6 import java.util.Collections JavaDoc;
7 import java.util.Iterator JavaDoc;
8 import java.util.LinkedList JavaDoc;
9
10 /*
11  * Copyright 2002 by
12  * <a HREF="http://www.coridan.com">Coridan</a>
13  * <a HREF="mailto: support@coridan.com ">support@coridan.com</a>
14  *
15  * The contents of this file are subject to the Mozilla Public License Version
16  * 1.1 (the "License"); you may not use this file except in compliance with the
17  * License. You may obtain a copy of the License at
18  * http://www.mozilla.org/MPL/
19  *
20  * Software distributed under the License is distributed on an "AS IS" basis,
21  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
22  * for the specific language governing rights and limitations under the
23  * License.
24  *
25  * The Original Code is "MantaRay" (TM).
26  *
27  * The Initial Developer of the Original Code is Coridan.
28  * Portions created by the Initial Developer are Copyright (C) 2006
29  * Coridan Inc. All Rights Reserved.
30  *
31  * Contributor(s): all the names of the contributors are added in the source
32  * code where applicable.
33  *
34  * Alternatively, the contents of this file may be used under the terms of the
35  * LGPL license (the "GNU LESSER GENERAL PUBLIC LICENSE"), in which case the
36  * provisions of LGPL are applicable instead of those above. If you wish to
37  * allow use of your version of this file only under the terms of the LGPL
38  * License and not to allow others to use your version of this file under
39  * the MPL, indicate your decision by deleting the provisions above and
40  * replace them with the notice and other provisions required by the LGPL.
41  * If you do not delete the provisions above, a recipient may use your version
42  * of this file under either the MPL or the GNU LESSER GENERAL PUBLIC LICENSE.
43  
44  *
45  * This library is free software; you can redistribute it and/or modify it
46  * under the terms of the MPL as stated above or under the terms of the GNU
47  * Lesser General Public License as published by the Free Software Foundation;
48  * either version 2.1 of the License, or any later version.
49  *
50  * This library is distributed in the hope that it will be useful, but WITHOUT
51  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
52  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
53  * License for more details.
54  */

55
56  /**
57  * User: Moti Tal
58  * Date: Apr 6, 2005
59  * Time: 9:28:06 AM
60  *
61  * This class is helper for writing XML attributes easly without having to insert simple parameters
62  */

63 public class NonNSAttributes {
64
65     private static final String JavaDoc TEXT_ATT_TYPE = "CDATA";
66     private static final String JavaDoc EMPTY_NS = NonNSContentHandler.EMPTY_NS;
67     private LinkedList JavaDoc m_atts;
68
69     /**
70      * Adds an attribute without a namespace.
71      *
72      * @param i_name The name of the attribute
73      * @param i_value The value of the attribute
74      */

75     public void addAttribute(String JavaDoc i_name, String JavaDoc i_value) {
76         if (m_atts == null) {
77             m_atts = new LinkedList JavaDoc();
78         }
79         m_atts.add(new NonNSAttribute(i_name, i_value));
80     }
81
82     /**
83      * Clears this attribute object for new usage.
84      */

85     public void clear() {
86         if (m_atts != null) {
87             m_atts.clear();
88         }
89     }
90
91     /**
92      * Tells if there are any attributes or not inside this object.
93      *
94      * @return True if no elements
95      */

96     public boolean isEmpty() {
97         return (m_atts == null || (m_atts.size() == 0));
98     }
99
100     /**
101      * Gets an iterator of NonNSAttribute objects.
102      *
103      * @return An attribute iterator
104      */

105     public Iterator JavaDoc attributes() {
106         if (m_atts == null) {
107             return Collections.EMPTY_LIST.iterator();
108         }
109         return m_atts.iterator();
110     }
111
112     public Attributes JavaDoc toAttributes() {
113         AttributesImpl JavaDoc ret = new AttributesImpl JavaDoc();
114         for (Iterator JavaDoc iterator = attributes(); iterator.hasNext();) {
115             NonNSAttribute nonNSAttribute = (NonNSAttribute) iterator.next();
116             String JavaDoc name = nonNSAttribute.getName();
117             String JavaDoc value = nonNSAttribute.getValue();
118             ret.addAttribute(EMPTY_NS, name, name, TEXT_ATT_TYPE, value);
119         }
120         return ret;
121     }
122
123     private static class NonNSAttribute {
124         private String JavaDoc m_name;
125         private String JavaDoc m_value;
126
127         public NonNSAttribute(String JavaDoc i_name, String JavaDoc i_value) {
128             m_name = i_name;
129             m_value = i_value;
130         }
131
132         public String JavaDoc getName() {
133             return m_name;
134         }
135
136         public String JavaDoc getValue() {
137             return m_value;
138         }
139     }
140 }
141
Popular Tags