SamEldin.com\XML_ParserProject\src\java\xml_dom_generator\MyFirstBeanDOM_XML_Converter.java
  1 /*
  2  *      Web Services New Tool®
  3  *      Project Tool - Demo
  4  *      http://sameldin.com/WebServices/index.html
  5  */
  6 
  7 package xml_dom_generator;
  8 
  9 import java.io.*;
 10 import javax.xml.parsers.*;
 11 import javax.xml.transform.*;
 12 import javax.xml.transform.dom.*;
 13 import javax.xml.transform.stream.*;
 14 import org.w3c.dom.*;
 15 import java.io.IOException;
 16 import javax.xml.parsers.DocumentBuilder;
 17 import javax.xml.parsers.DocumentBuilderFactory;
 18 import javax.xml.parsers.FactoryConfigurationError;
 19 import javax.xml.parsers.ParserConfigurationException;
 20 import org.w3c.dom.Document;
 21 import org.xml.sax.SAXException;
 22 
 23 import java.lang.reflect.*;
 24 import java.lang.Class;
 25 import java.math.BigDecimal;
 26 import java.math.BigInteger;
 27 import java.util.Calendar;
 28 
 29 import constants.Constants;
 30 
 31 import sample_data_objects.*;
 32 
 33 /**
 34  *
 35  * @author sameldin
 36  */
 37 public class MyFirstBeanDOM_XML_Converter
 38 {
 39 
 40     public MyFirstBeanDOM_XML_Converter()
 41     {
 42         MyFirstBean testMyFirstBean = new MyFirstBean();
 43 
 44         try
 45         {
 46             String root = "Root";
 47             DocumentBuilderFactory  documentBuilderFactory  = DocumentBuilderFactory.newInstance();
 48             DocumentBuilder         documentBuilder         = documentBuilderFactory.newDocumentBuilder();
 49             Document                document                = documentBuilder.newDocument();
 50             Element                 rootElement             = document.createElement(root);
 51             document.appendChild(rootElement);
 52             //----------------------------------------------
 53             Class testMyFirstBeanClass = testMyFirstBean.getClass();
 54             if(null == testMyFirstBeanClass)
 55             {
 56                 System.out.println("Error:  Class testMyFirstBeanClass = testMyFirstBean.getClass(); is null");
 57                 return;
 58             }
 59             if(null == document)
 60             {
 61                 System.out.println("Error:  document is null");
 62                 return;
 63             }
 64             if(null == rootElement)
 65             {
 66                 System.out.println("Error:  rootElement is null");
 67                 return;
 68             }
 69             //-----------------------------------------
 70             String  dataObjectTagName   = Constants.DATA_OBJECT_TAG_NAME;
 71             String  dataObjectTagLabel  = Constants.CLASS_NAME_LABEL;
 72             String  dataObjectName      = testMyFirstBeanClass.getName();
 73             Element classElement        = document.createElement(dataObjectTagName);
 74             classElement.setAttribute(dataObjectTagLabel, dataObjectName);
 75             rootElement.appendChild(classElement);
 76             //-----------------------------------------
 77             Class   returnTypeClass     = null;
 78             String  classToString       = "";
 79             //-----------------------------------------
 80             Method [] localClassMethods = null;
 81             localClassMethods           = testMyFirstBeanClass.getDeclaredMethods();
 82             for(int count = 0; count < localClassMethods.length; count++)
 83             {
 84                 try
 85                 {
 86                     String methodName = localClassMethods[count].getName();
 87                     if(methodName.indexOf("get", 0) < 0)
 88                           continue;
 89                     returnTypeClass = localClassMethods[count].getReturnType();
 90                     //-----------------------------------------
 91                     boolean     fieldFoundFlag      = false;
 92                     String      fieldTagName        = Constants.FIELD_TAG_NAME;
 93                     String      fieldLabel          = Constants.FIELD_LABEL;
 94                     String      fieldName           = methodName.substring(3);
 95                     int         returnIndex         = 0;
 96                     String      returnTypeTagName   = Constants.RETURN_TYPE_TAG_NAME;
 97                     String      returnLabel         = "";
 98                     String      returnValue         = "";
 99                     //-----------------------------------------
100                     System.out.println("getReturnType() .getName().toString();  = ");
101                     // the following is a debug point to check the return type
102                     String forDebugPoint = localClassMethods[count].getReturnType().getName().toString();
103                     //-----------------------------------------
104                     if (localClassMethods[count].getReturnType() == int.class)
105                     {
106                         fieldFoundFlag   = true;
107                         returnIndex      = Constants.INT_TYPE_INDEX;
108                         returnLabel      = Constants.INT_TYPE_NAME;
109                         returnValue      = "" + localClassMethods[count].invoke(testMyFirstBean);
110                     }
111                     else if(localClassMethods[count].getReturnType() == double[].class)
112                     {
113                         fieldFoundFlag   = true;
114                         returnIndex      = Constants.ARRAY_DOUBLE_TYPE_INDEX;
115                         returnLabel      = Constants.ARRAY_DOUBLE_TYPE_NAME;
116                         Object tempPointer      = localClassMethods[count].invoke(testMyFirstBean);
117                         if(tempPointer != null)
118                         {
119                             double [] doubleArrys   = (double [])tempPointer;
120                             returnValue      = "" + doubleArrys[0];
121                             for(int index = 0; index < doubleArrys.length; index++)
122                                 returnValue  += "," + doubleArrys[0];
123                         }
124                     }
125                     else if (localClassMethods[count].getReturnType() == String.class)
126                     {
127                         fieldFoundFlag   = true;
128                         returnIndex      = Constants.STRING_TYPE_INDEX;
129                         returnLabel      = Constants.STRING_TYPE_NAME;
130                         returnValue      = "" + localClassMethods[count].invoke(testMyFirstBean);
131                     }
132                     else
133                     {
134                         System.out.println("Else for No getReturnType()");
135                     }
136                     //-----------------------------------------
137                     if (fieldFoundFlag)
138                     {
139                         Element fieldElement        = document.createElement(fieldTagName);
140                         fieldElement.setAttribute(fieldLabel, fieldName);
141                         classElement.appendChild(fieldElement);
142                         //=======================================
143                             Element returnValueElement        = document.createElement(returnTypeTagName);
144                             returnValueElement.setAttribute(returnLabel, "" + returnIndex);
145                             returnValueElement.appendChild(document.createTextNode(returnValue));
146                             fieldElement.appendChild(returnValueElement);
147                         //=======================================
148                     }
149                     else
150                     {
151                         System.out.println("Else for fieldFoundFlag");
152                     }
153 
154 
155                 }
156                 catch(SecurityException mySecurityException)
157                 {
158                    mySecurityException.printStackTrace();
159                 }
160                 catch(IllegalArgumentException eIllegalArgumentException)
161                 {
162                    eIllegalArgumentException.printStackTrace();
163                 }
164                 catch(IllegalAccessException eIllegalAccessException)
165                 {
166                    eIllegalAccessException.printStackTrace();
167                 }
168                 catch(InvocationTargetException eInvocationTargetException)
169                 {
170                    eInvocationTargetException.printStackTrace();
171                 }
172             } // for loop
173             TransformerFactory transformerFactoryHandler = TransformerFactory.newInstance();
174             Transformer transformerHandler = transformerFactoryHandler.newTransformer();
175             DOMSource dOMSourceHandler = new DOMSource(document);
176             File myXMLfile = new File(Constants.TEST_FOLDER_PATH + Constants.MY_FIRST_BEAN_XML_FILE_NAME);
177             StreamResult streamResultHandler =  new StreamResult(myXMLfile);
178             transformerHandler.transform(dOMSourceHandler, streamResultHandler);
179 
180         }
181         catch (FactoryConfigurationError eFactoryConfigurationError)
182         {
183             eFactoryConfigurationError.printStackTrace();
184         }
185         catch (ParserConfigurationException eParserConfigurationException)
186         {
187             eParserConfigurationException.printStackTrace();
188         }
189         catch(Exception e)
190         {
191             e.printStackTrace();
192         }
193 
194     }
195     /**
196      *
197     */
198     public static void main (String[] args)
199     {
200         MyFirstBeanDOM_XML_Converter myFirstBeanDOM_XML_Converter = new MyFirstBeanDOM_XML_Converter();
201     }
202 }
203 
204