Android Open Source - Dumbledroid Json Reverse Reflector






From Project

Back to project page Dumbledroid.

License

The source code is released under:

Copyright (c) 2013, Leocadio Tin? All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: ...

If you think the Android project Dumbledroid listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package io.leocad.dumbledroidplugin.core;
//from  w ww  . j  a  v a 2  s  .  c  om
import io.leocad.dumbledroidplugin.exceptions.InvalidContentException;
import io.leocad.dumbledroidplugin.exceptions.InvalidUrlException;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Iterator;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.Path;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class JsonReverseReflector {

  public static void parseJsonToFiles(InputStream is, String url, String urlQueryString, boolean isPojo, long cacheDuration, IFile file) throws InvalidUrlException, InvalidContentException {

    String jsonString = getJsonString(is);

    JSONObject jsonObj = null;
    JSONArray jsonArray = null;

    try {
      jsonObj = new JSONObject(jsonString);

    } catch (JSONException e) {
      //Not a JsonObject. Try an array
      try {
        jsonArray = new JSONArray(jsonString);

      } catch (JSONException e2) {
        //Not a valid JSON.
        throw new InvalidContentException("JSON");
      }
    }

    if (jsonObj != null) {
      processJsonObjectFile(jsonObj, true, url, urlQueryString, isPojo, cacheDuration, file);
    } else {
      processJsonArrayFile(jsonArray, true, url, urlQueryString, isPojo, cacheDuration, file);
    }
  }

  private static String getJsonString(InputStream is) throws InvalidUrlException {

    StringBuilder total = new StringBuilder();

    try {
      BufferedReader r = new BufferedReader(new InputStreamReader(is));

      String line;
      while ((line = r.readLine()) != null) {
        total.append(line);
      }


    } catch (IOException e) {
      throw new InvalidUrlException();
    }

    try {
      is.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
    
    return total.toString();
  }

  private static void processJsonObjectFile(JSONObject jsonObj, boolean isAbstractModel, String url, String urlQueryString, boolean isPojo, long cacheDuration, IFile file) {

    StringBuffer fileBuffer = new StringBuffer();
    StringBuffer gettersBuffer = new StringBuffer();
    StringBuffer settersBuffer = new StringBuffer();

    ClassWriter.appendPackageDeclaration(fileBuffer, file);
    ClassWriter.appendImportStatements(fileBuffer, isAbstractModel);
    ClassWriter.appendClassDeclaration(fileBuffer, file, isAbstractModel);

    // Fields declaration
    @SuppressWarnings("unchecked")
    Iterator<String> keys = jsonObj.keys();
    while (keys.hasNext()) {

      String key = keys.next();
      Object object = jsonObj.get(key);
      String fieldTypeName = ClassMapper.getPrimitiveTypeName(object);

      if (fieldTypeName == null) {
        // Not a primitive. Recursion ahead.

        fieldTypeName = mapField(file, object, fileBuffer, key, isPojo, cacheDuration);
      }

      ClassWriter.appendFieldDeclaration(fileBuffer, key, fieldTypeName, isPojo, gettersBuffer, settersBuffer);
    }
    fileBuffer.append("\n");

    ClassWriter.appendConstructor(fileBuffer, file, url, cacheDuration, isAbstractModel);
    ClassWriter.appendOverridenLoad(fileBuffer, urlQueryString, isAbstractModel);
    ClassWriter.appendAccessorMethods(fileBuffer, gettersBuffer, settersBuffer);
    ClassWriter.appendInheritAbstractMethods(fileBuffer, true, isAbstractModel);
    ClassWriter.appendClassEnd(fileBuffer);

    FileUtils.write(file, fileBuffer.toString());
  }

  private static void processJsonArrayFile(JSONArray jsonArray, boolean isAbstractModel, String url, String urlQueryString, boolean isPojo, long cacheDuration, IFile file) {

    StringBuffer fileBuffer = new StringBuffer();
    StringBuffer gettersBuffer = new StringBuffer();
    StringBuffer settersBuffer = new StringBuffer();

    ClassWriter.appendPackageDeclaration(fileBuffer, file);
    ClassWriter.appendImportStatements(fileBuffer, isAbstractModel);
    ClassWriter.appendListImport(fileBuffer);
    ClassWriter.appendClassDeclaration(fileBuffer, file, isAbstractModel);

    // Field declaration
    // Since the root node is an array, this class will have only one field, named "list"
    String key = FileUtils.getFileNameWithoutExtension(file) + "List";
    Object object = jsonArray.get(0);
    
    String fieldTypeName;
    
    if (object == null) {
      // Empty array
      fieldTypeName = "Object";
      
    } else {
      fieldTypeName = ClassMapper.getPrimitiveTypeName(object);
    }

    if (fieldTypeName == null) {
      // Not a primitive. Recursion ahead.
      fieldTypeName = mapField(file, object, fileBuffer, key, isPojo, cacheDuration);
      
    } else {
      // Primitive type. Convert to wrapper class
      fieldTypeName = object.getClass().getSimpleName();
    }
    
    fieldTypeName = String.format("List<%s>", fieldTypeName);
    ClassWriter.appendFieldDeclaration(fileBuffer, "list", fieldTypeName, isPojo, gettersBuffer, settersBuffer);
    fileBuffer.append("\n");

    ClassWriter.appendConstructor(fileBuffer, file, url, cacheDuration, isAbstractModel);
    ClassWriter.appendOverridenLoad(fileBuffer, urlQueryString, isAbstractModel);
    ClassWriter.appendAccessorMethods(fileBuffer, gettersBuffer, settersBuffer);
    ClassWriter.appendInheritAbstractMethods(fileBuffer, true, isAbstractModel);
    ClassWriter.appendClassEnd(fileBuffer);

    FileUtils.write(file, fileBuffer.toString());
  }
  
  private static String mapField(IFile file, Object object, StringBuffer fileBuffer, String key, boolean isPojo, long cacheDuration) {

    String fieldTypeName;
    if (object instanceof JSONObject) {

      fieldTypeName = ClassWriter.uppercaseFirstChar(key);

      IFile newFile = file.getParent().getFile(new Path(fieldTypeName + ".java"));
      FileUtils.create(newFile);
      processJsonObjectFile((JSONObject) object, false, null, null, isPojo, cacheDuration, newFile);

    } else if (object instanceof JSONArray) {

      ClassWriter.appendListImport(fileBuffer);

      JSONArray array = (JSONArray) object;

      if (array.length() == 0) { // Empty array
        fieldTypeName = "List<Object>";

      } else { 
        Object child = array.get(0);
        
        if (child instanceof JSONObject) { // Non-primitive type
          final String childTypeName = ClassWriter.getArrayChildTypeName(key);
          fieldTypeName = String.format("List<%s>", childTypeName);
  
          //Create files for the children
          IFile newFile = file.getParent().getFile(new Path(childTypeName + ".java"));
          FileUtils.create(newFile);
          processJsonObjectFile((JSONObject) child, false, null, null, isPojo, cacheDuration, newFile);

        } else {
          fieldTypeName = String.format("List<%s>", child.getClass().getSimpleName());
        }
      }

    } else {
      //Unknown class
      fieldTypeName = object.getClass().getSimpleName();
    }
    return fieldTypeName;
  }
}




Java Source Code List

io.leocad.dumbledoreexample.activities.AboutActivity.java
io.leocad.dumbledoreexample.activities.BaseActivity.java
io.leocad.dumbledoreexample.activities.FlickrActivity.java
io.leocad.dumbledoreexample.activities.JediActivity.java
io.leocad.dumbledoreexample.activities.MainActivity.java
io.leocad.dumbledoreexample.activities.SithActivity.java
io.leocad.dumbledoreexample.adapters.FlickrAdapter.java
io.leocad.dumbledoreexample.models.FlickrPhotos.java
io.leocad.dumbledoreexample.models.Jedi.java
io.leocad.dumbledoreexample.models.Media.java
io.leocad.dumbledoreexample.models.PhotoItem.java
io.leocad.dumbledoreexample.models.Sith.java
io.leocad.dumbledoreexample.models.Suit.java
io.leocad.dumbledroid.data.AbstractModel.java
io.leocad.dumbledroid.data.DataController.java
io.leocad.dumbledroid.data.DataType.java
io.leocad.dumbledroid.data.JsonReflector.java
io.leocad.dumbledroid.data.ReflectionHelper.java
io.leocad.dumbledroid.data.XmlReflector.java
io.leocad.dumbledroid.data.cache.DiskCache.java
io.leocad.dumbledroid.data.cache.FileController.java
io.leocad.dumbledroid.data.cache.MemoryCache.java
io.leocad.dumbledroid.data.cache.ModelHolder.java
io.leocad.dumbledroid.data.cache.ObjectCopier.java
io.leocad.dumbledroid.data.xml.Node.java
io.leocad.dumbledroid.data.xml.SaxHandler.java
io.leocad.dumbledroid.data.xml.SaxParser.java
io.leocad.dumbledroid.net.HttpLoader.java
io.leocad.dumbledroid.net.HttpMethod.java
io.leocad.dumbledroid.net.NoConnectionException.java
io.leocad.dumbledroid.net.TimeoutException.java
io.leocad.dumbledroidplugin.core.ClassMapper.java
io.leocad.dumbledroidplugin.core.ClassWriter.java
io.leocad.dumbledroidplugin.core.DumbledroidClassCreator.java
io.leocad.dumbledroidplugin.core.FileUtils.java
io.leocad.dumbledroidplugin.core.JsonReverseReflector.java
io.leocad.dumbledroidplugin.core.XmlReverseReflector.java
io.leocad.dumbledroidplugin.exceptions.InvalidContentException.java
io.leocad.dumbledroidplugin.exceptions.InvalidUrlException.java
io.leocad.dumbledroidplugin.exceptions.UnsupportedContentTypeException.java
io.leocad.dumbledroidplugin.wizards.DataInputPage.java
io.leocad.dumbledroidplugin.wizards.FileCreationPage.java
io.leocad.dumbledroidplugin.wizards.NewModelWizard.java
org.apache.commons.validator.routines.DomainValidator.java
org.apache.commons.validator.routines.InetAddressValidator.java
org.apache.commons.validator.routines.RegexValidator.java
org.apache.commons.validator.routines.UrlValidator.java