Last active
December 10, 2015 10:21
-
-
Save ygweric/f1a274ca620a049c9bbd to your computer and use it in GitHub Desktop.
convert model of MTLModel(ObjC) to ObjectMapper(Swift)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.dogkk; | |
import java.io.File; | |
import java.io.FileNotFoundException; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.io.PrintWriter; | |
import java.nio.file.Files; | |
import java.nio.file.Path; | |
import java.nio.file.Paths; | |
import java.nio.file.StandardOpenOption; | |
import java.util.ArrayList; | |
import java.util.HashSet; | |
import java.util.Scanner; | |
import java.util.Set; | |
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
import org.apache.commons.io.FilenameUtils; | |
import org.omg.CosNaming.NamingContextExtPackage.StringNameHelper; | |
public class Main { | |
// static HashSet<String> typeNames=new HashSet<String>(); | |
static String typeNames=""; | |
public static void main(String[] args) { | |
System.out.println("hello word"); | |
try { | |
Path currentRelativePath = Paths.get(""); | |
System.out.println("Current relative path is: " + currentRelativePath.toString()); | |
String s = currentRelativePath.toAbsolutePath().toString(); | |
System.out.println("Current relative path is: " + s); | |
File[] listOfFiles; | |
listOfFiles = getAllFiles("objc"); | |
for (int i = 0; i < listOfFiles.length; i++) { | |
File file = listOfFiles[i]; | |
if (file.isFile()) { | |
System.out.println("File " + file.getName()); | |
String fileName=file.getName(); | |
if (fileName.endsWith("ModelS.swift") | |
|| fileName.endsWith("StoreS.swift") | |
) { | |
file.delete(); | |
} | |
} | |
} | |
listOfFiles = getAllFiles("objc"); | |
for (int i = 0; i < listOfFiles.length; i++) { | |
File file=listOfFiles[i]; | |
if (file.isFile()) { | |
System.out.println("File " +file.getName()); | |
String fileName=file.getName(); | |
if (fileName.endsWith("Model.h") | |
|| fileName.endsWith("Store.h") | |
) { | |
//if swift exist | |
String baseFilePath=file.getParent(); | |
String baseFileName= FilenameUtils.getBaseName(file.getName()); | |
String swiftFileName= baseFileName+"S.swift"; | |
if (fileName.endsWith(".h")){ | |
File swiftFile = new File(baseFilePath, swiftFileName); | |
assert !swiftFile.exists(); | |
createSwiftFile(baseFileName); | |
System.out.println(swiftFile.getAbsolutePath()); | |
String swiftFileContent = parseFile_h(file); | |
Files.write(Paths.get(swiftFile.getAbsolutePath()), swiftFileContent.getBytes(), StandardOpenOption.APPEND); | |
} | |
} | |
} else if (listOfFiles[i].isDirectory()) { | |
System.out.println("Directory " + listOfFiles[i].getName()); | |
} | |
} | |
System.out.println(typeNames); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
public static File[] getAllFiles(String dir){ | |
File folder = new File(dir); | |
File[] listOfFiles = folder.listFiles(); | |
return listOfFiles; | |
} | |
public static void readByLine(String fileName) throws FileNotFoundException{ | |
File file=new File(fileName); | |
Scanner scan = new Scanner(file); | |
while(scan.hasNextLine()){ | |
String line = scan.nextLine(); | |
System.out.println(line); | |
} | |
} | |
public static void createSwiftFile(String baseFileName) throws IOException{ | |
File file=new File("objc", baseFileName+"S.swift"); | |
FileOutputStream out = new FileOutputStream(file); | |
out.write(("//\n" | |
+ "// LoginModelS.swift\n" | |
+ "// ZBCool\n//\n" | |
+ "// Created by ericyang on 11/25/15.\n" | |
+ "// Copyright © 2015 i-chou. All rights reserved.\n//\n\n\n" | |
+ "import ObjectMapper\n\n" | |
+ "class "+baseFileName+"S: Mappable{\n\n" | |
).getBytes()); | |
out.close(); | |
} | |
public static String parseFile_h(File file) throws IOException{ | |
Scanner scan = new Scanner(file); | |
String swiftFileContent=""; | |
ArrayList<String> varNames=new ArrayList<String>(); | |
while (scan.hasNextLine()) { | |
String line = scan.nextLine(); | |
System.out.println(line); | |
if (line.startsWith("@property")) { | |
String typeName=""; | |
String varName=""; | |
{ | |
Matcher m = Pattern.compile("(?<=\\)) *[^ ]+").matcher(line); | |
while (m.find()) { | |
typeName = m.group(0).trim(); | |
System.out.println(typeName); | |
if (typeName.equals("BOOL")) { | |
typeName="Bool"; | |
}else if(typeName.endsWith("Model") | |
|| typeName.endsWith("Store") ){ | |
typeName+="S"; | |
} | |
break; | |
} | |
} | |
{ | |
Matcher m = Pattern.compile("[^ *]+(?=;)").matcher(line); | |
while (m.find()) { | |
varName = m.group(0).trim(); | |
varNames.add(varName); | |
System.out.println(varName); | |
break; | |
} | |
} | |
swiftFileContent+=" var "+varName+": "+typeName+"?\n"; | |
typeNames+=file.getName()+" "+typeName+" "+varName+"\n"; | |
} | |
} | |
swiftFileContent+="\n" | |
+ " required init?(_ map: Map) {\n\n" | |
+ " }\n"; | |
swiftFileContent+="\n" | |
+ " // Mappable\n" | |
+ " func mapping(map: Map) {\n"; | |
for (String varName : varNames) { | |
swiftFileContent+=" "+varName+ " <- map[\""+varName+"\"]\n"; | |
} | |
swiftFileContent+=" }\n"; | |
swiftFileContent+="}"; | |
return swiftFileContent; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment