List of usage examples for org.dom4j Element addNamespace
Element addNamespace(String prefix, String uri);
From source file:at.jabberwocky.impl.core.io.PacketReader.java
private Document parseDocument() throws DocumentException, IOException, XmlPullParserException { DocumentFactory df = docFactory;//from www . j a v a 2s. com Document document = df.createDocument(); Element parent = null; XmlPullParser pp = parser; int count = 0; while (true) { int type = -1; type = pp.nextToken(); switch (type) { case XmlPullParser.PROCESSING_INSTRUCTION: { String text = pp.getText(); int loc = text.indexOf(" "); if (loc >= 0) { document.addProcessingInstruction(text.substring(0, loc), text.substring(loc + 1)); } else { document.addProcessingInstruction(text, ""); } break; } case XmlPullParser.COMMENT: { if (parent != null) { parent.addComment(pp.getText()); } else { document.addComment(pp.getText()); } break; } case XmlPullParser.CDSECT: { String text = pp.getText(); if (parent != null) { parent.addCDATA(text); } else { if (text.trim().length() > 0) { throw new DocumentException("Cannot have text content outside of the root document"); } } break; } case XmlPullParser.ENTITY_REF: { String text = pp.getText(); if (parent != null) { parent.addText(text); } else { if (text.trim().length() > 0) { throw new DocumentException("Cannot have an entityref outside of the root document"); } } break; } case XmlPullParser.END_DOCUMENT: { return document; } case XmlPullParser.START_TAG: { QName qname = (pp.getPrefix() == null) ? df.createQName(pp.getName(), pp.getNamespace()) : df.createQName(pp.getName(), pp.getPrefix(), pp.getNamespace()); Element newElement = null; // Do not include the namespace if this is the start tag of a new packet // This avoids including "jabber:client", "jabber:server" or // "jabber:component:accept" if ("jabber:client".equals(qname.getNamespaceURI()) || "jabber:server".equals(qname.getNamespaceURI()) || "jabber:component:accept".equals(qname.getNamespaceURI()) || "http://jabber.org/protocol/httpbind".equals(qname.getNamespaceURI())) { newElement = df.createElement(pp.getName()); } else { newElement = df.createElement(qname); } int nsStart = pp.getNamespaceCount(pp.getDepth() - 1); int nsEnd = pp.getNamespaceCount(pp.getDepth()); for (int i = nsStart; i < nsEnd; i++) { if (pp.getNamespacePrefix(i) != null) { newElement.addNamespace(pp.getNamespacePrefix(i), pp.getNamespaceUri(i)); } } for (int i = 0; i < pp.getAttributeCount(); i++) { QName qa = (pp.getAttributePrefix(i) == null) ? df.createQName(pp.getAttributeName(i)) : df.createQName(pp.getAttributeName(i), pp.getAttributePrefix(i), pp.getAttributeNamespace(i)); newElement.addAttribute(qa, pp.getAttributeValue(i)); } if (parent != null) { parent.add(newElement); } else { document.add(newElement); } parent = newElement; count++; break; } case XmlPullParser.END_TAG: { if (parent != null) { parent = parent.getParent(); } count--; if (count < 1) { return document; } break; } case XmlPullParser.TEXT: { String text = pp.getText(); if (parent != null) { parent.addText(text); } else { if (text.trim().length() > 0) { throw new DocumentException("Cannot have text content outside of the root document"); } } break; } default: } } }
From source file:ch.epfl.codimsd.qep.QEPFactory.java
License:Open Source License
/** * Creates the xml structure where we embed the operators. * //from www .j a v a 2 s . c om * @param type type of the xml template. * @return dom4j Document. */ private static Document createTemplate(String type) { // Create empty dom4j document. Document document = DocumentHelper.createDocument(); // Add "QEPTemlate", "op", "qep", tags. Element root = document.addElement("QEPTemplate", "http://giga03.lncc.br/DIP/WP4/CoDIMS-D"); root.addNamespace("op", "http://giga03.lncc.br/DIP/WP4/CoDIMS-D/Operator"); root.addNamespace("qep", "http://giga03.lncc.br/DIP/WP4/CoDIMS-D/QEP"); root.addElement("qep:QEP").addAttribute("type", type); return document; }
From source file:cn.buk.api.service.CtripHotelServiceImpl.java
License:LGPL
/** * ??/*from w w w.j a v a 2s . co 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 a va 2s .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:com.alibaba.citrus.springext.support.SchemaUtil.java
License:Open Source License
/** contributionsschema?schema */ public static Document createConfigurationPointSchema(ConfigurationPoint configurationPoint, String version) { Document doc = createDocument(); // <xsd:schema> Element schemaRoot = doc.addElement("xsd:schema", W3C_XML_SCHEMA_NS_URI); schemaRoot.addNamespace("xsd", W3C_XML_SCHEMA_NS_URI); schemaRoot.addNamespace("beans", BEANS_NAMESPACE_URI); schemaRoot.addNamespace("springext", SPRINGEXT_BASE_URI); schemaRoot.addNamespace("", configurationPoint.getNamespaceUri()); schemaRoot.addAttribute("targetNamespace", configurationPoint.getNamespaceUri()); // <xsd:include schemaLocation="contribution schema" /> Set<String> includings = createTreeSet(); for (Contribution contrib : configurationPoint.getContributions()) { Schema contribSchema = contrib.getSchemas().getVersionedSchema(version); if (contribSchema == null) { contribSchema = contrib.getSchemas().getMainSchema(); }//from ww w .jav a 2 s . co m if (contribSchema != null) { includings.add(contribSchema.getName()); } } for (String including : includings) { Element includeElement = schemaRoot.addElement("xsd:include"); includeElement.addAttribute("schemaLocation", including); } if (configurationPoint.getDefaultElementName() != null) { // <xsd:import namespace="http://www.springframework.org/schema/beans" // schemaLocation="http://www.springframework.org/schema/beans/spring-beans.xsd" /> Element importBeans = schemaRoot.addElement("xsd:import"); importBeans.addAttribute("namespace", BEANS_NAMESPACE_URI); importBeans.addAttribute("schemaLocation", BEANS_NAMESPACE_URI + "/spring-beans.xsd"); // <xsd:import namespace="http://www.alibaba.com/schema/springext/base" // schemaLocation="http://www.alibaba.com/schema/springext/springext-base.xsd" /> Element importSpringextBase = schemaRoot.addElement("xsd:import"); importSpringextBase.addAttribute("namespace", SPRINGEXT_BASE_URI); importSpringextBase.addAttribute("schemaLocation", SPRINGEXT_BASE_XSD); // <xsd:element name="defaultElementName" type="springext:referenceableBeanType" /> Element element = schemaRoot.addElement("xsd:element"); element.addAttribute("name", configurationPoint.getDefaultElementName()); element.addAttribute("type", "springext:referenceableBeanType"); } return doc; }
From source file:com.alibaba.citrus.springext.support.SchemaUtil.java
License:Open Source License
/** schema?includes */ public static Transformer getTransformerWhoAddsIndirectIncludes(final Map<String, Schema> includes) { return new Transformer() { public void transform(Document document, String systemId) { Element root = document.getRootElement(); root.addNamespace("xsd", W3C_XML_SCHEMA_NS_URI); // <xsd:schema> if (W3C_XML_SCHEMA_NS_URI.equals(root.getNamespaceURI()) && "schema".equals(root.getName())) { Namespace xsd = DocumentHelper.createNamespace("xsd", W3C_XML_SCHEMA_NS_URI); QName includeName = DocumentHelper.createQName("include", xsd); // for each <xsd:include> for (Iterator<?> i = root.elementIterator(includeName); i.hasNext();) { i.next();/*from ww w . j a v a2s .c om*/ i.remove(); } // includes @SuppressWarnings("unchecked") List<Node> nodes = root.elements(); int i = 0; for (Schema includedSchema : includes.values()) { Element includeElement = DocumentHelper.createElement(includeName); nodes.add(i++, includeElement); includeElement.addAttribute("schemaLocation", includedSchema.getName()); } } } }; }
From source file:com.bluexml.side.Framework.alfresco.dataGenerator.serialization.mapping.XMLForACPMappingHelper.java
License:Open Source License
/** * create the xml's element corresponding to type of generated node * @param root//from w w w . ja v a2 s. c o m * @param node * @return element corresponding to given generated node's type */ public Element createType(Element root, INode node) { Element type = root.addElement(createType(node)); type.addNamespace(Constants.NATIVE_SIDE_PREFIX, Constants.NATIVE_SIDE_URI); return type; }
From source file:com.cladonia.xml.XMLUtilities.java
License:Open Source License
/** * Sets the Schema Location attribute on the root element * * @param document The Exchanger document * @param schemaType The schema type (either schemaLocation or noNamespaceSchemaLocation) * @param namespace The namespace//from ww w . j a v a 2s. c o m * @param schemaURL The URL or the schema * */ public static void setSchemaLocation(ExchangerDocument document, String schemaType, String namespace, String schemaURL) { schemaURL = URLUtilities.encodeURL(schemaURL); XDocument xdoc = document.getDocument(); Element root = xdoc.getRootElement(); if (schemaType.equals(SCHEMALOCATION)) { Attribute attrNoNS = root.attribute(NOSCHEMALOCATION); if (attrNoNS != null) { root.remove(attrNoNS); } // need to set both namspace and url Attribute attr = root.attribute(SCHEMALOCATION); if (attr == null) { // does the schema instance already exist Namespace ns = root.getNamespaceForURI(SCHEMAINSTANCE); if (ns != null) { String schemaInstancePrefix = ns.getPrefix(); StringBuffer name = new StringBuffer(); name.append(schemaInstancePrefix); name.append(":"); name.append(SCHEMALOCATION); StringBuffer value = new StringBuffer(); value.append(namespace); value.append(" "); value.append(schemaURL); root.addAttribute(name.toString(), value.toString()); } else { root.addNamespace("xsi", SCHEMAINSTANCE); StringBuffer name = new StringBuffer(); name.append("xsi"); name.append(":"); name.append(SCHEMALOCATION); StringBuffer value = new StringBuffer(); value.append(namespace); value.append(" "); value.append(schemaURL); root.addAttribute(name.toString(), value.toString()); } } else { String attrValue = attr.getValue(); // break up all the namespace and url pairs ArrayList stringValues = new ArrayList(); StringTokenizer st = new StringTokenizer(attrValue); while (st.hasMoreTokens()) { stringValues.add(st.nextToken()); } // update existing attribute, Note: it may have multiple attribute pairs StringBuffer value = new StringBuffer(); value.append(namespace); value.append(" "); value.append(schemaURL); //need to start at the third value (i.e we only replace the first namespace-url pair) for (int i = 2; i < stringValues.size(); i++) { value.append(" "); value.append((String) stringValues.get(i)); } attr.setValue(value.toString()); } } else { // is of type "no schema location" Attribute attrSchema = root.attribute(SCHEMALOCATION); if (attrSchema != null) { root.remove(attrSchema); } // just need to set the url Attribute attr = root.attribute(NOSCHEMALOCATION); if (attr == null) { // does the schema instance already exist Namespace ns = root.getNamespaceForURI(SCHEMAINSTANCE); if (ns != null) { String schemaInstancePrefix = ns.getPrefix(); StringBuffer name = new StringBuffer(); name.append(schemaInstancePrefix); name.append(":"); name.append(NOSCHEMALOCATION); root.addAttribute(name.toString(), schemaURL); } else { root.addNamespace("xsi", SCHEMAINSTANCE); StringBuffer name = new StringBuffer(); name.append("xsi"); name.append(":"); name.append(NOSCHEMALOCATION); root.addAttribute(name.toString(), schemaURL); } } else { // update existing attribute attr.setValue(schemaURL); } } }
From source file:com.ibm.cognos.API.java
License:Open Source License
public API() { Element reportElement = DocumentHelper.createElement("report"); // In dom4j 1.6.1, can no longer add xmlns with addAttribute reportElement.addNamespace("", "http://developer.cognos.com/schemas/report/8.0/"); reportElement.addAttribute("expressionLocale", "en-us"); oDocument = DocumentHelper.createDocument(reportElement); }
From source file:com.ibm.cognos.API.java
License:Open Source License
public void updateXMLNS() { Element e = oDocument.getRootElement(); // In dom4j 1.6.1, can no longer add xmlns with addAttribute e.addNamespace("", "http://developer.cognos.com/schemas/report/8.0/"); }