Java tutorial
package com.castis.sysComp; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.LineNumberReader; import java.io.Reader; import java.io.UnsupportedEncodingException; import java.nio.ByteBuffer; import java.nio.channels.GatheringByteChannel; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.http.NameValuePair; import org.apache.http.message.BasicNameValuePair; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.dao.DataIntegrityViolationException; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import com.castis.common.Constant.RequestMethodType; import com.castis.common.exception.DataParsingException; import com.castis.dto.InputDataDTO; import com.castis.dto.ResponseDTO; import com.castis.dto.TreeNodeDTO; import com.castis.dto.TreeNodeForCount; import com.castis.dto.WeekHourPair; import com.castis.dto.sceneDTO; import com.castis.dto.subDataDTO; import com.castis.util.CiFileUtil; import com.castis.util.FilePolling; import com.castis.util.HttpUtil; import com.google.gson.Gson; public class PoisConverterSysComp { private static Log log = LogFactory.getLog(PoisConverterSysComp.class); @Autowired @Qualifier("filePolling") private FilePolling filePolling; @Autowired private HttpUtil httpUtil; @Value("#{propertyConfigurer['pois.url']}") private String poisUrl; @Value("#{propertyConfigurer['file.resultDir']}") private String resultDir; public void setFilePolling(FilePolling filePolling) { this.filePolling = filePolling; } public static final String FILE_CHARSET = "UTF-8"; private Map<String, List<TreeNodeDTO>> treeMap = new HashMap<String, List<TreeNodeDTO>>(); private Map<WeekHourPair, Map<String, TreeNodeForCount>> trees = new HashMap<WeekHourPair, Map<String, TreeNodeForCount>>(); private String type = ""; public boolean loadFileList() { if (filePolling.checkMadeFolder() == false) return false; List<File> tempFileList = filePolling.getTargetFileList(); for (File file : tempFileList) { try { log.info("============================================="); log.info("file name : " + file.getName()); log.info("Running Running ==>>>>>"); String filename = file.getName(); String parentArr[] = filename.split("-"); String parent = parentArr[0]; String subArr[] = parentArr[1].split("_"); String sub = subArr[0]; if (parent.equalsIgnoreCase("viewcount")) { // parseViewCountFile(file); filePolling.moveSuccessFile(file); } else if (parent.equalsIgnoreCase("category")) { // - parseCategoryFile(file, "category"); } else if (parent.equalsIgnoreCase("allCategory")) { // - parseCategoryFile(file, "allCategory"); } else if (parent.equalsIgnoreCase("channel")) { // - ? parseChannelFile(file, sub); } else if (parent.equalsIgnoreCase("") || parent == null) { if (sub.equalsIgnoreCase("content")) { // - ? parseContentFile(file); } else if (sub.equalsIgnoreCase("region")) { // - parseRegionFile(file); filePolling.moveSuccessFile(file); } else if (sub.equalsIgnoreCase("clientUI")) { // clientUI parseClientUIFile(file); filePolling.moveSuccessFile(file); } } log.info("file name : " + file.getName() + " process complete"); log.info("============================================="); } catch (DataParsingException e) { log.error(e); filePolling.moveFailFile(file, "dataParsing", resultDir); } catch (DataIntegrityViolationException e) { log.error(e); filePolling.moveFailFile(file, "dataIntegrityViolationException", resultDir); } catch (Exception e) { log.error(e); filePolling.moveFailFile(file, e.getClass().getSimpleName(), resultDir); } } treeMap.clear(); return true; } public void parseViewCountFile(File file) throws Exception { String line = ""; FileInputStream in = null; Reader isReader = null; LineNumberReader bufReader = null; if (file.getName().contains("channel")) { type = "channel"; } else { type = "vod"; } try { String encodingCharset = FILE_CHARSET; in = new FileInputStream(file); isReader = new InputStreamReader(in, encodingCharset); bufReader = new LineNumberReader(isReader); boolean first = true; while ((line = bufReader.readLine()) != null) { if (line.length() == 0) { continue; } InputDataDTO data = new InputDataDTO(); String result[] = line.split("\\|"); if (first == true && result.length <= 1) { first = false; continue; } if (result[0] == null || result[0].equals("")) { throw new DataParsingException("data parsing error(region)"); } if (result[1] == null || result[1].equals("")) { throw new DataParsingException("data parsing error(category)"); } if (result[2] == null || result[2].equals("")) { throw new DataParsingException("data parsing error(weekday)"); } if (result[3] == null || result[3].equals("")) { throw new DataParsingException("data parsing error(hour)"); } if (result[4] == null || result[4].equals("")) { throw new DataParsingException("data parsing error(platform)"); } if (result[5] == null || result[5].equals("")) { throw new DataParsingException("data parsing error(count)"); } String platform = result[4]; if (platform != null && platform.equalsIgnoreCase("stb")) platform = "STB"; else if (platform != null && platform.equalsIgnoreCase("mobile")) { platform = "Mobile"; } data.setPlatform(platform); List<TreeNodeDTO> tree = treeMap.get(platform); if (tree == null) { tree = getAxis(platform); treeMap.put(platform, tree); } String fullpath = getFullPath(tree, result[0]); if (fullpath == "") { throw new DataParsingException("region ID[" + result[0] + "] can't find fullpath."); } data.setRegion(fullpath); data.setCategory(result[1]); data.setWeekday(result[2]); data.setHour(result[3]); data.setCount(Integer.parseInt(result[5])); insertTreeNode(data); } for (WeekHourPair key : trees.keySet()) { Map<String, TreeNodeForCount> map = trees.get(key); calculateCount(map); } writeFile(file); } catch (Exception e) { String errorMsg = "Fail to parsing Line.[current line(" + bufReader.getLineNumber() + ") :" + line + "] : "; log.error(errorMsg, e); throw new DataParsingException(errorMsg, e); //throw(e); } finally { if (in != null) in.close(); if (isReader != null) isReader.close(); if (bufReader != null) bufReader.close(); } } private void writeFile(File file) throws IOException { Map<String, String> middleNodeMap = new HashMap<String, String>(); String line = ""; FileInputStream in = null; Reader isReader = null; LineNumberReader bufReader = null; FileOutputStream fos = null; String dir = filePolling.getValidFileDirectory(resultDir); String fileName = file.getName(); int index = fileName.indexOf("-"); if (index != -1) { fileName = fileName.substring(index + 1, fileName.length()); } String tempDir = dir + "/temp/"; File targetDirectory = new File(CiFileUtil.getReplaceFullPath(tempDir)); if (!targetDirectory.isDirectory()) { CiFileUtil.createDirectory(tempDir); } fos = new FileOutputStream(tempDir + fileName); int byteSize = 2048; ByteBuffer byteBuffer = ByteBuffer.allocateDirect(byteSize); GatheringByteChannel outByteCh = fos.getChannel(); try { String encodingCharset = FILE_CHARSET; in = new FileInputStream(file); isReader = new InputStreamReader(in, encodingCharset); bufReader = new LineNumberReader(isReader); boolean first = true; while ((line = bufReader.readLine()) != null) { if (line.length() == 0) { continue; } InputDataDTO data = new InputDataDTO(); String result[] = line.split("\\|"); if (first == true && result.length <= 1) { first = false; continue; } String platform = result[4]; if (platform != null && platform.equalsIgnoreCase("stb")) platform = "STB"; else if (platform != null && platform.equalsIgnoreCase("mobile")) { platform = "Mobile"; } data.setPlatform(platform); List<TreeNodeDTO> tree = treeMap.get(platform); if (tree == null) { tree = getAxis(platform); treeMap.put(platform, tree); } String fullpath = getFullPath(tree, result[0]); data.setRegion(fullpath); data.setCategory(result[1]); data.setWeekday(result[2]); data.setHour(result[3]); data.setCount(Integer.parseInt(result[5])); List<subDataDTO> subDataList = writeNodeInfoOnFile(byteSize, byteBuffer, outByteCh, data, "Y"); if (subDataList != null && subDataList.size() > 0) { writeMiddleNode(byteSize, byteBuffer, outByteCh, data, middleNodeMap, subDataList, "N"); } } fos.close(); index = fileName.indexOf("_"); String targetDir = resultDir; File sourceFile = new File(tempDir + fileName); if (index != -1) { String directory = fileName.substring(0, index); targetDir += "/viewCount/" + directory; } try { File resultTargetDir = new File(CiFileUtil.getReplaceFullPath(targetDir)); if (!resultTargetDir.isDirectory()) { CiFileUtil.createDirectory(targetDir); } CiFileUtil.renameFile(sourceFile, targetDir, fileName); } catch (Exception e) { log.error(e.getMessage()); } } catch (Exception e) { String errorMsg = "Fail to parsing Line.[current line(" + bufReader.getLineNumber() + ") :" + line + "] : "; log.error(errorMsg, e); throw new DataParsingException(errorMsg, e); //throw(e); } finally { if (in != null) in.close(); if (isReader != null) isReader.close(); if (bufReader != null) bufReader.close(); } } private List<subDataDTO> writeNodeInfoOnFile(int byteSize, ByteBuffer byteBuffer, GatheringByteChannel outByteCh, InputDataDTO data, String isLeafNode) { StringBuffer strBuffer = new StringBuffer(); strBuffer.append(data.getRegion()); strBuffer.append("|"); strBuffer.append(data.getCategory()); strBuffer.append("|"); strBuffer.append(data.getWeekday()); strBuffer.append("|"); strBuffer.append(data.getHour()); strBuffer.append("|"); strBuffer.append(data.getPlatform()); strBuffer.append("|"); strBuffer.append(data.getCount()); List<subDataDTO> subDataList = getSubDataList(data); // leafNode or not(Y/N) strBuffer.append("|"); strBuffer.append(isLeafNode); strBuffer.append("\r\n"); byte[] outByte = null; try { outByte = strBuffer.toString().getBytes("UTF-8"); } catch (UnsupportedEncodingException e2) { e2.printStackTrace(); } byteBuffer.put(outByte); byteBuffer.flip(); try { outByteCh.write(byteBuffer); } catch (IOException e) { } byteBuffer.clear(); return subDataList; } private void writeMiddleNode(int byteSize, ByteBuffer byteBuffer, GatheringByteChannel outByteCh, InputDataDTO data, Map<String, String> middleNodeMap, List<subDataDTO> subDataList, String isLeafNode) { for (int t = 0; t < subDataList.size(); t++) { subDataDTO subDataDto = subDataList.get(t); InputDataDTO middleNodeDto = new InputDataDTO(); middleNodeDto.setPlatform(data.getPlatform()); middleNodeDto.setWeekday(data.getWeekday()); middleNodeDto.setHour(data.getHour()); middleNodeDto.setCount(subDataDto.getCount()); middleNodeDto.setRegion(subDataDto.getRegion_id()); String myKey = data.getWeekday() + "&" + data.getHour() + "&" + subDataDto.getRegion_id() + "&"; if (type.equals("channel")) { myKey += subDataDto.getChannel_id(); middleNodeDto.setCategory(subDataDto.getChannel_id()); } else { myKey += subDataDto.getCategory_id(); middleNodeDto.setCategory(subDataDto.getCategory_id()); } if (!middleNodeMap.containsKey(myKey)) { writeNodeInfoOnFile(byteSize, byteBuffer, outByteCh, middleNodeDto, isLeafNode); middleNodeMap.put(myKey, "true"); } } } private List<subDataDTO> getSubDataList(InputDataDTO data) { List<subDataDTO> list = new ArrayList<subDataDTO>(); String[] categorys = data.getCategory().split("\\/"); String[] regions = data.getRegion().split("\\/"); StringBuffer category = new StringBuffer(); for (int i = 0; i < categorys.length; i++) { if (i == 0) category.append(categorys[i]); else category.append("/").append(categorys[i]); StringBuffer region = new StringBuffer(); for (int j = 0; j < regions.length; j++) { if (i == (categorys.length - 1) && j == (regions.length - 1)) continue; subDataDTO sub = new subDataDTO(); if (j == 0) region.append(regions[j]); else region.append("/").append(regions[j]); if (type.equals("channel")) { sub.setChannel_id(category.toString()); } else { sub.setCategory_id(category.toString()); } sub.setRegion_id(region.toString()); WeekHourPair pairKey = new WeekHourPair(data.getWeekday(), data.getHour()); Map<String, TreeNodeForCount> categoryMap = findCategoryMap(trees.get(pairKey), category.toString()); long count = getCount(categoryMap, region.toString()); sub.setCount(count); list.add(sub); } } return list; } private long getCount(Map<String, TreeNodeForCount> map, String region) { int sum = 0; for (String key : map.keySet()) { TreeNodeForCount node = map.get(key); if (node.getType().equals("category")) { sum += getCount(node.getChildren(), region); } else { TreeNodeForCount treeNode = node; String[] path = region.split("\\/"); for (int i = 0; i < path.length; i++) { if (path[i].length() == 0 || node.getId().equals(path[i])) { continue; } if (treeNode != null && treeNode.getChildren() != null) //treeNode = getRegionCount(treeNode.getChildren(), path[i]); treeNode = treeNode.getChildren().get(path[i]); } if (treeNode != null) return treeNode.getCount(); else return 0; } } return sum; } private Map<String, TreeNodeForCount> findCategoryMap(Map<String, TreeNodeForCount> map, String category) { String cate[] = category.split("\\/"); String c = category.substring(category.indexOf("/") + 1, category.length()); TreeNodeForCount node = map.get(cate[0]); if (node == null) return map; else { return findCategoryMap(node.getChildren(), c); } } private void calculateCount(Map<String, TreeNodeForCount> map) { for (String key : map.keySet()) { TreeNodeForCount node = map.get(key); long sum = 0; if (node.getChildren() != null) { calculateCount(node.getChildren()); for (String childKey : node.getChildren().keySet()) { TreeNodeForCount child = node.getChildren().get(childKey); sum += child.getCount(); } node.setCount(sum); } } } private void insertTreeNode(InputDataDTO data) { String[] categorys = data.getCategory().split("\\/"); String[] regions = data.getRegion().split("\\/"); TreeNodeForCount node = null; WeekHourPair pairKey = new WeekHourPair(data.getWeekday(), data.getHour()); Map<String, TreeNodeForCount> tree = trees.get(pairKey); if (tree == null) { tree = new HashMap<String, TreeNodeForCount>(); trees.put(pairKey, tree); } for (String c : categorys) { if (tree == null) { tree = new HashMap<String, TreeNodeForCount>(); node.setChildren(tree); } if (tree.get(c) == null) { node = new TreeNodeForCount(); node.setId(c); node.setType("category"); tree.put(c, node); } else { node = tree.get(c); } tree = node.getChildren(); } for (String r : regions) { if (tree == null) { tree = new HashMap<String, TreeNodeForCount>(); node.setChildren(tree); } if (tree.get(r) == null) { node = new TreeNodeForCount(); node.setId(r); node.setType("region"); tree.put(r, node); } else { node = tree.get(r); } tree = node.getChildren(); } node.setCount(data.getCount()); } public List<TreeNodeDTO> getAxis(String platform) throws Exception { List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("axisType", "region")); params.add(new BasicNameValuePair("platform", platform)); Gson gson = new Gson(); String resultJsonString = httpUtil.requestHttp(poisUrl + "getAxis", params, RequestMethodType.POST); ResponseDTO response = gson.fromJson(resultJsonString, ResponseDTO.class); if (response.getError() == null) { List<TreeNodeDTO> tree = response.getResult(); setTreeFullPath(tree, ""); return tree; } else { throw new Exception(response.getError().getName()); } } public void setTreeFullPath(List<TreeNodeDTO> tree, String parentPath) { for (int i = 0; i < tree.size(); i++) { TreeNodeDTO node = tree.get(i); if (parentPath.length() != 0) node.setFullpath(parentPath + "/" + node.getId()); else node.setFullpath(node.getId()); if (node.getChildren() != null && node.getChildren().size() > 0) { setTreeFullPath(node.getChildren(), node.getFullpath()); } } } public String getFullPath(List<TreeNodeDTO> tree, String id) { for (int i = 0; i < tree.size(); i++) { TreeNodeDTO node = tree.get(i); if (id != null && id.equals(node.getId())) { return node.getFullpath(); } if (node.getChildren() != null && node.getChildren().size() > 0) { String result = getFullPath(node.getChildren(), id); if (result != "") return result; } } return ""; } public void parseContentFile(File file) throws Exception { DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(file); doc.getDocumentElement().normalize(); try { NodeList pList = doc.getElementsByTagName("xml-fragment"); Node pNode = pList.item(0); if (pNode.getNodeType() == Node.ELEMENT_NODE) { Element eElement = (Element) pNode; String id = null; String title = null; String platformType = null; String cpId = null; String licenseStartDate = null; String licenseEndDate = null; if (eElement.getElementsByTagName("Content_ID").getLength() > 0) id = eElement.getElementsByTagName("Content_ID").item(0).getTextContent(); if (eElement.getElementsByTagName("Title").getLength() > 0) title = eElement.getElementsByTagName("Title").item(0).getTextContent(); if (eElement.getElementsByTagName("Platform_Type").getLength() > 0) platformType = eElement.getElementsByTagName("Platform_Type").item(0).getTextContent(); if (eElement.getElementsByTagName("Cp_Id").getLength() > 0) cpId = eElement.getElementsByTagName("Cp_Id").item(0).getTextContent(); if (eElement.getElementsByTagName("License_Start_Date").getLength() > 0) licenseStartDate = eElement.getElementsByTagName("License_Start_Date").item(0).getTextContent(); if (eElement.getElementsByTagName("License_End_Date").getLength() > 0) licenseEndDate = eElement.getElementsByTagName("License_End_Date").item(0).getTextContent(); if (id == null || id.equals("")) { throw new DataParsingException("data parsing error(Content_ID)"); } if (title == null || title.equals("")) { throw new DataParsingException("data parsing error(Title)"); } if (platformType == null || platformType.equals("")) { throw new DataParsingException("data parsing error(Platform_Type)"); } if (cpId == null || cpId.equals("")) { throw new DataParsingException("data parsing error(Cp_Id)"); } if (licenseStartDate == null || licenseStartDate.equals("")) { throw new DataParsingException("data parsing error(License_Start_Date)"); } if (licenseEndDate == null || licenseEndDate.equals("")) { throw new DataParsingException("data parsing error(License_End_Date)"); } } String fileName = file.getName(); int index = fileName.indexOf("-"); if (index != -1) { fileName = fileName.substring(index + 1, fileName.length()); } index = fileName.indexOf("_"); String targetDir = resultDir; if (index != -1) { String directory = fileName.substring(0, index); targetDir += "/" + directory; } try { File resultTargetDir = new File(CiFileUtil.getReplaceFullPath(targetDir)); if (!resultTargetDir.isDirectory()) { CiFileUtil.createDirectory(targetDir); } CiFileUtil.renameFile(file, targetDir, fileName); } catch (Exception e) { log.error(e.getMessage()); } } catch (Exception e) { String errorMsg = e.getMessage(); log.error(errorMsg, e); throw new DataParsingException(errorMsg, e); //throw(e); } } public void parseCategoryFile(File file, String sub) throws Exception { String line = ""; FileInputStream in = null; Reader isReader = null; LineNumberReader bufReader = null; try { String encodingCharset = FILE_CHARSET; in = new FileInputStream(file); isReader = new InputStreamReader(in, encodingCharset); bufReader = new LineNumberReader(isReader); boolean first = true; while ((line = bufReader.readLine()) != null) { if (first == true) { first = false; continue; } //Depth, Menu ID, Menu Name, Hidden, Menu Type. String result[] = line.split(","); String depth = result[0]; String menuId = result[1]; String menuName = result[2]; String hidden = result[3]; String menuType = result[5]; if (depth == null || depth.equals("")) { throw new DataParsingException("data parsing error(depth)"); } if (menuId == null || menuId.equals("")) { throw new DataParsingException("data parsing error(Menu ID)"); } if (menuName == null || menuName.equals("")) { throw new DataParsingException("data parsing error(Menu Name)"); } if (hidden == null || hidden.equals("")) { throw new DataParsingException("data parsing error(Hidden)"); } if (menuType == null || menuType.equals("")) { throw new DataParsingException("data parsing error(Menu Type)"); } } } catch (Exception e) { String errorMsg = e.getMessage(); log.error(errorMsg, e); throw new DataParsingException(errorMsg, e); //throw(e); } finally { if (in != null) in.close(); if (isReader != null) isReader.close(); if (bufReader != null) bufReader.close(); } String fileName = file.getName(); int index = fileName.indexOf("-"); if (index != -1) { fileName = fileName.substring(index + 1, fileName.length()); } index = fileName.indexOf("_"); String targetDir = resultDir; if (index != -1) { String directory = fileName.substring(0, index); targetDir += "/" + sub + "/" + directory; } try { File resultTargetDir = new File(CiFileUtil.getReplaceFullPath(targetDir)); if (!resultTargetDir.isDirectory()) { CiFileUtil.createDirectory(targetDir); } CiFileUtil.renameFile(file, targetDir, fileName); } catch (Exception e) { log.error(e.getMessage()); } } public void parseChannelFile(File file, String sub) throws Exception { DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(file); doc.getDocumentElement().normalize(); if (sub.equalsIgnoreCase("stb")) { try { NodeList pList = doc.getElementsByTagName("ProgramGuide"); for (int i = 0; i < pList.getLength(); i++) { Node pNode = pList.item(i); if (pNode.getNodeType() == Node.ELEMENT_NODE) { Element eElement = (Element) pNode; NodeList serviceList = eElement.getElementsByTagName("Service"); for (int j = 0; j < serviceList.getLength(); j++) { Node sNode = serviceList.item(j); if (sNode.getNodeType() == Node.ELEMENT_NODE) { Element sElement = (Element) sNode; String id = sElement.getAttribute("id"); if (id == null || id.equals("")) throw new DataParsingException("data parsing error(service id)"); String name = null; Element nameElement = (Element) sElement.getElementsByTagName("ServiceName") .item(0); if (nameElement.getElementsByTagName("Name").getLength() > 0) { name = nameElement.getElementsByTagName("Name").item(0).getTextContent(); } if (name == null || name.equals("")) throw new DataParsingException("data parsing error(service name)"); } } } } } catch (Exception e) { String errorMsg = e.getMessage(); log.error(errorMsg, e); throw new DataParsingException(errorMsg, e); //throw(e); } } else { try { NodeList pList = doc.getElementsByTagName("Province"); for (int i = 0; i < pList.getLength(); i++) { Node pNode = pList.item(i); if (pNode.getNodeType() == Node.ELEMENT_NODE) { Element eElement = (Element) pNode; NodeList serviceList = eElement.getElementsByTagName("ProvService"); for (int j = 0; j < serviceList.getLength(); j++) { Node sNode = serviceList.item(j); if (sNode.getNodeType() == Node.ELEMENT_NODE) { Element sElement = (Element) sNode; String id = sElement.getAttribute("id"); if (id == null || id.equals("")) throw new DataParsingException("data parsing error(service id)"); } } } } } catch (Exception e) { String errorMsg = e.getMessage(); log.error(errorMsg, e); throw new DataParsingException(errorMsg, e); //throw(e); } } String fileName = file.getName(); int index = fileName.indexOf("-"); if (index != -1) { fileName = fileName.substring(index + 1, fileName.length()); } index = fileName.indexOf("_"); String targetDir = resultDir; if (index != -1) { String directory = fileName.substring(0, index); targetDir += "/channel/" + directory; } try { File resultTargetDir = new File(CiFileUtil.getReplaceFullPath(targetDir)); if (!resultTargetDir.isDirectory()) { CiFileUtil.createDirectory(targetDir); } CiFileUtil.renameFile(file, targetDir, fileName); } catch (Exception e) { log.error(e.getMessage()); } } public void parseClientUIFile(File file) throws Exception { DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(file); doc.getDocumentElement().normalize(); try { NodeList pList = doc.getElementsByTagName("clientUIInventory"); Node pNode = pList.item(0); List<sceneDTO> sceneDTOList = new ArrayList<sceneDTO>(); String platformType = ""; if (pNode.getNodeType() == Node.ELEMENT_NODE) { Element eElement = (Element) pNode; if (eElement.getElementsByTagName("adPlatformType").getLength() > 0) platformType = eElement.getElementsByTagName("adPlatformType").item(0).getTextContent(); else { if (platformType == null || platformType.equals("")) throw new DataParsingException("data parsing error(adPlatformType)"); } NodeList sceneList = eElement.getElementsByTagName("scene"); for (int i = 0; i < sceneList.getLength(); i++) { Node sNode = sceneList.item(i); if (sNode.getNodeType() == Node.ELEMENT_NODE) { Element sElement = (Element) sNode; String id = sElement.getAttribute("id"); if (id == null || id.equals("")) throw new DataParsingException("data parsing error(scene id)"); String name = sElement.getAttribute("name"); if (name == null || name.equals("")) throw new DataParsingException("data parsing error(scene name)"); String templateFileName = null; if (sElement.getElementsByTagName("templateFileName").getLength() > 0) templateFileName = sElement.getElementsByTagName("templateFileName").item(0) .getTextContent(); if (templateFileName == null || templateFileName.equals("")) { throw new DataParsingException("data parsing error(templateFileName)"); } Node rNode = null; if (sElement.getElementsByTagName("relatedMenuList").getLength() > 0) rNode = sElement.getElementsByTagName("relatedMenuList").item(0); if (rNode == null || rNode.equals("")) { throw new DataParsingException("data parsing error(relatedMenuList)"); } NodeList relatedMenuList = ((Element) rNode).getElementsByTagName("relatedMenu"); if (relatedMenuList.getLength() == 0) { throw new DataParsingException("data parsing error(relatedMenuList)"); } for (int j = 0; j < relatedMenuList.getLength(); j++) { Node relatedNode = relatedMenuList.item(j); if (relatedNode.getNodeType() == Node.ELEMENT_NODE) { Element rElement = (Element) relatedNode; String menuId = rElement.getAttribute("id"); String menuName = rElement.getAttribute("name"); if (menuId == null || menuId.equals("")) throw new DataParsingException("data parsing error(related menu id)"); if (menuName == null || menuName.equals("")) throw new DataParsingException("data parsing error(related menu name)"); Node adNode = null; if (sElement.getElementsByTagName("adSpaceList").getLength() > 0) adNode = sElement.getElementsByTagName("adSpaceList").item(0); if (adNode == null || adNode.equals("")) { throw new DataParsingException("data parsing error(adSpaceList)"); } NodeList spaceList = ((Element) adNode).getElementsByTagName("adSpace"); if (spaceList.getLength() == 0) { throw new DataParsingException("data parsing error(adSpace)"); } for (int k = 0; k < spaceList.getLength(); k++) { Node spaceNode = spaceList.item(k); if (spaceNode.getNodeType() == Node.ELEMENT_NODE) { Element spaceElement = (Element) spaceNode; String spaceId = spaceElement.getAttribute("id"); String spaceName = spaceElement.getAttribute("name"); if (spaceId == null || spaceId.equals("")) throw new DataParsingException("data parsing error(space id)"); if (spaceName == null || spaceName.equals("")) throw new DataParsingException("data parsing error(space name)"); String resolution = null; String onFocus = null; String clickable = null; if (spaceElement.getElementsByTagName("size").getLength() > 0) resolution = spaceElement.getElementsByTagName("size").item(0) .getTextContent(); if (spaceElement.getElementsByTagName("sizeOnFocus").getLength() > 0) onFocus = spaceElement.getElementsByTagName("sizeOnFocus").item(0) .getTextContent(); String sizeLimit = ""; NodeList tempList = spaceElement.getElementsByTagName("fileSizeLimit"); if (tempList != null && tempList.getLength() > 0) { sizeLimit = spaceElement.getElementsByTagName("fileSizeLimit").item(0) .getTextContent(); } if (spaceElement.getElementsByTagName("clickable").getLength() > 0) clickable = spaceElement.getElementsByTagName("clickable").item(0) .getTextContent(); if (resolution == null || resolution.equals("")) throw new DataParsingException("data parsing error(size)"); if (clickable == null || clickable.equals("")) throw new DataParsingException("data parsing error(clickable)"); if (clickable.equalsIgnoreCase("Y") && (onFocus == null || onFocus.equals(""))) throw new DataParsingException("data parsing error(sizeOnFocus)"); sceneDTO sceneDTO = new sceneDTO(); sceneDTO.setId(id); sceneDTO.setName(name); sceneDTO.setTemplateFileName(templateFileName); sceneDTO.setMenuId(menuId); sceneDTO.setMenuName(menuName); sceneDTO.setSpaceId(spaceId); sceneDTO.setSpaceName(spaceName); sceneDTO.setResolution(resolution); sceneDTO.setResolutionOnFocus(onFocus); sceneDTO.setSizeLimit(sizeLimit); if (sizeLimit == null || sizeLimit == "") { sceneDTO.setSizeLimit("-1"); } sceneDTO.setClickable(clickable); sceneDTOList.add(sceneDTO); if (checkDuplicateMenu(sceneDTOList, sceneDTO) == false) { throw new DataParsingException("data parsing error(duplicate menu)"); } } } } } } } } writeClientUIFile(sceneDTOList, platformType, file); } catch (Exception e) { String errorMsg = e.getMessage(); log.error(errorMsg, e); throw new DataParsingException(errorMsg, e); //throw(e); } } private boolean checkDuplicateMenu(List<sceneDTO> list, sceneDTO scene) { boolean result = true; for (int i = 0; i < list.size(); i++) { sceneDTO sceneDTO = list.get(i); if (sceneDTO.getId().equals(scene.getId())) { continue; } if (sceneDTO.getMenuId().equals(scene.getMenuId())) return false; } return result; } private void writeClientUIFile(List<sceneDTO> list, String platform, File file) throws FileNotFoundException { FileOutputStream fos = null; String dir = filePolling.getValidFileDirectory(resultDir); String fileName = file.getName(); String tempDir = dir + "/temp/"; File targetDirectory = new File(CiFileUtil.getReplaceFullPath(tempDir)); if (!targetDirectory.isDirectory()) { CiFileUtil.createDirectory(tempDir); } fos = new FileOutputStream(tempDir + fileName); int byteSize = 2048; ByteBuffer byteBuffer = ByteBuffer.allocateDirect(byteSize); GatheringByteChannel outByteCh = fos.getChannel(); try { for (int i = 0; i < list.size(); i++) { sceneDTO scene = list.get(i); StringBuffer strBuffer = new StringBuffer(); if (i == 0) { strBuffer.append("policy"); strBuffer.append("|"); strBuffer.append(platform); strBuffer.append("|"); strBuffer.append("ClientUI"); strBuffer.append("\r\n"); } strBuffer.append("info"); strBuffer.append("|"); strBuffer.append(platform); strBuffer.append("|"); strBuffer.append(scene.getId()); strBuffer.append("|"); strBuffer.append(scene.getName()); strBuffer.append("|"); strBuffer.append(scene.getTemplateFileName()); strBuffer.append("|"); strBuffer.append(scene.getMenuId()); strBuffer.append("|"); strBuffer.append(scene.getMenuName()); strBuffer.append("|"); strBuffer.append(scene.getSpaceId()); strBuffer.append("|"); strBuffer.append(scene.getSpaceName()); strBuffer.append("|"); strBuffer.append(scene.getResolution()); strBuffer.append("|"); strBuffer.append(scene.getResolutionOnFocus()); strBuffer.append("|"); strBuffer.append(scene.getSizeLimit()); strBuffer.append("|"); strBuffer.append(scene.getClickable()); strBuffer.append("\r\n"); byte[] outByte = null; try { outByte = strBuffer.toString().getBytes("UTF-8"); } catch (UnsupportedEncodingException e2) { e2.printStackTrace(); } byteBuffer.put(outByte); byteBuffer.flip(); try { outByteCh.write(byteBuffer); } catch (IOException e) { } byteBuffer.clear(); } fos.close(); String targetDir = resultDir; File sourceFile = new File(tempDir + fileName); int index = fileName.indexOf("-"); if (index != -1) { fileName = fileName.substring(index + 1, fileName.length()); } index = fileName.indexOf("_"); if (index != -1) { String directory = fileName.substring(0, index); targetDir += "/" + directory; } index = fileName.indexOf("."); if (index != -1) { fileName = fileName.substring(0, index) + ".csv"; } try { File resultTargetDir = new File(CiFileUtil.getReplaceFullPath(targetDir)); if (!resultTargetDir.isDirectory()) { CiFileUtil.createDirectory(targetDir); } CiFileUtil.renameFile(sourceFile, targetDir, fileName); } catch (Exception e) { log.error(e.getMessage()); } } catch (Exception e) { String errorMsg = e.getMessage(); log.error(errorMsg, e); throw new DataParsingException(errorMsg, e); //throw(e); } } public void parseRegionFile(File file) throws Exception { String line = ""; FileInputStream in = null; Reader isReader = null; LineNumberReader bufReader = null; FileOutputStream fos = null; String fileName = file.getName(); int index = fileName.indexOf("-"); if (index != -1) { fileName = fileName.substring(index + 1, fileName.length()); } String dir = filePolling.getValidFileDirectory(resultDir); String tempDir = dir + "/temp/"; File targetDirectory = new File(CiFileUtil.getReplaceFullPath(tempDir)); if (!targetDirectory.isDirectory()) { CiFileUtil.createDirectory(tempDir); } fos = new FileOutputStream(tempDir + fileName); int byteSize = 2048; ByteBuffer byteBuffer = ByteBuffer.allocateDirect(byteSize); GatheringByteChannel outByteCh = fos.getChannel(); try { in = new FileInputStream(file); isReader = new InputStreamReader(in, "UTF-16LE"); bufReader = new LineNumberReader(isReader); boolean first = true; while ((line = bufReader.readLine()) != null) { byte[] utf8 = line.getBytes("UTF-8"); String string = new String(utf8, "UTF-8"); String data[] = string.split("\t"); if (first == true) { first = false; if (data[0] == null || data[0].contains("region") == false) { throw new DataParsingException("data parsing error(not formatted)"); } continue; } if (data[0] == null || data[0].equals("")) { throw new DataParsingException("data parsing error(region id)"); } if (data[1] == null || data[1].equals("")) { throw new DataParsingException("data parsing error(region name)"); } if (data[2] == null || data[2].equals("")) { throw new DataParsingException("data parsing error(parent id)"); } StringBuffer strBuffer = new StringBuffer(); strBuffer.append(data[0]); strBuffer.append("\t"); strBuffer.append(data[1]); strBuffer.append("\t"); strBuffer.append(data[2]); strBuffer.append("\r\n"); byte[] outByte = null; try { outByte = strBuffer.toString().getBytes("UTF-8"); } catch (UnsupportedEncodingException e2) { e2.printStackTrace(); } byteBuffer.put(outByte); byteBuffer.flip(); try { outByteCh.write(byteBuffer); } catch (IOException e) { } byteBuffer.clear(); } fos.close(); index = fileName.indexOf("_"); String targetDir = resultDir; File sourceFile = new File(tempDir + fileName); if (index != -1) { String directory = fileName.substring(0, index); targetDir += "/" + directory; } try { File resultTargetDir = new File(CiFileUtil.getReplaceFullPath(targetDir)); if (!resultTargetDir.isDirectory()) { CiFileUtil.createDirectory(targetDir); } CiFileUtil.renameFile(sourceFile, targetDir, fileName); } catch (Exception e) { log.error(e.getMessage()); } } catch (Exception e) { String errorMsg = "Fail to parsing Line.[current line(" + bufReader.getLineNumber() + ") :" + line + "] : "; log.error(errorMsg, e); throw new DataParsingException(errorMsg, e); //throw(e); } finally { if (in != null) in.close(); if (isReader != null) isReader.close(); if (bufReader != null) bufReader.close(); } } }