KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.log4j.Logger ;
15
16 /** class for represent the structure of the value of an attribute.
17  * It is used because part of the value of an attribute can be modified
18  * with the value of an XmlComponent.
19  * The value is represented by a three with nodes of three branchs
20  * the fist and the third branchs are another XmlThreeTemplate, a String or null
21  * the second is an XmlComponent
22 */

23 class XmlThreeTemplate implements XmlComponentTemplate {
24   protected XmlThreeTemplate xmlBegin = null;
25   protected boolean isBeginXmlThreeTemplate = false;
26   protected String JavaDoc stringBegin;
27   protected XmlComponentTemplate xmlMiddle = null;
28   protected XmlThreeTemplate xmlEnd = null;
29   protected boolean isEndXmlThreeTemplate = false;
30   protected String JavaDoc stringEnd;
31   static Logger log4jXmlThreeTemplate = Logger.getLogger(XmlThreeTemplate.class);
32
33   public XmlThreeTemplate() {}
34
35   public XmlThreeTemplate(String JavaDoc character) {
36     log4jXmlThreeTemplate.debug("constructor with parameter:" + character);
37     stringBegin = character;
38   }
39
40   public XmlThreeTemplate(XmlComponentTemplate xmlComponentTemplate) {
41     xmlMiddle = xmlComponentTemplate;
42   }
43
44   public XmlThreeTemplate(String JavaDoc characterBegin, XmlComponentTemplate xmlComponentTemplate, String JavaDoc characterEnd) {
45     stringBegin = characterBegin;
46     stringEnd = characterEnd;
47     xmlMiddle = xmlComponentTemplate;
48     log4jXmlThreeTemplate.debug("constructor with stringBegin: " + stringBegin + " stringEnd: " + stringEnd);
49   }
50
51   public String JavaDoc printStringBegin () {
52     return stringBegin;
53   }
54
55   public String JavaDoc printStringEnd () {
56     return stringEnd;
57   }
58
59   public void replace(AttributeComponentTemplate attributeComponentTemplate) {
60     if (isBeginXmlThreeTemplate) {
61       xmlBegin.replace(attributeComponentTemplate);
62     } else {
63       if (stringBegin != null) {
64         int index = stringBegin.indexOf(attributeComponentTemplate.replace());
65         if (index != -1) {
66           xmlBegin = new XmlThreeTemplate(stringBegin.substring(0,index),
67               attributeComponentTemplate.xmlComponentTemplate(),
68               stringBegin.substring(index + attributeComponentTemplate.replace().length()));
69           isBeginXmlThreeTemplate = true;
70         }
71       }
72     }
73     if (isEndXmlThreeTemplate) {
74       xmlEnd.replace(attributeComponentTemplate);
75     } else {
76       if (stringEnd != null) {
77         int index = stringEnd.indexOf(attributeComponentTemplate.replace());
78         if (index != -1) {
79           xmlEnd = new XmlThreeTemplate(stringEnd.substring(0,index),
80               attributeComponentTemplate.xmlComponentTemplate(),
81               stringEnd.substring(index + attributeComponentTemplate.replace().length()));
82           isEndXmlThreeTemplate = true;
83         }
84       }
85     }
86   }
87
88   public XmlThreeValue createXmlThreeValue(XmlDocument xmlDocument) {
89     return new XmlThreeValue(this, xmlDocument);
90   }
91   public XmlComponentValue createXmlComponentValue(XmlDocument xmlDocument) {
92     return new XmlThreeValue(this, xmlDocument);
93   }
94
95 }
96
Popular Tags