List of usage examples for org.dom4j.io DocumentResult getDocument
public Document getDocument()
From source file:bookmarks.BookmarkXML.java
License:Open Source License
/** * /*www .j a va2 s . c o m*/ * @param xslFileName * */ public void styleDocument(String xslFileName, String prefLang) { try { TransformerFactory factory = TransformerFactory.newInstance(); if (prefLang.equals("eng")) { Transformer transformer = factory.newTransformer(new StreamSource( "file:///" + PropertyManager.CATALINA_BASE + PropertyManager.XSLPATHENG + xslFileName)); String test2 = PropertyManager.CATALINA_BASE + PropertyManager.XSLPATHENG + xslFileName; transformer.setOutputProperty(OutputKeys.ENCODING, Utilities.REQUEST_ENCODING); DocumentSource source = new DocumentSource(xmlDoc); DocumentResult result = new DocumentResult(); transformer.transform(source, result); xmlDoc = result.getDocument(); } else { String test2 = PropertyManager.CATALINA_BASE + PropertyManager.XSLPATHGER + xslFileName; Transformer transformer = factory.newTransformer(new StreamSource( "file:///" + PropertyManager.CATALINA_BASE + PropertyManager.XSLPATHGER + xslFileName)); transformer.setOutputProperty(OutputKeys.ENCODING, Utilities.REQUEST_ENCODING); DocumentSource source = new DocumentSource(xmlDoc); DocumentResult result = new DocumentResult(); transformer.transform(source, result); xmlDoc = result.getDocument(); } } catch (TransformerConfigurationException e) { throw new RuntimeException(e); } catch (TransformerFactoryConfigurationError e) { throw new RuntimeException(e); } catch (TransformerException e) { throw new RuntimeException(e); } }
From source file:ca.coder2000.recipes.RecipeFile.java
License:Mozilla Public License
/** * Returns formatted HTML from the xml file. * @param stylesheet The stylesheet to transform against. * @return The HTML to be used.// ww w . java 2 s . c om */ public Document getHTML(File stylesheet) { Transformer transformer; TransformerFactory factory = TransformerFactory.newInstance(); DocumentSource source = new DocumentSource(doc); DocumentResult result = new DocumentResult(); try { transformer = factory.newTransformer(new StreamSource(stylesheet)); transformer.transform(source, result); // Hard coded path MUST remove OutputFormat format = OutputFormat.createPrettyPrint(); XMLWriter writer = new XMLWriter(new FileWriter("k:/recipe/recipe.tmp"), format); writer.write(result.getDocument()); } catch (TransformerException te) { } catch (IOException ioe) { } return result.getDocument(); }
From source file:cn.buk.api.service.CtripHotelServiceImpl.java
License:LGPL
/** * ??// w w w.j av 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?/*from w ww . j av a 2s . com*/ 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 String createXml4HotelRequestBody(HotelRequestBody request, String requestType) throws JAXBException { 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);// ww w. ja va 2 s .c om org.dom4j.Document document = documentResult.getDocument(); org.dom4j.Element requestElement = document.getRootElement(); org.dom4j.Document doc1 = DocumentHelper.createDocument(); org.dom4j.Element rootElement = createRequestHeaderElement(doc1, requestType); rootElement.add(requestElement); return doc1.asXML(); }
From source file:com.amalto.commons.core.utils.XMLUtils.java
License:Open Source License
public static org.dom4j.Document styleDocument(org.dom4j.Document document, String stylesheet) throws Exception { // load the transformer using JAXP Transformer transformer = saxonTransformerFactory .newTransformer(new StreamSource(new StringReader(stylesheet))); // now lets style the given document DocumentSource source = new DocumentSource(document); DocumentResult result = new DocumentResult(); transformer.transform(source, result); // return the transformed document org.dom4j.Document transformedDoc = result.getDocument(); if (logger.isDebugEnabled()) { logger.debug("The xml file style transformed successfully "); }/*w ww. j a va2 s . co m*/ return transformedDoc; }
From source file:com.amalto.workbench.utils.XmlUtil.java
License:Open Source License
public static Document styleDocument(Document document, String stylesheet) throws Exception { // load the transformer using JAXP TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(new StreamSource(stylesheet)); // now lets style the given document DocumentSource source = new DocumentSource(document); DocumentResult result = new DocumentResult(); transformer.transform(source, result); // return the transformed document Document transformedDoc = result.getDocument(); logger.info(Messages.XmlUtil_Loginfo2); return transformedDoc; }
From source file:com.collabnet.ccf.core.transformer.DynamicXsltProcessor.java
License:Open Source License
/** * Applies the transform to the Dom4J document * //from w ww . j a v a 2s . co m * @param transformer * List of transformers to be applied, all transformers have to * execute properly, but only the result from latest one is * returned * @param d * the document to transform * * @return an array containing a single XML string representing the * transformed document * @throws TransformerException * thrown if an XSLT runtime error happens during transformation */ public static Document transform(List<Transformer> transformer, Document d) throws TransformerException { DocumentSource source = null; DocumentResult result = null; /** * We will run through all transformers, so that we can have specially * configured transformers that check things like calls to external * functions Only the result from the last transformer is returned */ for (Transformer trans : transformer) { // TODO: Allow the user to specify stylesheet parameters? source = new DocumentSource(d); result = new DocumentResult(); trans.transform(source, result); } return result.getDocument(); }
From source file:com.collabnet.ccf.core.transformer.XsltProcessor.java
License:Open Source License
/** * Applies the transform to the Dom4J document * // w ww . j av a2 s . c o m * @param transformer * @param d * the document to transform * * @return an array containing a single XML string representing the * transformed document * @throws TransformerException * thrown if an XSLT runtime error happens during transformation */ public static Document transform(Transformer transformer, Document d) throws TransformerException { DocumentSource source = new DocumentSource(d); DocumentResult result = new DocumentResult(); // TODO: Allow the user to specify stylesheet parameters? transformer.transform(source, result); return result.getDocument(); }
From source file:com.collabnet.ccf.schemageneration.CCFXSLTSchemaAndXSLTFileGenerator.java
License:Apache License
/** * Applies the transform to the Dom4J document * /*w ww . j av a 2s . c o m*/ * @param transformer * @param d * the document to transform * * @return an array containing a single XML string representing the * transformed document * @throws TransformerException * thrown if an XSLT runtime error happens during transformation */ private static Document transform(Transformer transformer, Document d) throws TransformerException { DocumentSource source = new DocumentSource(d); DocumentResult result = new DocumentResult(); // TODO: Allow the user to specify stylesheet parameters? transformer.transform(source, result); return result.getDocument(); }