json.org에서 재공되는 java api는 너무 보편화된 json 작성법을 따르는지라, 내부적인 프로젝트에 맞게 customizing하려 조금 끄적거리다 울컥~ 다 뒤집고 새로 만들어 버리다.
내용 : json -> markup + css로 export
접근 :
I. String으로 받은 json을 char단위로 읽어 의미있는 문장으로 분리하여 저장하다.
II. I에서 생성한 목록을 Linked List object에 저장한다.
III. object를 순차적으로 읽어 markup과 css로 분리 출력한다.
처음에는 II와 III부분을 recursive하게 만들어서 작성하려 했으나 시뮬로 돌려본 결과 double linked list를 사용하여 작성해야 함을 알게 되었다. 결국 III번만 recursive하게 작성하다.
구현 :
I.
public String nextValue(String str){
StringBuffer sb = new StringBuffer();
char tmpchar;
boolean content_flag = false;
while(++idx < str.length()){
tmpchar = str.charAt(idx);
if(tmpchar=='\''){
if(content_flag)
content_flag = false;
else
content_flag = true;
continue;
}
if(!content_flag){
if(tmpchar==':'||tmpchar==',') break;
}
if(tmpchar==' '){ if(!content_flag) continue; }
if(tmpchar=='{' || tmpchar=='}'){ continue; }
if(tmpchar=='[') continue;
if(tmpchar==']') break;
if(tmpchar=='\r'||tmpchar=='\t'||tmpchar=='\n') continue;
sb.append(tmpchar);
}
return sb.toString();
}
II.
public JsonObject json2object(JsonObject rootobj, String[] jsonlist,Map markupmap){
JsonList jlist = new JsonList();
jlist.addToList(rootobj);
JsonObject jobj = null;
while(loopIdx<jsonlist.length){
jobj = nextJsonObject(jsonlist,markupmap);
jlist.addToList(jobj);
if(loopIdx<jsonlist.length){
if(jsonlist[loopIdx].equals("true")){
jlist.moveNext();
loopIdx = loopIdx+2;
}else{
if(jsonlist[loopIdx+1].equals("]")){
jlist.movePrev();
loopIdx = loopIdx+3;
}else{
loopIdx++;
}
}
}
}
return rootobj;
}
public JsonObject nextJsonObject(String[] jsonlist,Map markupmap){
JsonObject jobj = new JsonObject();
String str_flag;
boolean style_flag = false;
boolean type_flag = false;
while(loopIdx<jsonlist.length){
str_flag = jsonlist[loopIdx++];
if(str_flag.startsWith("//")) continue;
if(str_flag.equals("type")){
if(type_flag){
if(style_flag){
jobj.setStyles(str_flag, jsonlist[loopIdx++]);
}else{
jobj.setValues(str_flag, jsonlist[loopIdx++]);
}
}else{
type_flag = true;
jobj.setId(jsonlist[loopIdx++]);
jobj.setMarkup((String)markupmap.get(jsonlist[loopIdx++]));
}
}else if(str_flag.startsWith("style_")){
if(str_flag.equals("style_id")){
jobj.setCss_flag('I');
}else if(str_flag.equals("style_class")){
jobj.setCss_flag('C');
}else if(str_flag.equals("style_tag")){
jobj.setCss_flag('T');
}else{
jobj.setCss_flag('N');
}
style_flag = true;
}else if(str_flag.equals("subYN")){
return jobj;
}else if(str_flag.equals("")){
}else{
if(style_flag){
jobj.setStyles(str_flag, jsonlist[loopIdx++]);
}else{
jobj.setValues(str_flag, jsonlist[loopIdx++]);
}
}
}
return jobj;
}
III.
public JsonObject recursiveHTML(JsonObject jobj){
tmpcss = makeStylesheet(jobj); //CSS 만들기
if(!tmpcss.equals(""))
css_word += tmpcss+"\n";
tmpStr = StringUtil.prefixMarkup(jobj.getMarkup());
if(!tmpStr.trim().equals("")){
jobj.addHtmlString(tmpStr+"\r\n");
}
if(jobj.isSubYN){
for(int i=0; i<jobj.getSubJson().length; i++){
String aaaa = recursiveHTML(jobj.getSubJson()[i]).getHtmlString();
jobj.addHtmlString("\t"+aaaa);
}
}
tmpStr = StringUtil.suffixMarkup(jobj.getMarkup());
if(!tmpStr.trim().equals("")){
jobj.addHtmlString(tmpStr+"\r\n");
}
return jobj;
}
CSS생성 로직과 JsonObject는 생략... 중간에 부모의 ArrayList를 받아 자식의 path를 추가하는 로직이 있었는데, 아놔~ 링크로 걸리는 바람에 debug하느라 고생했다는.....orz
이 지저분하고 막무가내에 메모리를 퍼먹는 소스를 리팩토링하려면.. 주말을 투자해야 할듯하다. 휴일따윈 필요없어, 여름!!! 핑~
추가]
어벙하게 recursive하게 작성했던 소스들...
public JsonObject recursiveProcess(JsonObject jobj,String[] jsonlist,Map markupmap,boolean start_flag){
String str_flag;
boolean style_flag = false;
boolean submarkup_flag = false;
while(loopIdx<jsonlist.length){
str_flag = jsonlist[loopIdx++];
if(str_flag.equals("type")){
style_flag = false;
if(start_flag || submarkup_flag){
JsonObject subObj = new JsonObject();
subObj.setId(jsonlist[loopIdx++]);
subObj.setMarkup((String)markupmap.get(jsonlist[loopIdx++]));// type 뒤에 태그가 2번 나오므로 한번 더 넘겨보냄.
List templist = jobj.getCsspath();
subObj.setCsspath(templist);
jobj.addSubJson(recursiveProcess(subObj,jsonlist,markupmap,false));
}
}else if(str_flag.startsWith("style_")){
if(str_flag.equals("style_id")){
jobj.setCss_flag('I');
}else if(str_flag.equals("style_class")){
jobj.setCss_flag('C');
}else{
jobj.setCss_flag('T');
}
style_flag = true;
}else if(str_flag.equals("subYN")){
if(jsonlist[loopIdx++].equals(false)){
jobj.addCsspath();
return jobj;
}else{
jobj.addCsspath();
}
style_flag = false;
}else if(str_flag.equals("subMarkup")){
style_flag = false;
submarkup_flag = true;
}else if(str_flag.equals("]")){
return jobj;
}else if(str_flag.equals("")){
}else{
if(style_flag){
jobj.setStyles(str_flag, jsonlist[loopIdx++]);
}else{
jobj.setValues(str_flag, jsonlist[loopIdx++]);
}
}
}
return jobj;
}
Trackback Address
http://babyp.net/trackback/442