KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openbravo > xmlEngine > TagTemplate


1 /*
2  ************************************************************************************
3  * Copyright (C) 2001-2006 Openbravo S.L.
4  * Licensed under the Apache Software License version 2.0
5  * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6  * Unless required by applicable law or agreed to in writing, software distributed
7  * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
8  * CONDITIONS OF ANY KIND, either express or implied. See the License for the
9  * specific language governing permissions and limitations under the License.
10  ************************************************************************************
11 */

12 package org.openbravo.xmlEngine;
13
14 import java.util.Vector JavaDoc;
15 import java.util.Enumeration JavaDoc;
16
17 import org.xml.sax.Attributes JavaDoc;
18
19 class TagTemplate implements XmlComponentTemplate {
20   protected String JavaDoc tag;
21   protected Vector JavaDoc<AttributeItemTemplate> attributeVectorTemplate; // vector of AttributeItemTemplate
22

23   public TagTemplate() {}
24
25   public TagTemplate(String JavaDoc tag) {
26     this.tag = tag;
27   }
28
29   public String JavaDoc tag() {
30     return tag;
31   }
32
33   public TagTemplate(String JavaDoc name, Attributes JavaDoc amap, String JavaDoc replaceWhat, String JavaDoc replaceWith,
34       String JavaDoc prefix, String JavaDoc uri) {
35     this.tag = name;
36     attributeVectorTemplate = new Vector JavaDoc<AttributeItemTemplate>();
37     for (int i = 0; i < amap.getLength(); i++) {
38       AttributeItemTemplate attribute = new AttributeItemTemplate();
39       attribute.name = amap.getQName(i);
40       String JavaDoc value = replace(amap.getValue(i), replaceWhat, replaceWith);
41       attribute.valueTemplate = new XmlThreeTemplate(value);
42       attributeVectorTemplate.addElement(attribute);
43     }
44     if (prefix != null) {
45       AttributeItemTemplate attribute = new AttributeItemTemplate();
46       attribute.name = "xmlns:" + prefix;
47       attribute.valueTemplate = new XmlThreeTemplate(uri);
48       attributeVectorTemplate.addElement(attribute);
49     }
50   }
51
52   public String JavaDoc replace (String JavaDoc initValue, String JavaDoc replaceWhat, String JavaDoc replaceWith) {
53     if (initValue != null && replaceWhat != null) {
54       int index = initValue.indexOf(replaceWhat);
55       if (index != -1) {
56         String JavaDoc finalValue = new String JavaDoc(initValue.substring(0,index) +
57             replaceWith +
58             initValue.substring(index + replaceWhat.length()));
59         return finalValue;
60       }
61     }
62     return initValue;
63   }
64
65   public void setAttribute(AttributeComponentTemplate attributeComponentTemplate) {
66     // attribute for value or replace
67
if (attributeComponentTemplate.attributeName() != null) {
68       for (Enumeration JavaDoc<AttributeItemTemplate> e = attributeVectorTemplate.elements() ; e.hasMoreElements() ;) {
69         AttributeItemTemplate attribute = e.nextElement();
70         if (attribute.name.equals(attributeComponentTemplate.attributeName())) {
71           if (attributeComponentTemplate.replace() != null) {
72             attribute.valueTemplate.replace(attributeComponentTemplate);
73           } else {
74             attribute.valueTemplate = new XmlThreeTemplate(attributeComponentTemplate.xmlComponentTemplate());
75           }
76         }
77       }
78     }
79     // attribute boolean
80
if (attributeComponentTemplate.attributeBooleanName() != null) {
81       for (Enumeration JavaDoc<AttributeItemTemplate> e = attributeVectorTemplate.elements() ; e.hasMoreElements() ;) {
82         AttributeItemTemplate attribute = e.nextElement();
83         if (attribute.name.equals(attributeComponentTemplate.attributeBooleanName())) {
84           attribute.valueTemplate = new XmlThreeTemplate(attributeComponentTemplate.xmlComponentTemplate());
85           attribute.attributeBoolean = true;
86           attribute.valueToCompareTemplate = attributeComponentTemplate.booleanWithId();
87           return;
88         }
89       }
90       //if it arrive here, then the attribute is not present, we create it
91
AttributeItemTemplate attribute = new AttributeItemTemplate();
92       attribute.name = attributeComponentTemplate.attributeBooleanName();
93       attribute.valueTemplate = new XmlThreeTemplate(attributeComponentTemplate.xmlComponentTemplate());
94       attribute.attributeBoolean = true;
95       attribute.valueToCompareTemplate = attributeComponentTemplate.booleanWithId();
96       attributeVectorTemplate.addElement(attribute);
97     }
98   }
99
100   public XmlComponentValue createXmlComponentValue(XmlDocument xmlDocument) {
101     return new TagValue(this, xmlDocument);
102   }
103
104 }
105
Popular Tags