코드를 깔끔하게 하는 리펙토리 작업을 배워보았다.
리펙토리 작업을한 소스코드 깔끔해!
package service1;
import java.io.IOException;
import kr.pe.okjsp.rss.RSSProxy;
public class NaverShopping {
/**
* @param query
* @return
* @throws IOException
*/
public static String getProduct(String query) throws IOException {
String path = "http://openapi.naver.com/search?key=6f174XXXXXXXXXX6de91072c9&query="+query
+"&display=5&start=1&target=shop&sort=sim";
String xml = RSSProxy.getXML(path,"utf-8");
String product = getValue(xml, "item");
String title = getValue(product, "title");
title = title.replaceAll("<", "<");
title = title.replaceAll(">", ">");
String link = getValue(product, "link");
String image = getValue(product, "image");
String lprice = getValue(product, "lprice");
String htmlCode = "<link rel=\"stylesheet\" type=\"text/css\" href=\"http://www.okjsp.pe.kr/mashup/style.css\" />"+
"<table class=\"shopg\"><tr><td rowspan=\"2\"><a href=\""+link+"\">" +
"<img src=\""+image+"\"></a></td>" +
"<td><div class=\"shopg_name\">"+title+"</div></td></tr>" +
"<tr><td>"+lprice+"</td></tr></table>";
return htmlCode;
}
private static String getValue(String product, String nodeName) {
int tagStart = product.indexOf("<"+nodeName+">");
int tagEnd = product.indexOf("</"+nodeName+">");
return product.substring(tagStart+nodeName.length()+2, tagEnd);
}
}
하지만 처음 소스를 접하는 사람들이나 초보자에게는 리펙토리 작업을 한 소스보단 이전의 소스를 보고 이해하는 것이 더 쉽겠죠?
