List of usage examples for org.dom4j.io SAXReader read
public Document read(InputSource in) throws DocumentException
Reads a Document from the given InputSource
using SAX
From source file:com.appdynamics.monitors.hbase.HBaseMonitor.java
License:Apache License
private void getCredentials(final Map<String, String> args) { credentials = new ArrayList<Credential>(); Credential cred = new Credential(); cred.dbname = args.get("dbname"); cred.host = args.get("host"); cred.port = args.get("port"); cred.username = args.get("user"); cred.password = args.get("pass"); if (!isNotEmpty(cred.dbname)) { cred.dbname = "DB 1"; }/*from w ww . j a v a 2 s .co m*/ credentials.add(cred); String xmlPath = args.get("properties-path"); if (isNotEmpty(xmlPath)) { try { SAXReader reader = new SAXReader(); Document doc = reader.read(xmlPath); Element root = doc.getRootElement(); for (Element credElem : (List<Element>) root.elements("credentials")) { cred = new Credential(); cred.dbname = credElem.elementText("dbname"); cred.host = credElem.elementText("host"); cred.port = credElem.elementText("port"); cred.username = credElem.elementText("user"); cred.password = credElem.elementText("pass"); if (isNotEmpty(cred.host) && isNotEmpty(cred.port)) { if (!isNotEmpty(cred.dbname)) { cred.dbname = "DB " + (credentials.size() + 1); } credentials.add(cred); } } } catch (DocumentException e) { logger.error("Cannot read '" + xmlPath + "'. Monitor is running without additional credentials"); } } }
From source file:com.appdynamics.snmp.SNMPTrapSender.java
License:Apache License
/** * Parses the config xml//from w w w . ja v a2s .c o m * @param xml Configuration file locations * @return Map<String, String> - Map of config objects * @throws DocumentException */ private static Map<String, String> parseXML(String xml) throws DocumentException { Map<String, String> map = new HashMap<String, String>(); SAXReader reader = new SAXReader(); Document document = reader.read(xml); Element root = document.getRootElement(); for (Iterator<Element> i = root.elementIterator(); i.hasNext();) { Element element = (Element) i.next(); if (element.getName().equals("snmp-v3")) { Iterator<Element> elementIterator = element.elementIterator(); for (Iterator<Element> j = elementIterator; j.hasNext();) { element = (Element) j.next(); map.put(element.getName(), element.getText()); } } else { map.put(element.getName(), element.getText()); } } return map; }
From source file:com.appeligo.ccdataindexer.Indexer.java
License:Apache License
public boolean indexProgram(File programFile, Network network) throws IOException { log.debug("processing file " + programFile + " for " + network.getStationName()); boolean needToClose = openIndex(); StringBuilder captions = new StringBuilder(); InputStream is = null;//from www.j ava 2 s . c o m try { is = new GZIPInputStream(new BufferedInputStream(new FileInputStream(programFile))); SAXReader reader = new SAXReader(); reader.setEntityResolver(new ExternalResolver()); Document document = null; try { document = reader.read(is); } catch (DocumentException e) { log.warn("Could not open document " + programFile + "; ", e); return false; } //Node startTimeNode = document.selectSingleNode("//meta[@name='StartTime']"); Node startTimeNode = document.selectSingleNode("//*[name()='meta'][@name='StartTime']"); long startTime; try { startTime = Long.parseLong(startTimeNode.valueOf("@content")); } catch (NumberFormatException e) { log.warn("Error parsing StartTime " + startTimeNode + "; ", e); return false; } //Node programNode = document.selectSingleNode("//meta[@name='ProgramID']"); Node programNode = document.selectSingleNode("//*[name()='meta'][@name='ProgramID']"); String programId = programNode.valueOf("@content"); programId = updateProgramId(programId); //Node endTimeNode = document.selectSingleNode("//meta[@name='EndTime']"); Node endTimeNode = document.selectSingleNode("//*[name()='meta'][@name='EndTime']"); long endTime; try { endTime = Long.parseLong(endTimeNode.valueOf("@content")); } catch (NumberFormatException e) { log.warn("Error parsing endTime " + endTimeNode + "; ", e); return false; } //List divs = document.selectNodes("/html/body/div"); List divs = document.selectNodes("/*[name()='html']/*[name()='body']/*[name()='div']"); while (divs.size() > 0) { Element div = (Element) divs.remove(0); List children = div.selectNodes("child::node()"); while (children.size() > 0) { Node a = (Node) children.remove(0); while (!("a".equals(a.getName()))) { if (children.size() == 0) { break; } a = (Node) children.remove(0); } if (!("a".equals(a.getName()))) { break; } Node afterA = (Node) children.remove(0); if (afterA instanceof Element) { if (!("span".equals(afterA.getName()))) { throw new IOException("span expected... bad data in " + programFile); } //Don't include this in the captions or should we? //Element span = (Element)afterA; //captions.append(' '); //captions.append(span.getText().replace(">>", "").trim()); afterA = (Node) children.remove(0); } StringBuilder sentence = new StringBuilder(); if (afterA instanceof Text) { Text sentenceNode = (Text) afterA; sentence.append(sentenceNode.getText()); } else { Entity entity = (Entity) afterA; sentence.append(entity.asXML()); } /* while (children.get(0) instanceof Text) { Text moreText = (Text)children.remove(0); captions.append(' '); captions.append(DocumentUtil.prettySentence(moreText.getText())); } */ while (children.get(0) instanceof Text || children.get(0) instanceof Entity) { if (children.get(0) instanceof Text) { Text moreText = (Text) children.remove(0); sentence.append(moreText.getText()); } else { Entity entity = (Entity) children.remove(0); sentence.append(entity.asXML()); } } captions.append(DocumentUtil.prettySentence(sentence.toString().trim())); captions.append(' '); } } ArrayList<ScheduledProgram> skedulePrograms = new ArrayList<ScheduledProgram>(); long lookupTimeStart = System.currentTimeMillis(); for (String lineupId : networkLineups) { log.debug("looking for future scheduled program: " + programId + " on lineup " + lineupId); ScheduledProgram skedProg = epg.getNextShowing(lineupId, programId, false, true); if (skedProg == null) { log.debug("looking for past scheduled program: " + programId + " on lineup " + lineupId); skedProg = epg.getLastShowing(lineupId, programId); } log.debug("Sked prog for program: " + programId + " is null " + (skedProg == null)); //We Can we fake a scheduled program? //Program program = epg.getProgram(programId); if (skedProg == null) { log.debug("Unable to locate ScheduleProgram for " + programId); Program program = epg.getProgram(programId); if (program != null) { skedProg = new ScheduledProgram(); skedProg.setNetwork(network); skedProg.setDescription(program.getDescription()); skedProg.setDescriptionWithActors(program.getDescriptionWithActors()); skedProg.setEndTime(new Date(endTime)); skedProg.setStartTime(new Date(startTime)); skedProg.setCredits(program.getCredits()); skedProg.setGenreDescription(program.getGenreDescription()); skedProg.setProgramId(program.getProgramId()); skedProg.setProgramTitle(program.getProgramTitle()); skedProg.setEpisodeTitle(program.getEpisodeTitle()); skedProg.setLastModified(program.getLastModified()); skedProg.setOriginalAirDate(program.getOriginalAirDate()); skedProg.setRunTime(program.getRunTime()); skedProg.setScheduleId(0); skedProg.setTvRating(program.getTvRating()); skedProg.setNewEpisode(false); skedProg.setLineupId(lineupId); } } if (skedProg != null) { skedulePrograms.add(skedProg); if (indexWriter == null) { // probably only doing composite index, so one is all we need break; } } } lookupTime += (System.currentTimeMillis() - lookupTimeStart); if (captions.length() > 250 && skedulePrograms.size() > 0) { //Delete any old duplicates Term term = new Term("programID", skedulePrograms.get(0).getProgramId()); if (indexWriter != null) { indexWriter.deleteDocuments(term); //Now insert the new document. org.apache.lucene.document.Document doc = new org.apache.lucene.document.Document(); DocumentUtil.addCaptions(doc, captions.toString()); DocumentUtil.populateDocument(doc, skedulePrograms, new Date()); indexWriter.addDocument(doc); } if (compositeIndexWriter != null) { compositeIndexWriter.deleteDocuments(term); org.apache.lucene.document.Document compositeDoc = new org.apache.lucene.document.Document(); DocumentUtil.populateCompositeDocument(compositeDoc, captions.toString().replaceAll("[.!?]* ", " "), skedulePrograms); compositeIndexWriter.addDocument(compositeDoc); } log.debug("Adding to index now:" + skedulePrograms.get(0).getProgramId() + " " + skedulePrograms.get(0).getProgramTitle()); return true; } else { log.debug("Limited CC data(" + captions.length() + " character) or unable to locate EPG date for the program. Not adding this program to the index:" + programId); return false; } } finally { if (is != null) { try { is.close(); } catch (IOException e) { log.error("Error closing file", e); } } if (needToClose) { closeIndex(); } } }
From source file:com.appeligo.channelfeed.work.FileReaderThread.java
License:Apache License
public void run() { try {/* w ww.ja v a 2s. c o m*/ SAXReader reader = new SAXReader(); Document document; try { document = reader.read(new File(ccDocumentRoot + captionFilename)); } catch (DocumentException e) { log.error("Could not open document " + ccDocumentRoot + captionFilename + "; ", e); return; } do { //Node startTimeNode = document.selectSingleNode("//meta[@name='StartTime']"); Node startTimeNode = document.selectSingleNode("//*[name()='meta'][@name='StartTime']"); long startTime; try { startTime = Long.parseLong(startTimeNode.valueOf("@content")); } catch (NumberFormatException e) { throw new Error(e); } //Node endTimeNode = document.selectSingleNode("//meta[@name='EndTime']"); Node endTimeNode = document.selectSingleNode("//*[name()='meta'][@name='EndTime']"); long endTime; try { endTime = Long.parseLong(endTimeNode.valueOf("@content")); } catch (NumberFormatException e) { throw new Error(e); } long durationMillis = endTime - startTime; if (autoAdvance) { long durationSeconds = durationMillis / 1000; Calendar midnight = Calendar.getInstance(); midnight.setTime(new Date()); midnight.setTimeZone(TimeZone.getTimeZone("GMT")); int year = midnight.get(Calendar.YEAR); int month = midnight.get(Calendar.MONTH); int dayOfMonth = midnight.get(Calendar.DAY_OF_MONTH); midnight.clear(); midnight.set(year, month, dayOfMonth); long midnightMillis = midnight.getTimeInMillis(); long secondsSinceMidnight = (System.currentTimeMillis() - midnightMillis) / 1000; //int loopsSinceMidnight = (int)(secondsSinceMidnight / durationSeconds); advanceSeconds = (int) (secondsSinceMidnight % durationSeconds); } long newStartTime = System.currentTimeMillis() - (advanceSeconds * 1000); long newEndTime = newStartTime + durationMillis; setProgram(newStartTime).setStartTime(new Date(newStartTime)); // List divs = document.selectNodes("/html/body/div"); List divs = document.selectNodes("/*[name()='html']/*[name()='body']/*[name()='div']"); while ((divs.size() > 0) && (!aborted)) { Element div = (Element) divs.remove(0); List children = div.selectNodes("child::node()"); while ((children.size() > 0) && (!aborted)) { Node a = (Node) children.remove(0); while (!("a".equals(a.getName()))) { if (children.size() == 0) { break; } a = (Node) children.remove(0); } if (!("a".equals(a.getName()))) { break; } long timestamp; try { timestamp = Long.parseLong(a.valueOf("@name")); } catch (NumberFormatException e) { throw new Error(e); } long offset = timestamp - startTime; long delayUntil = newStartTime + offset; log.debug("Next sentence in " + ((delayUntil - System.currentTimeMillis()) / 1000) + " seconds"); while (System.currentTimeMillis() < delayUntil) { try { Thread.sleep(delayUntil - System.currentTimeMillis()); } catch (InterruptedException e) { } } String speakerChange = null; Node afterA = (Node) children.remove(0); if (afterA instanceof Element) { if (!("span".equals(afterA.getName()))) { throw new Error("span expected"); } Element span = (Element) afterA; speakerChange = span.getText().replace(">>", "").trim(); afterA = (Node) children.remove(0); } String sentence; if (afterA instanceof Text) { Text sentenceNode = (Text) afterA; sentence = sentenceNode.getText(); } else { Entity entity = (Entity) afterA; sentence = entity.asXML(); } while (children.get(0) instanceof Text || children.get(0) instanceof Entity) { if (children.get(0) instanceof Text) { Text moreText = (Text) children.remove(0); sentence += moreText.getText(); } else { Entity entity = (Entity) children.remove(0); sentence += entity.asXML(); } } sentence = sentence.trim(); /* if (!docOpened) { openDoc(newStartTime - SCHEDULE_VARIANCE); } */ //if (program != null) { getDestinations().writeSentence(getProgram(), delayUntil, speakerChange, sentence); //} } } if (loop) { autoAdvance = false; advanceSeconds = 0; log.debug("Waiting for end of program in " + ((newEndTime - System.currentTimeMillis()) / 1000) + " seconds"); while (System.currentTimeMillis() < newEndTime) { try { Thread.sleep(newEndTime - System.currentTimeMillis()); } catch (InterruptedException e) { } } } } while (loop); } catch (Exception e) { log.error("Uncaught exception!", e); } }
From source file:com.appleframework.pay.app.reconciliation.parser.ALIPAYParser.java
License:Apache License
/** * ????//from w w w. j a v a 2 s. c om * * @param file * ?? * @param billDate * ? * @param batch * * @return */ @SuppressWarnings({ "rawtypes", "unchecked" }) public List<ReconciliationEntityVo> parser(File file, Date billDate, RpAccountCheckBatch batch) throws IOException { SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_STYLE); // xml?file SAXReader reader = new SAXReader(); Document document; try { document = reader.read(file); // dom4jXpathAccountQueryAccountLogVO List projects = document.selectNodes( "alipay/response/account_page_query_result/account_log_list/AccountQueryAccountLogVO"); Iterator it = projects.iterator(); // ? List<AlipayAccountLogVO> tradeList = new ArrayList<AlipayAccountLogVO>(); // ? List<AlipayAccountLogVO> otherAll = new ArrayList<AlipayAccountLogVO>(); while (it.hasNext()) { AlipayAccountLogVO vo = new AlipayAccountLogVO(); Element elm = (Element) it.next(); List<Element> childElements = elm.elements(); for (Element child : childElements) { String name = child.getName(); // switch (name) { case "balance": vo.setBalance(new BigDecimal(child.getText())); break; case "rate": vo.setBankRate(new BigDecimal(("").equals(child.getText()) ? "0" : child.getText())); break; case "buyer_account": vo.setBuyerAccount(child.getText()); break; case "goods_title": vo.setGoodsTitle(child.getText()); break; case "income": vo.setIncome(new BigDecimal(("").equals(child.getText()) ? "0" : child.getText())); break; case "outcome": vo.setOutcome(new BigDecimal(("").equals(child.getText()) ? "0" : child.getText())); break; case "merchant_out_order_no": vo.setMerchantOrderNo(child.getText()); break; case "total_fee": vo.setTotalFee(new BigDecimal(("").equals(child.getText()) ? "0" : child.getText())); break; case "trade_no":// ? vo.setTradeNo(child.getText()); break; case "trans_code_msg":// vo.setTransType(child.getText()); break; case "trans_date": String dateStr = child.getText(); Date date; try { date = sdf.parse(dateStr); } catch (ParseException e) { date = billDate; } vo.setTransDate(date); break; default: break; } } // ?? if ("".equals(vo.getTransType())) { tradeList.add(vo); } else { otherAll.add(vo); } } // ????????? // ????????? for (AlipayAccountLogVO trade : tradeList) { String tradeNo = trade.getTradeNo(); for (AlipayAccountLogVO other : otherAll) { String otherTradeNo = other.getTradeNo(); if (tradeNo.equals(otherTradeNo)) { trade.setBankFee(other.getOutcome()); } } } // AlipayAccountLogVOvoReconciliationEntityVo?list List<ReconciliationEntityVo> list = new ArrayList<ReconciliationEntityVo>(); // ??? int totalCount = 0; BigDecimal totalAmount = BigDecimal.ZERO; BigDecimal totalFee = BigDecimal.ZERO; for (AlipayAccountLogVO trade : tradeList) { // totalCount++; totalAmount = totalAmount.add(trade.getTotalFee()); totalFee = totalFee.add(trade.getBankFee()); // AlipayAccountLogVOReconciliationEntityVo ReconciliationEntityVo vo = new ReconciliationEntityVo(); vo.setAccountCheckBatchNo(batch.getBatchNo()); vo.setBankAmount(trade.getTotalFee()); vo.setBankFee(trade.getBankFee()); vo.setBankOrderNo(trade.getMerchantOrderNo()); vo.setBankRefundAmount(BigDecimal.ZERO); vo.setBankTradeStatus("SUCCESS"); vo.setBankTradeTime(trade.getTransDate()); vo.setBankTrxNo(trade.getTradeNo()); vo.setBankType(PayWayEnum.ALIPAY.name()); vo.setOrderTime(trade.getTransDate()); list.add(vo); } batch.setBankTradeCount(totalCount); batch.setBankTradeAmount(totalAmount); batch.setBankRefundAmount(BigDecimal.ZERO); batch.setBankFee(totalFee); return list; } catch (DocumentException e) { LOG.warn("?", e); batch.setStatus(BatchStatusEnum.FAIL.name()); batch.setCheckFailMsg("?, payway[" + PayWayEnum.ALIPAY.name() + "], billdata[" + sdf.format(billDate) + "]"); return null; } }
From source file:com.appleframework.pay.trade.utils.WeiXinPayUtils.java
License:Apache License
/** * ??xml?,?/*from w ww. j a va 2 s. co m*/ * @param requestUrl * @param requestMethod * @param xmlStr * @return */ public static Map<String, Object> httpXmlRequest(String requestUrl, String requestMethod, String xmlStr) { // ?HashMap Map<String, Object> map = new HashMap<String, Object>(); try { HttpsURLConnection urlCon = (HttpsURLConnection) (new URL(requestUrl)).openConnection(); urlCon.setDoInput(true); urlCon.setDoOutput(true); // ?GET/POST urlCon.setRequestMethod(requestMethod); if ("GET".equalsIgnoreCase(requestMethod)) { urlCon.connect(); } urlCon.setRequestProperty("Content-Length", String.valueOf(xmlStr.getBytes().length)); urlCon.setUseCaches(false); // gbk????? if (null != xmlStr) { OutputStream outputStream = urlCon.getOutputStream(); outputStream.write(xmlStr.getBytes("UTF-8")); outputStream.flush(); outputStream.close(); } InputStream inputStream = urlCon.getInputStream(); InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8"); // ?? SAXReader reader = new SAXReader(); Document document = reader.read(inputStreamReader); // xml Element root = document.getRootElement(); // ? @SuppressWarnings("unchecked") List<Element> elementList = root.elements(); // ??? for (Element e : elementList) { map.put(e.getName(), e.getText()); } inputStreamReader.close(); inputStream.close(); inputStream = null; urlCon.disconnect(); } catch (MalformedURLException e) { LOG.error(e.getMessage()); } catch (IOException e) { LOG.error(e.getMessage()); } catch (Exception e) { LOG.error(e.getMessage()); } return map; }
From source file:com.appleframework.pay.trade.utils.WeiXinPayUtils.java
License:Apache License
/** * ???XML// www. jav a 2s. co m * * @param inputStream * @return * @throws Exception */ @SuppressWarnings("unchecked") public static Map<String, String> parseXml(InputStream inputStream) throws Exception { if (inputStream == null) { return null; } Map<String, String> map = new HashMap<String, String>();// ?HashMap SAXReader reader = new SAXReader();// ?? Document document = reader.read(inputStream); Element root = document.getRootElement();// xml List<Element> elementList = root.elements();// ? for (Element e : elementList) { // ??? map.put(e.getName(), e.getText()); } inputStream.close(); // ? inputStream = null; return map; }
From source file:com.apporiented.hermesftp.usermanager.impl.XmlFileReader.java
License:Open Source License
/** * Reads the user management data from a file. If the file was not found the classpath is * searched.// w w w .j ava2s .co m * * @return The user management data. * @throws FtpConfigException Error on reading or processing a configuration file. */ public UserManagerData read() throws FtpConfigException { UserManagerData result; File file = null; try { SAXReader reader = new SAXReader(); file = new File(getFilename()); BufferedReader br; if (file.exists()) { br = new BufferedReader(new FileReader(file)); } else { InputStream is = getClass().getResourceAsStream("/" + DEFAULT_HERMESFTP_USERS_FILE); br = new BufferedReader(new InputStreamReader(is)); } Document doc = reader.read(br); result = process(doc); } catch (IOException e) { throw new FtpConfigException("Reading " + getFilename() + " failed."); } catch (DocumentException e) { throw new FtpConfigException("Error while processing the configuration file " + file + "."); } return result; }
From source file:com.arc.cdt.debug.seecode.core.launch.CMPDInfoFromVDKConfigReader.java
License:Open Source License
/** * Given a VDK configuration file, extract CMPD information suitable for creating a launch configuration. * @param vdkConfig the XML file to read from. * @return cmpd description/* w w w .j a v a 2 s . com*/ * @throws VDKConfigException if an error occurs in reading the config file. */ @SuppressWarnings("unchecked") public static ICMPDInfo extractCMPDInfo(File vdkConfig, IProject project) throws VDKConfigException { try { SAXReader reader = new SAXReader(); Document doc = reader.read(vdkConfig); Element root = doc.getRootElement(); if (root == null || !root.getName().equalsIgnoreCase("CMPD")) { throw new DocumentException("Root element is not \"CMPD\" node"); } if (!"1".equals(root.attributeValue("version"))) { throw new DocumentException("VDK config file has unknown version: " + root.attribute("version")); } List<Element> processes = root.elements("PROCESS"); final List<ICMPDInfo.IProcess> pList = new ArrayList<ICMPDInfo.IProcess>(processes.size()); File workingDir = vdkConfig.getParentFile(); for (Element p : processes) { pList.add(formProcess(p, workingDir, project)); } List<Element> launches = root.elements("LAUNCH"); // should be just one final List<String> launchSwitches = new ArrayList<String>(); final List<String> startupCommands = new ArrayList<String>(); if (launches != null) { for (Element e : launches) { appendLaunchSwitches(launchSwitches, startupCommands, e); } } return new ICMPDInfo() { @Override public String[] getLaunchArgs() { return launchSwitches.toArray(new String[launchSwitches.size()]); } @Override public IProcess[] getProcesses() { return pList.toArray(new IProcess[pList.size()]); } @Override public String[] getStartupCommands() { return startupCommands.toArray(new String[startupCommands.size()]); } }; } catch (MalformedURLException e) { throw new VDKConfigException(e.getMessage(), e); } catch (DocumentException e) { throw new VDKConfigException(e.getMessage(), e); } }
From source file:com.arc.cdt.debug.seecode.options.SeeCodeOptions.java
License:Open Source License
/** * Read the XML file that maps compiler options to equivalent seecode * arguments that will be passed to the SeeCode swahili processor ("scac"). * For example, ARC's "-Xswap", "-arc700", etc. * <P>/*from w w w . j a va 2 s. c om*/ * If the target does not require any compiler options to be reflected, then * this method returns null. * <P> * The name of the XML file is derived from the target name. * * @param target * the name of the target (e.g., "ac", "vc", etc.) * @param project * applicable project. * @return the option mapping for computing seecode arguments, or * <code>null</code> if there isn't any such mapping. */ public static ISeeCodeOptionsAugmenter readOptionMapping(String target, IProject project) throws ConfigurationException { // Use cached copy if we've already loaded it. try { Element root = null; // If already parsed, remember it. if (target.equals(sCachedTarget)) root = sCached; else { String xml = target.toLowerCase() + "_options.xml"; InputStream input = SeeCodeOptions.class.getResourceAsStream(xml); if (input != null) { SAXReader reader = new SAXReader(); Document doc = reader.read(input); root = doc.getRootElement(); sCached = root; sCachedTarget = target; } } if (root != null) { IConfiguration buildConfiguration = getBuildConfiguration(project); if (buildConfiguration != null && isConsistentWithTarget(buildConfiguration, target.toLowerCase())) { return new SeeCodeOptionsAugmenter(root, buildConfiguration); } } } catch (DocumentException e) { throw new ConfigurationException(e.getMessage(), e); } return null; }