KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > deliver > controllers > kernel > impl > simple > FormDeliveryController


1 /* ===============================================================================
2  *
3  * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4  *
5  * ===============================================================================
6  *
7  * Copyright (C)
8  *
9  * This program is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License version 2, as published by the
11  * Free Software Foundation. See the file LICENSE.html for more information.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20  *
21  * ===============================================================================
22  */

23
24 package org.infoglue.deliver.controllers.kernel.impl.simple;
25
26 import java.io.StringReader JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.List JavaDoc;
29
30 import org.apache.log4j.Logger;
31 import org.apache.xerces.parsers.DOMParser;
32 import org.infoglue.cms.entities.management.ContentTypeAttribute;
33 import org.infoglue.cms.entities.management.ContentTypeAttributeParameter;
34 import org.infoglue.cms.entities.management.ContentTypeAttributeParameterValue;
35 import org.w3c.dom.Document JavaDoc;
36 import org.w3c.dom.Element JavaDoc;
37 import org.w3c.dom.NamedNodeMap JavaDoc;
38 import org.w3c.dom.Node JavaDoc;
39 import org.w3c.dom.NodeList JavaDoc;
40 import org.xml.sax.InputSource JavaDoc;
41
42 public class FormDeliveryController
43 {
44     private final static Logger logger = Logger.getLogger(FormDeliveryController.class.getName());
45
46     /**
47      * Private constructor to enforce factory-use
48      */

49     
50     private FormDeliveryController()
51     {
52     }
53     
54     /**
55      * Factory method
56      */

57     
58     public static FormDeliveryController getFormDeliveryController()
59     {
60         return new FormDeliveryController();
61     }
62     
63     /**
64      * This method returns the attributes in the content type definition for generation.
65      */

66         
67     public List JavaDoc getContentTypeAttributes(String JavaDoc schemaValue)
68     {
69         logger.info("schemaValue:" + schemaValue);
70         List JavaDoc attributes = new ArrayList JavaDoc();
71             
72         try
73         {
74             InputSource JavaDoc xmlSource = new InputSource JavaDoc(new StringReader JavaDoc(schemaValue));
75                 
76             DOMParser parser = new DOMParser();
77             parser.parse(xmlSource);
78             Document JavaDoc document = parser.getDocument();
79                 
80             String JavaDoc attributesXPath = "/xs:schema/xs:complexType/xs:all/xs:element/xs:complexType/xs:all/xs:element";
81             NodeList JavaDoc anl = org.apache.xpath.XPathAPI.selectNodeList(document.getDocumentElement(), attributesXPath);
82             for(int i=0; i < anl.getLength(); i++)
83             {
84                 Element JavaDoc child = (Element JavaDoc)anl.item(i);
85                 String JavaDoc attributeName = child.getAttribute("name");
86                 String JavaDoc attributeType = child.getAttribute("type");
87                                     
88                 ContentTypeAttribute contentTypeAttribute = new ContentTypeAttribute();
89                 contentTypeAttribute.setPosition(i);
90                 contentTypeAttribute.setName(attributeName);
91                 contentTypeAttribute.setInputType(attributeType);
92     
93                 // Get extra parameters
94
Node JavaDoc paramsNode = org.apache.xpath.XPathAPI.selectSingleNode(child, "xs:annotation/xs:appinfo/params");
95                 if (paramsNode != null)
96                 {
97                     NodeList JavaDoc childnl = ((Element JavaDoc)paramsNode).getElementsByTagName("param");
98                     for(int ci=0; ci < childnl.getLength(); ci++)
99                     {
100                         Element JavaDoc param = (Element JavaDoc)childnl.item(ci);
101                         String JavaDoc paramId = param.getAttribute("id");
102                         String JavaDoc paramInputTypeId = param.getAttribute("inputTypeId");
103                             
104                         ContentTypeAttributeParameter contentTypeAttributeParameter = new ContentTypeAttributeParameter();
105                         contentTypeAttributeParameter.setId(paramId);
106                         if(paramInputTypeId != null && paramInputTypeId.length() > 0)
107                             contentTypeAttributeParameter.setType(Integer.parseInt(paramInputTypeId));
108                     
109                         contentTypeAttribute.putContentTypeAttributeParameter(paramId, contentTypeAttributeParameter);
110                             
111                         NodeList JavaDoc valuesNodeList = param.getElementsByTagName("values");
112                         for(int vsnli=0; vsnli < valuesNodeList.getLength(); vsnli++)
113                         {
114                             Element JavaDoc values = (Element JavaDoc)valuesNodeList.item(vsnli);
115     
116                             NodeList JavaDoc valueNodeList = param.getElementsByTagName("value");
117                             for(int vnli=0; vnli < valueNodeList.getLength(); vnli++)
118                             {
119                                 Element JavaDoc value = (Element JavaDoc)valueNodeList.item(vnli);
120                                 String JavaDoc valueId = value.getAttribute("id");
121                                     
122                                 ContentTypeAttributeParameterValue contentTypeAttributeParameterValue = new ContentTypeAttributeParameterValue();
123                                 contentTypeAttributeParameterValue.setId(valueId);
124                                     
125                                 NamedNodeMap JavaDoc nodeMap = value.getAttributes();
126                                 for(int nmi =0; nmi < nodeMap.getLength(); nmi++)
127                                 {
128                                     Node JavaDoc attribute = (Node JavaDoc)nodeMap.item(nmi);
129                                     String JavaDoc valueAttributeName = attribute.getNodeName();
130                                     String JavaDoc valueAttributeValue = attribute.getNodeValue();
131                                     contentTypeAttributeParameterValue.addAttribute(valueAttributeName, valueAttributeValue);
132                                 }
133                                     
134                                 contentTypeAttributeParameter.addContentTypeAttributeParameterValue(valueId, contentTypeAttributeParameterValue);
135                             }
136                         }
137                     }
138                 }
139                 // End extra parameters
140

141                 attributes.add(contentTypeAttribute);
142             }
143                 
144         }
145         catch(Exception JavaDoc e)
146         {
147             logger.error("An error occurred when we tried to get the attributes of the content type: " + e.getMessage(), e);
148         }
149                     
150         return attributes;
151     }
152
153 }
Popular Tags