List of usage examples for org.dom4j Element attributeValue
String attributeValue(QName qName);
From source file:ch.javasoft.xml.config.XmlConfig.java
License:BSD License
@SuppressWarnings("unchecked") protected List<Element> getReferredElementContent(String name, String path) throws XmlConfigException, MissingReferableException { Iterator<Element> it = getRootElement().elementIterator(XmlElement.referable.getXmlName()); while (it.hasNext()) { Element ref = it.next(); String refName = ref.attributeValue(XmlAttribute.name.getXmlName()); if (name.equals(refName)) { return ref.elements(); }/*from w w w . j ava 2 s . c o m*/ } throw new MissingReferableException(name, "cannot find referred element '" + name + "'", path); }
From source file:ch.javasoft.xml.config.XmlConfig.java
License:BSD License
@SuppressWarnings("unchecked") private void printUsageLines(PrintStream stream, Iterator<Element> usageChildIt) throws XmlConfigException { while (usageChildIt.hasNext()) { Element el = usageChildIt.next(); if (XmlUtil.isExpectedElementName(el, XmlElement.line)) { Element copy = el.createCopy(); List<Element> res = resolve(copy, XmlUtil.getElementPath(copy, true /*recurseParents*/)); for (Element r : res) { stream.println(r.attributeValue(XmlAttribute.value.getXmlName())); }//from w ww .j a v a 2 s. c o m } else if (XmlUtil.isExpectedElementName(el, XmlElement.usage)) { Element parentCopy = el.getParent().createCopy(); Element copy = el.createCopy(); parentCopy.add(copy); List<Element> res = resolve(copy, XmlUtil.getElementPath(copy, true /*recurseParents*/)); for (Element r : res) { printUsageLines(stream, r.elementIterator()); } } } }
From source file:ch.javasoft.xml.config.XmlUtil.java
License:BSD License
/** * Returns the child element of the given type which has the desired * attribute value in the given attribute. In xpath, this would be somewhat * like <code>/child[@attribute=$attributeValue]</code> * <p>/* w w w . j a v a 2 s . c om*/ * If multiple children match the criteria, an exception is thrown. If no * child matching the criteria is found, then either <code>null</code> is * returned (if <code>throwExceptionIfNull==false</code>), or an exception * is thrown (otherwise). * * @param element the parent element containing the desired * child element * @param child specifies the name of the child element * @param attribute specifies the name of the identifying attribute * @param attributeValue the desired attribute value * @param throwExceptionIfNull if <code>true</code>, an exception is thrown * if no child matches the criteria * @return the desired child element, or * <code>null</code> if * <code>throwExceptionIfNull==false</code> and * no child element matched the criteria * @throws XmlConfigException If multiple matches occur, or if no match * was found and exception throwing is desired * for this case (see <code>throwExceptionIfNull</code>) */ @SuppressWarnings("unchecked") public static Element getChildElementByAttributeValue(Element element, XmlNode child, XmlNode attribute, String attributeValue, boolean throwExceptionIfNull) throws XmlConfigException { Element res = null; Iterator<Element> it = element.elementIterator(child.getXmlName()); while (it.hasNext()) { Element el = it.next(); String attValue = el.attributeValue(attribute.getXmlName()); if (attributeValue.equals(attValue)) { if (res == null) res = el; else { throw new XmlConfigException("multiple matches for " + child.getXmlName() + " children with attribute " + attribute.getXmlName() + "='" + attributeValue + "'", element); } } } if (res == null && throwExceptionIfNull) { throw new XmlConfigException("missing " + child.getXmlName() + " child with attribute " + attribute.getXmlName() + "='" + attributeValue + "'", element); } return res; }
From source file:ch.javasoft.xml.config.XmlUtil.java
License:BSD License
/** * Check whether the given element has the desired value for a specific * attribute. Throws an exception otherwise. * //from w w w .j a v a 2s. c o m * @param element the element to check * @param attribute specifies the attribute name * @param expectedValue the desired attribute value * @throws XmlConfigException if the check fails */ public static void checkExpectedAttributeValue(Element element, XmlNode attribute, String expectedValue) throws XmlConfigException { String value = element.attributeValue(attribute.getXmlName()); if (!expectedValue.equals(value)) { throw new XmlConfigException("expected value '" + expectedValue + "' for attribute '" + attribute.getXmlName() + "', but found '" + value + "'", element); } }
From source file:ch.javasoft.xml.config.XmlUtil.java
License:BSD License
/** * Returns an xpath like string for the given xml element * /*from w w w . jav a 2 s. c o m*/ * @param elem the element to convert to a string * @param recurseParents true if parents should be included * * @return an xpath like string for the given element */ public static String getElementPath(Element elem, boolean recurseParents) { StringBuilder sb = new StringBuilder(elem.getName()); String name = elem.attributeValue(XmlAttribute.name.getXmlName()); if (name != null) { sb.append("[" + name + "]"); } if (recurseParents && !elem.isRootElement() && elem.getParent() != null) { sb.insert(0, '/'); sb.insert(0, getElementPath(elem.getParent(), recurseParents)); } return sb.toString(); }
From source file:ch.javasoft.xml.factory.XmlFactoryUtil.java
License:BSD License
/** * The factory class name is taken from the xml configuration using the * specified attribute name. The factory is instantiated and an object * created and returned, again using the xml configuration. * /* w w w . jav a 2 s . com*/ * @param <T> type of objects produced by the factory * @param clazz class of objects produced by the factory * @param config the element containing configuration * settings for the object to create * @param factoryClassAttribute name of the xml attribute containing the * class name of the factory * @throws ConfigException * @throws IllegalFactoryException * @throws FactoryNotFoundException * * @throws ConfigException if the factory class attribute was not * found in {@code config}, or if the * config content was not as expected * @throws FactoryNotFoundException if the factory class was not found * @throws IllegalFactoryException if the factory class was not a factory, * if the factory could not be instantiated, * if the config was not of the expected * type for this factory, or if the object * created by the factory was not * compatible with the type specified by * {@code clazz} */ public static <T, F extends XmlConfiguredFactory<T>> T create(Class<T> clazz, Element config, String factoryClassAttribute) throws FactoryNotFoundException, IllegalFactoryException, ConfigException { final String factoryClassName = config.attributeValue(factoryClassAttribute); if (factoryClassName == null) { throw new ConfigException("attribute with factory class not found: " + factoryClassAttribute, XmlUtil.getElementPath(config, true)); } return FactoryUtil.create(clazz, factoryClassName, config); }
From source file:cms.service.template.TemplateXml.java
private void parseRecord(Element record) { List<Element> fileds = record.elements(); dbobject = doc.getRootElement().getName(); fields = new String[fileds.size()]; datatype = new String[fileds.size()]; value = new String[fileds.size()]; index = new int[fileds.size()]; //set query// www . j a v a 2 s . c o m insertquery = "\tinsert into table_" + dbobject + "( " + (isparent ? "" : relfield + ","); insertvalue = ")values(" + (isparent ? "" : "'0',"); updatequery = "\tupdate table_" + dbobject + " set "; colcount = 0; for (Element field : fileds) { String elmXml = field.asXML(); String fname = field.getName(); String fvalue = field.getText(); fields[colcount] = field.getName(); datatype[colcount] = field.attributeValue("type"); index[colcount] = colcount; setValue(fvalue, field.getName(), field.attributeValue("type"), field.attributeValue("isRequired")); } makeSql(); }
From source file:cn.buk.api.service.CtripHotelServiceImpl.java
License:LGPL
/** * ??/* w ww.jav a2 s .c o m*/ * @param cityId ? * @return ?? */ @Override public String searchHotel(int cityId) { if (cityId <= 0) return "ER#CITYID IS " + cityId; //headerAPI? Cache cache = getCache(); String cacheKey = ConfigData.OTA_HotelSearch_Request; net.sf.ehcache.Element cacheElement = cache.get(cacheKey); if (cacheElement != null) { Element headerElement = (Element) cacheElement.getValue(); int accessCount = Integer.parseInt(headerElement.attributeValue("AccessCount")); int currentCount = Integer.parseInt(headerElement.attributeValue("CurrentCount")) + 1; logger.info("AccessCount=" + headerElement.attributeValue("AccessCount") + ", CurrentCount=" + headerElement.attributeValue("CurrentCount") + ", ResetTime=" + headerElement.attributeValue("ResetTime")); if (currentCount >= accessCount) { try { logger.info("Sleep for one minute."); Thread.sleep(60000); } catch (InterruptedException ex) { logger.warn(Thread.currentThread().getName() + " is interrupted."); return "ER#Thread.sleep is interrupted."; } } } HotelRequestBody request = new HotelRequestBody(); request.createHotelRequestRQ(); request.getHotelRequestRQ().getCriteria().getCriterion().getHotelRef().setHotelCityCode(cityId); String xml; try { JAXBContext jc = JAXBContext.newInstance(HotelRequestBody.class); Marshaller m = jc.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); m.setProperty(Marshaller.JAXB_ENCODING, "utf-8"); m.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE); DocumentResult documentResult = new DocumentResult(); m.marshal(request, documentResult); org.dom4j.Document document = documentResult.getDocument(); org.dom4j.Element requestElement = document.getRootElement(); Element ele = requestElement.element("OTA_HotelSearchRQ"); ele.addNamespace("", "http://www.opentravel.org/OTA/2003/05"); org.dom4j.Document doc1 = DocumentHelper.createDocument(); org.dom4j.Element rootElement = createRequestHeaderElement(doc1, ConfigData.OTA_HotelSearch_Request); rootElement.add(requestElement); xml = doc1.asXML(); } catch (JAXBException ex) { logger.error(ex.getMessage()); return "ER"; } if (serviceStopped) return "ER#Service stopped."; String response = execApiRequest(xml, ConfigData.OTA_HotelSearch_Url, "requestXML"); //? SAXReader reader = new SAXReader(); Document document;// ?XML try { document = reader.read(new StringReader(response)); if (serviceStopped) return "ER#Service stopped."; response = processHotelSearchResponse(document); } catch (Exception ex) { logger.error(ex.getMessage()); return "ER"; } return response; }
From source file:cn.buk.api.service.CtripHotelServiceImpl.java
License:LGPL
@Override public synchronized String searchHotelDetail(List<String> hotelCodes, boolean returnXml) { if (hotelCodes == null || hotelCodes.size() == 0) return "ER#hotelcodes is null"; //headerAPI?/*w w w . j a va 2 s . c o m*/ net.sf.ehcache.Element cacheElement = getCache().get(ConfigData.OTA_HotelDetail_Request); if (cacheElement != null) { Element headerElement = (Element) cacheElement.getValue(); int accessCount = Integer.parseInt(headerElement.attributeValue("AccessCount")); int currentCount = Integer.parseInt(headerElement.attributeValue("CurrentCount")) + 1; logger.info(ConfigData.OTA_HotelDetail_Request + " AccessCount=" + headerElement.attributeValue("AccessCount") + ", CurrentCount=" + headerElement.attributeValue("CurrentCount") + ", ResetTime=" + headerElement.attributeValue("ResetTime")); if (currentCount >= accessCount) { try { logger.info("Sleep for one minute."); Thread.sleep(60000); } catch (InterruptedException ex) { logger.warn(Thread.currentThread().getName() + " is interrupted."); return "ER#SearchHotelDetail thread.sleep is interrupted."; } } } if (this.serviceStopped) return "ER#Service stopped."; HotelRequestBody request = new HotelRequestBody(); request.createHotelDetailRequest(); for (String hotelCode : hotelCodes) { if (this.serviceStopped) return "ER#Service stopped."; HotelDescriptiveInfo hotelDescriptiveInfo = new HotelDescriptiveInfo(); hotelDescriptiveInfo.setHotelCode(hotelCode); cn.buk.hotel.entity.HotelInfo hotelInfo = hotelDao.getHotelDetailInfoByHotelCode(hotelCode); if (hotelInfo != null) { //if (hotelInfo.getGuestRooms().size() == 0) { hotelDescriptiveInfo.setHotelInfoFacilityInfo(new HotelInfoFacilityInfo()); hotelDescriptiveInfo.getHotelInfoFacilityInfo().setSendGuestRooms(true); //} if (hotelInfo.getRefPoints().size() == 0) { hotelDescriptiveInfo.setHotelInfoAreaInfo(new HotelInfoAreaInfo()); hotelDescriptiveInfo.getHotelInfoAreaInfo().setSendAttractions(true); hotelDescriptiveInfo.getHotelInfoAreaInfo().setSendRecreations(true); } if (hotelInfo.getMedias().size() == 0) { hotelDescriptiveInfo.setHotelInfoMultimedia(new HotelInfoMultimedia()); hotelDescriptiveInfo.getHotelInfoMultimedia().setSendData(true); } } request.getHotelDetailRequest().getHotelDescriptiveInfos().add(hotelDescriptiveInfo); } String requestXml; try { JAXBContext jc = JAXBContext.newInstance(HotelRequestBody.class); Marshaller m = jc.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); m.setProperty(Marshaller.JAXB_ENCODING, "utf-8"); m.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE); DocumentResult documentResult = new DocumentResult(); m.marshal(request, documentResult); org.dom4j.Document document = documentResult.getDocument(); org.dom4j.Element requestElement = document.getRootElement(); Element ele = requestElement.element("OTA_HotelDescriptiveInfoRQ"); ele.addNamespace("", "http://www.opentravel.org/OTA/2003/05"); org.dom4j.Document doc1 = DocumentHelper.createDocument(); if (this.serviceStopped) return "ER#Service stopped."; org.dom4j.Element rootElement = createRequestHeaderElement(doc1, ConfigData.OTA_HotelDetail_Request); rootElement.add(requestElement); requestXml = doc1.asXML(); } catch (JAXBException e) { e.printStackTrace(); return "ER#OTA_exception"; } Date date0 = DateUtil.getCurDateTime(); logger.debug(ConfigData.OTA_HotelDetail_Request + ": begin"); logger.debug(requestXml); if (this.serviceStopped) return "ER#Service stopped."; String response = execApiRequest(requestXml, ConfigData.OTA_HotelDetail_Url, "requestXML"); logger.debug(response); int apiElapsedTime = DateUtil.getPastTime(date0); logger.debug(ConfigData.OTA_HotelDetail_Request + ": end, " + apiElapsedTime + "ms"); if (returnXml) return response; //? String rs; SAXReader reader = new SAXReader(); Document document;// ?XML try { document = reader.read(new StringReader(response)); Element headerElement = document.getRootElement().element("Header"); //header? getCache().put(new net.sf.ehcache.Element(ConfigData.OTA_HotelDetail_Request, headerElement)); if (!headerElement.attribute("ResultCode").getValue().equalsIgnoreCase("Success")) { logger.error(requestXml); logger.error(document.asXML()); return "ER#ResultCode is not Success."; } DocumentDto documentDto = new DocumentDto(); documentDto.setDocument(document); if (hotelDetailQueue.size() == MAX_HOTEL_DETAIL_QUEUE) { logger.warn("hotelDetailQueue is full, thread will be blocked here."); } hotelDetailQueue.put(documentDto); hotelDetailDaoExecutor.execute(new HotelDetailDaoThread()); rs = "OK#save to the queue."; } catch (InterruptedException ex) { return "ER#Interrupt occured."; } catch (Exception ex) { logger.error(ex.getMessage()); return "ER#searchHotelDetail"; } return rs; }
From source file:cn.buk.api.service.CtripHotelServiceImpl.java
License:LGPL
private synchronized String doSearchHotelRatePlan(String requestXml) { logger.debug("doSearchHotelRatePlan begin..." + Thread.currentThread().getName()); //headerAPI?/*from w ww . jav a 2 s . c o m*/ String cacheKey = ConfigData.OTA_HotelRatePlan_Request; net.sf.ehcache.Element cacheElement = getCache().get(cacheKey); if (cacheElement != null) { Element headerElement = (Element) cacheElement.getValue(); int accessCount = Integer.parseInt(headerElement.attributeValue("AccessCount")); int currentCount = Integer.parseInt(headerElement.attributeValue("CurrentCount")) + 1; logger.info(ConfigData.OTA_HotelRatePlan_Request + " AccessCount=" + headerElement.attributeValue("AccessCount") + ", CurrentCount=" + headerElement.attributeValue("CurrentCount") + ", ResetTime=" + headerElement.attributeValue("ResetTime")); if (currentCount >= accessCount) { try { logger.info("Sleep for one minute."); Thread.sleep(60000); } catch (InterruptedException ex) { logger.warn(Thread.currentThread().getName() + " is interrupted."); return "ER#DoSearchHotelRatePlan thread.sleep is interrupted."; } } } if (this.serviceStopped) return "ER#Service is stopped."; Date date0 = DateUtil.getCurDateTime(); String response = execApiRequest(requestXml, ConfigData.OTA_HotelRatePlan_Url, "requestXML"); int apiElapsedTime = DateUtil.getPastTime(date0); if (apiElapsedTime > 1000) logger.debug(ConfigData.OTA_HotelRatePlan_Request + " API: " + apiElapsedTime + "ms"); //? try { SAXReader reader = new SAXReader(); Document document = reader.read(new StringReader(response)); Element rootElement = document.getRootElement(); Element headerElement = rootElement.element("Header"); if (headerElement.attribute("ResultCode").getValue().equalsIgnoreCase("Success")) { //header? logger.debug("put headerElement to cache by key " + ConfigData.OTA_HotelRatePlan_Request); getCache().put(new net.sf.ehcache.Element(ConfigData.OTA_HotelRatePlan_Request, headerElement)); } else { logger.error(response); logger.error("ResultCode is not Success."); } } catch (Exception e) { logger.error(e.getMessage()); } logger.debug("doSearchHotelRatePlan end..." + Thread.currentThread().getName()); return response; }