Information

0
Story Points

Technologies

Decompiled Java File
package org.exolab.castor.tools;

import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Properties;
import org.castor.xml.InternalContext;
import org.exolab.castor.mapping.FieldDescriptor;
import org.exolab.castor.mapping.MappingException;
import org.exolab.castor.mapping.loader.CollectionHandlers;
import org.exolab.castor.mapping.loader.Types;
import org.exolab.castor.mapping.xml.BindXml;
import org.exolab.castor.mapping.xml.ClassChoice;
import org.exolab.castor.mapping.xml.ClassMapping;
import org.exolab.castor.mapping.xml.FieldMapping;
import org.exolab.castor.mapping.xml.MapTo;
import org.exolab.castor.mapping.xml.MappingRoot;
import org.exolab.castor.mapping.xml.types.BindXmlNodeType;
import org.exolab.castor.mapping.xml.types.ClassMappingAccessType;
import org.exolab.castor.mapping.xml.types.FieldMappingCollectionType;
import org.exolab.castor.tools.MappingToolMappingLoader;
import org.exolab.castor.util.CommandLineOptions;
import org.exolab.castor.util.dialog.ConsoleDialog;
import org.exolab.castor.xml.Introspector;
import org.exolab.castor.xml.Marshaller;
import org.exolab.castor.xml.XMLClassDescriptor;
import org.exolab.castor.xml.XMLContext;
import org.exolab.castor.xml.XMLFieldDescriptor;

public class MappingTool {
   private static final String UNDERSCORE = "_";
   private Hashtable _mappings;
   private MappingToolMappingLoader _mappingLoader;
   private boolean _forceIntrospection = false;
   private InternalContext _internalContext;

   public static void main(String[] args) {
      CommandLineOptions allOptions = new CommandLineOptions();
      allOptions.addFlag("i", "classname", "Sets the input class");
      String desc = "Sets the output mapping filename";
      allOptions.addFlag("o", "filename", desc, true);
      desc = "Force overwriting of files.";
      allOptions.addFlag("f", "", desc, true);
      desc = "Displays this help screen.";
      allOptions.addFlag("h", "", desc, true);
      Properties options = allOptions.getOptions(args);
      if(options.getProperty("h") != null) {
         PrintWriter classname1 = new PrintWriter(System.out, true);
         allOptions.printHelp(classname1);
         classname1.flush();
      } else {
         String classname = options.getProperty("i");
         String mappingName = options.getProperty("o");
         boolean force = options.getProperty("f") != null;
         if(classname == null) {
            PrintWriter tool1 = new PrintWriter(System.out, true);
            allOptions.printUsage(tool1);
            tool1.flush();
         } else {
            try {
               XMLContext except = new XMLContext();
               MappingTool tool = except.createMappingTool();
               tool.addClass(classname);
               Object writer = null;
               if(mappingName != null && mappingName.length() != 0) {
                  File file = new File(mappingName);
                  if(file.exists() && !force) {
                     ConsoleDialog dialog = new ConsoleDialog();
                     String message = "The file already exists. Do you wish to overwrite \'" + mappingName + "\'?";
                     if(!dialog.confirm(message)) {
                        return;
                     }
                  }

                  writer = new FileWriter(file);
               } else {
                  writer = new PrintWriter(System.out, true);
               }

               tool.write((Writer)writer);
            } catch (Exception var13) {
               System.out.println(var13);
               var13.printStackTrace();
            }

         }
      }
   }

   public void addClass(String name) throws MappingException {
      this.addClass(name, true);
   }

   public void addClass(String name, boolean deep) throws MappingException {
      if(name == null) {
         throw new MappingException("Cannot introspect a null class.");
      } else {
         try {
            this.addClass(Class.forName(name), deep);
         } catch (ClassNotFoundException var4) {
            throw new MappingException(var4);
         }
      }
   }

   public void addClass(Class cls) throws MappingException {
      this.addClass(cls, true);
   }

   public void addClass(Class cls, boolean deep) throws MappingException {
      if(cls == null) {
         throw new MappingException("Cannot introspect a null class.");
      } else if(this._mappings.get(cls) == null) {
         if(cls.isArray()) {
            Class xmlClass = cls.getComponentType();
            if(this._mappings.get(xmlClass) != null) {
               return;
            }

            if(Types.isSimpleType(xmlClass)) {
               return;
            }

            this.addClass(xmlClass);
         }

         if(this._forceIntrospection && !Types.isConstructable(cls)) {
            throw new MappingException("mapping.classNotConstructable", cls.getName());
         } else {
            boolean introspected = false;

            XMLClassDescriptor var17;
            try {
               if(this._forceIntrospection) {
                  var17 = this._internalContext.getIntrospector().generateClassDescriptor(cls);
                  introspected = true;
               } else {
                  var17 = (XMLClassDescriptor)this._internalContext.getXMLClassDescriptorResolver().resolve(cls);
                  this._internalContext.getIntrospector();
                  introspected = Introspector.introspected(var17);
               }
            } catch (Exception var16) {
               throw new MappingException(var16);
            }

            ClassMapping classMap = new ClassMapping();
            classMap.setName(cls.getName());
            classMap.setDescription("Default mapping for class " + cls.getName());
            classMap.setAccess((ClassMappingAccessType)null);
            MapTo mapTo = new MapTo();
            mapTo.setXml(var17.getXMLName());
            mapTo.setNsUri(var17.getNameSpaceURI());
            mapTo.setNsPrefix(var17.getNameSpacePrefix());
            classMap.setMapTo(mapTo);
            this._mappings.put(cls, classMap);
            FieldDescriptor[] fields = var17.getFields();

            for(int i = 0; i < fields.length; ++i) {
               FieldDescriptor fdesc = fields[i];
               String fieldName = fdesc.getFieldName();
               boolean isContainer = false;
               if(introspected && fieldName.startsWith("##container")) {
                  fdesc = fdesc.getClassDescriptor().getFields()[0];
                  fieldName = fdesc.getFieldName();
                  isContainer = true;
               }

               Class fieldType = fdesc.getFieldType();
               String colName;
               if(!introspected && fieldName.startsWith("_")) {
                  if(!this._mappingLoader.canFindAccessors(cls, fieldName, fieldType)) {
                     fieldName = fieldName.substring(1);
                  }

                  if(!this._mappingLoader.canFindAccessors(cls, fieldName, fieldType) && fieldName.endsWith("List")) {
                     int isArray = fieldName.length() - 4;
                     colName = fieldName.substring(0, isArray);
                     if(this._mappingLoader.canFindAccessors(cls, colName, fieldType)) {
                        fieldName = colName;
                     }
                  }
               }

               FieldMapping fieldMap = new FieldMapping();
               fieldMap.setName(fieldName);

               boolean var18;
               for(var18 = fieldType.isArray(); fieldType.isArray(); fieldType = fieldType.getComponentType()) {
                  ;
               }

               if(fdesc.isRequired()) {
                  fieldMap.setRequired(true);
               }

               if(fdesc.isTransient()) {
                  fieldMap.setTransient(true);
               }

               if(fdesc.isMultivalued()) {
                  if(isContainer) {
                     fieldMap.setContainer(false);
                  }

                  if(var18) {
                     fieldMap.setCollection(FieldMappingCollectionType.ARRAY);
                  } else {
                     colName = CollectionHandlers.getCollectionName(fieldType);
                     if(colName != null) {
                        fieldMap.setCollection(FieldMappingCollectionType.valueOf(colName));
                        fieldType = Object.class;
                     } else if(this._mappingLoader.returnsArray(cls, fieldName, fieldType)) {
                        fieldMap.setCollection(FieldMappingCollectionType.ARRAY);
                     } else {
                        fieldMap.setCollection(FieldMappingCollectionType.ENUMERATE);
                     }
                  }
               }

               fieldMap.setType(fieldType.getName());
               fieldMap.setBindXml(new BindXml());
               fieldMap.getBindXml().setName(((XMLFieldDescriptor)fdesc).getXMLName());
               fieldMap.getBindXml().setNode(BindXmlNodeType.valueOf(((XMLFieldDescriptor)fields[i]).getNodeType().toString()));
               if(classMap.getClassChoice() == null) {
                  classMap.setClassChoice(new ClassChoice());
               }

               classMap.getClassChoice().addFieldMapping(fieldMap);
               if(deep && this._mappings.get(fieldType) == null && !Types.isSimpleType(fieldType)) {
                  this.addClass(fieldType);
               }
            }

         }
      }
   }

   public void setForceIntrospection(boolean force) {
      this._forceIntrospection = force;
   }

   public void write(Writer writer) throws MappingException {
      try {
         MappingRoot mapping = new MappingRoot();
         mapping.setDescription("Castor generated mapping file");
         Enumeration enumeration = this._mappings.elements();

         while(enumeration.hasMoreElements()) {
            mapping.addClassMapping((ClassMapping)enumeration.nextElement());
         }

         Marshaller marshal = new Marshaller(writer);
         marshal.setNamespaceMapping((String)null, "http://castor.exolab.org/");
         marshal.setNamespaceMapping("cst", "http://castor.exolab.org/");
         marshal.marshal(mapping);
      } catch (Exception var6) {
         throw new MappingException(var6);
      }
   }

   public void setInternalContext(InternalContext internalContext) {
      this._internalContext = internalContext;
      this._mappings = new Hashtable();
      this._mappingLoader = new MappingToolMappingLoader(this._internalContext.getJavaNaming());
   }
}
Page generated: Oct 19, 2017 2:34:47 PM