Java Code Examples for org.dom4j.Element#asXML()

The following examples show how to use org.dom4j.Element#asXML() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AppMsgXmlHandler.java    From SmartIM with Apache License 2.0 6 votes vote down vote up
/**
 * 构造发送文件时的消息内容xml
 * 
 * @param file
 *            文件
 * @param mediaId
 *            通过文件上传返回的mediaId
 * @return 消息xml内容
 */
public String encode(File file, String mediaId) {
    Element root = DocumentHelper.createElement("appmsg");
    root.addAttribute("appid", "wxeb7ec651dd0aefa9");
    root.addAttribute("sdkver", "");
    root.addElement("title").setText(file.getName());
    root.addElement("des");
    root.addElement("action");
    root.addElement("type").setText("6");
    root.addElement("content");
    root.addElement("url");
    root.addElement("rowurl");
    Element appattach = root.addElement("appattach");
    appattach.addElement("totallen").setText(String.valueOf(file.length()));
    appattach.addElement("attachid").setText(mediaId);
    root.addElement("extinfo");
    return root.asXML();
}
 
Example 2
Source File: RemoteArticleService.java    From boubei-tss with Apache License 2.0 6 votes vote down vote up
private String createReturnXML(PageInfo pageInfo, Long channelId){
    Channel channel = channelDao.getEntity(channelId);
    List<?> articleList = pageInfo.getItems();
    
    Document doc = DocumentHelper.createDocument();
    Element channelElement = doc.addElement("rss").addAttribute("version", "2.0");
    
    channelElement.addElement("channelName").setText( channel.getName() ); // 多个栏目一起查找,取第一个栏目
    channelElement.addElement("totalRows").setText(String.valueOf(pageInfo.getTotalRows()));
    channelElement.addElement("totalPageNum").setText(String.valueOf(pageInfo.getTotalPages()));
    channelElement.addElement("currentPage").setText(String.valueOf(pageInfo.getPageNum()));
    for (int i = 0; i < articleList.size(); i++) {
        Object[] fields = (Object[]) articleList.get(i);
        Element itemElement = createArticleElement(channelElement, fields);
        
        Long articleId = (Long) fields[0];
        List<Attachment> attachments = articleDao.getArticleAttachments(articleId);
        ArticleHelper.addPicListInfo(itemElement, attachments);
    }
    
    return channelElement.asXML();
}
 
Example 3
Source File: CfTableRepository.java    From mybatis-dalgen with Apache License 2.0 5 votes vote down vote up
/**
 * Sets cf operation cdata.
 *
 * @param cfTable the cf table
 * @param e the e
 * @param cfOperation the cf operation
 */
private void setCfOperationCdata(CfTable cfTable, Element e, CfOperation cfOperation) {
    String cXml = e.asXML();
    String[] lines = StringUtils.split(cXml, "\n");
    StringBuilder sb = new StringBuilder();
    for (int i = 1; i < lines.length - 1; i++) {
        if (i > 1) {
            sb.append("\n");
        }
        sb.append(lines[i]);
    }
    String cdata = sb.toString();
    //SQlDESC
    String sqlDesc = cdata.replaceAll(FOR_DESC_SQL_P, " ");
    sqlDesc = sqlDesc.replaceAll(FOR_DESC_SQL_PN, " ");
    cfOperation.setSqlDesc(sqlDesc);

    String text = e.getTextTrim();
    if (StringUtils.indexOf(text, "*") > 0) {
        Matcher m = STAR_BRACKET.matcher(text);
        if (!m.find()) {
            cdata = StringUtils.replace(cdata, "*", "<include refid=\"Base_Column_List\" />");
        }
    }
    //? 参数替换 不指定类型
    cdata = delQuestionMarkParam(cdata, cfOperation, cfTable);

    //添加sql注释,以便于DBA 分析top sql 定位
    cfOperation.setCdata(addSqlAnnotation(cdata, cfOperation.getName(), cfTable.getSqlname()));
    //pageCount添加
    setCfOperationPageCdata(cdata, cfOperation);
}
 
Example 4
Source File: RemoteArticleService.java    From boubei-tss with Apache License 2.0 5 votes vote down vote up
public String getArticleListByChannel(Long channelId, int page, int pageSize, boolean isNeedPic) {
    Channel channel = channelDao.getEntity(channelId);
    if(channel == null) {
    	log.info("ID为:" + channelId + " 的栏目不存在!");
        return "<Response><ArticleList><rss><totalPageNum>0</totalPageNum><totalRows>0</totalRows></rss></ArticleList></Response>";
    }
    
    channelId = checkBrowsePermission(channelId);
    
    ArticleQueryCondition condition = new ArticleQueryCondition();
    condition.setChannelId(channelId);
    condition.getPage().setPageNum(page);
    condition.getPage().setPageSize(pageSize);
    condition.setStatus(CMSConstants.XML_STATUS);
    
    PageInfo pageInfo = articleDao.getChannelPageArticleList(condition);
        
    Document doc = org.dom4j.DocumentHelper.createDocument();
    Element channelElement = doc.addElement("rss").addAttribute("version", "2.0");
    
    channelElement.addElement("channelName").setText(channel.getName()); 
    channelElement.addElement("totalPageNum").setText(String.valueOf(pageInfo.getTotalPages()));
    channelElement.addElement("totalRows").setText(String.valueOf(pageInfo.getTotalRows()));
    channelElement.addElement("currentPage").setText(page + "");
    List<?> articleList = pageInfo.getItems();
    for (int i = 0; i < articleList.size(); i++) {
        Object[] fields = (Object[]) articleList.get(i);
        Element itemElement = createArticleElement(channelElement, fields);
        
        if(isNeedPic){
        	Long articleId = (Long) fields[0];
        	List<Attachment> attachments = articleDao.getArticleAttachments(articleId);
            ArticleHelper.addPicListInfo(itemElement, attachments);
        }
    }
    return "<Response><ArticleList>" + channelElement.asXML() + "</ArticleList></Response>";
}
 
Example 5
Source File: FvoMessage.java    From mts with GNU General Public License v3.0 5 votes vote down vote up
public void parseElement(Element root) throws Exception {
    Element header = (Element) root.selectSingleNode("./header");
    Attribute messageTypeValue = (Attribute) root.selectSingleNode("./header/field[@name='Message_Type']/@value");
    
    //header
    if (header != null) {
        _header = new FvoParameter(_msg);
        _header.setLittleEndian( _dictionary.getHeader().isLittleEndian());
        _header.parseElement(header);
    }
    
    //typeCode
    if (messageTypeValue != null) {
        try {
            setMessageType(Integer.decode(messageTypeValue.getValue()));
        }
        catch (Exception e) {
            // TODO: handle exception recherche dans dictionnaire
            throw e;
        }
    }
    else {
        throw new ExecutionException("The Message_Type field must be set in the header\n" + root.asXML());
    }

    //parameters
    parseParametersFromXml((List<Element>)(List<?>)root.selectNodes("./parameter"));
}
 
Example 6
Source File: PublishedItem.java    From Openfire with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the payload included when publishing the item. A published item may or may not
 * have a payload. Transient nodes that are configured to not broadcast payloads may allow
 * published items to have no payload.
 *
 * @param payload the payload included when publishing the item or {@code null}
 *        if none was found.
 */
void setPayload(Element payload) {
    this.payload = payload;
    // Update XML representation of the payload
    if (payload == null) {
        payloadXML = null;
    } else {
        payloadXML = payload.asXML();
    }
}
 
Example 7
Source File: UpdateManager.java    From Openfire with Apache License 2.0 5 votes vote down vote up
private String getServerUpdateRequest() {
    XMPPServer server = XMPPServer.getInstance();
    Element xmlRequest = docFactory.createDocument().addElement("version");
    // Add current openfire version
    Element openfire = xmlRequest.addElement("openfire");
    openfire.addAttribute("current", server.getServerInfo().getVersion().getVersionString());
    return xmlRequest.asXML();
}
 
Example 8
Source File: UpdateManager.java    From Openfire with Apache License 2.0 5 votes vote down vote up
private String getAvailablePluginsUpdateRequest() {
    Element xmlRequest = docFactory.createDocument().addElement("available");
    // Add locale so we can get current name and description of plugins
    Element locale = xmlRequest.addElement("locale");
    locale.addText(JiveGlobals.getLocale().toString());
    return xmlRequest.asXML();
}
 
Example 9
Source File: HttpSessionManager.java    From Openfire with Apache License 2.0 5 votes vote down vote up
private static String createSessionCreationResponse(HttpSession session) throws DocumentException {
    Element response = DocumentHelper.createElement( QName.get( "body", "http://jabber.org/protocol/httpbind" ) );
    response.addNamespace("stream", "http://etherx.jabber.org/streams");
    response.addAttribute("from", session.getServerName());
    response.addAttribute("authid", session.getStreamID().getID());
    response.addAttribute("sid", session.getStreamID().getID());
    response.addAttribute("secure", Boolean.TRUE.toString());
    response.addAttribute("requests", String.valueOf(session.getMaxRequests()));
    response.addAttribute("inactivity", String.valueOf(session.getInactivityTimeout()));
    response.addAttribute("polling", String.valueOf(session.getMaxPollingInterval()));
    response.addAttribute("wait", String.valueOf(session.getWait()));
    if ((session.getMajorVersion() == 1 && session.getMinorVersion() >= 6) ||
        session.getMajorVersion() > 1) {
        response.addAttribute("hold", String.valueOf(session.getHold()));
        response.addAttribute("ack", String.valueOf(session.getLastAcknowledged()));
        response.addAttribute("maxpause", String.valueOf(session.getMaxPause()));
        response.addAttribute("ver", String.valueOf(session.getMajorVersion())
                + "." + String.valueOf(session.getMinorVersion()));
    }

    Element features = response.addElement("stream:features");
    for (Element feature : session.getAvailableStreamFeaturesElements()) {
        features.add(feature);
    }

    return response.asXML();
}
 
Example 10
Source File: HttpSession.java    From Openfire with Apache License 2.0 5 votes vote down vote up
protected String createEmptyBody(boolean terminate)
{
    final Element body = DocumentHelper.createElement( QName.get( "body", "http://jabber.org/protocol/httpbind" ) );
    if (terminate) { body.addAttribute("type", "terminate"); }
    body.addAttribute("ack", String.valueOf(getLastAcknowledged()));
    return body.asXML();
}
 
Example 11
Source File: HttpSession.java    From Openfire with Apache License 2.0 5 votes vote down vote up
private String createSessionRestartResponse()
{
    final Element response = DocumentHelper.createElement( QName.get( "body", "http://jabber.org/protocol/httpbind" ) );
    response.addNamespace("stream", "http://etherx.jabber.org/streams");

    final Element features = response.addElement("stream:features");
    for (Element feature : getAvailableStreamFeaturesElements()) {
        features.add(feature);
    }

    return response.asXML();
}
 
Example 12
Source File: DefaultXmlCodec.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the xml string from YDT.
 *
 * @param ydtBuilder YDT builder
 * @return the xml string from YDT
 */
private String buildXmlForYdt(YdtBuilder ydtBuilder) {

    YdtExtendedBuilder extBuilder = (YdtExtendedBuilder) ydtBuilder;
    YdtExtendedContext rootNode = extBuilder.getRootNode();

    if (rootNode == null) {
        throw new YchException(E_YDT_ROOT_NODE);
    }

    // Creating the root element for xml.
    Element rootElement =
            DocumentHelper.createDocument().addElement(rootNode.getName());

    // Adding the name space if exist for root name.
    if (rootNode.getNamespace() != null) {
        rootElement.add(Namespace.get(rootNode.getNamespace()));
    }

    if ("config".equals(rootElement.getName())) {
        rootElement.add(new Namespace("nc", "urn:ietf:params:xml:ns:netconf:base:1.0"));
    }

    // Adding the attribute if exist
    Map<String, String> tagAttrMap = extBuilder.getRootTagAttributeMap();
    if (tagAttrMap != null && !tagAttrMap.isEmpty()) {
        for (Map.Entry<String, String> attr : tagAttrMap.entrySet()) {
            rootElement.addAttribute(attr.getKey(), attr.getValue());
        }
    }

    XmlCodecYdtListener listener = new XmlCodecYdtListener(XML, rootNode);
    listener.getElementStack().push(rootElement);

    // Walk through YDT and build the xml.
    YdtExtendedWalker extWalker = new DefaultYdtWalker();
    extWalker.walk(listener, rootNode);

    return rootElement.asXML();
}
 
Example 13
Source File: RemoteArticleService.java    From boubei-tss with Apache License 2.0 4 votes vote down vote up
public String getArticleListByChannelAndTime(Long channelId, Integer year, Integer month) {
    if(channelId == null){
        throw new BusinessException(EX.CMS_1);
    }
    if(year == null || month == null){
        throw new BusinessException(EX.CMS_10);
    }
    
    Channel channel = channelDao.getEntity(channelId);
    if(channel == null) {
        throw new BusinessException(EX.CMS_11);
    }
    Channel site = channel.getSite();
    String publishBaseDir = site.getPath();
   
    String publishDir = publishBaseDir + "/" + year + "/" + DateUtil.fixMonth(month);
    List<File> xmlFiles = FileHelper.listFilesByTypeDeeply(".xml", new File(publishDir));
  
    Document doc = org.dom4j.DocumentHelper.createDocument();
    Element channelElement = doc.addElement("rss").addAttribute("version", "2.0");
 
    channelElement.addElement("channelName").setText(channel.getName()); 
    channelElement.addElement("totalPageNum").setText("1");
    channelElement.addElement("totalRows").setText("100");
    channelElement.addElement("currentPage").setText("1");
    for( File xmlFile : xmlFiles ){
        if(xmlFile.getName().startsWith(channelId + "_")){
            Document articleDoc = XMLDocUtil.createDocByAbsolutePath2(xmlFile.getPath());
            
            Node articleNode   = articleDoc.getRootElement();
            Node idNode        = articleNode.selectSingleNode("//id");
            Node titleNode     = articleNode.selectSingleNode("//title");
            Node authorNode    = articleNode.selectSingleNode("//author");
            Node summaryNode   = articleNode.selectSingleNode("//summary");
            Node issueDateNode = articleNode.selectSingleNode("//issueDate");
            
            createArticleElement(channelElement,
            		XMLDocUtil.getNodeText(idNode), 
            		XMLDocUtil.getNodeText(titleNode), 
            		XMLDocUtil.getNodeText(authorNode),
            		DateUtil.parse(XMLDocUtil.getNodeText(issueDateNode)), 
            		XMLDocUtil.getNodeText(summaryNode), 
                    0, 0, null);
        }
    }
    return "<Response><ArticleList>" + channelElement.asXML() + "</ArticleList></Response>";
}
 
Example 14
Source File: TlvField.java    From mts with GNU General Public License v3.0 4 votes vote down vote up
/**
  * Parse a field from a XML file
  *
  * @param 	root		: path of the field in the XML scenario file
  * @throws Exception
  */
 public void parseElement(Element root) throws Exception {
     //Capture the different attribute from the XML file
     String name = root.attributeValue("name");
     String length = root.attributeValue("length");
     String lengthBit = root.attributeValue("lengthBit");
     String value = root.attributeValue("value");
     String format = root.attributeValue("format");

     // decode enumeration to value
     if(_dictionary.getEnumerationCodeFromName(name, value) != null){
         value = _dictionary.getEnumerationCodeFromName(name, value);
         value = "" + (int) Long.parseLong(value);
     }

     if(null != value){
         _value = value;
     }

     if(null != format){
         _format = format;
     }

     if(null != name){
         _name = name;
     }

     // check the type is valid
     if(!("fvo".equalsIgnoreCase(_format) ||
             "integer".equalsIgnoreCase(_format) ||
             "spare".equalsIgnoreCase(_format) ||
             "string".equalsIgnoreCase(_format) || 
             "binary".equalsIgnoreCase(_format))){
             throw new ExecutionException("UA layer : The format of a field must be set integer/string/binary/fvo\n" + root.asXML());
     }

     // if the type is fvo, then search if the fvo message is defined in message
     if ("fvo".equalsIgnoreCase(_format) && null == _msg.getFvoMessage()) {
         throw new ExecutionException("<SS7> tag should be defined in message because there is a 'fvo' type field\n" + root.asXML());
     }

     // override the length if defined in element
     if(null != lengthBit || null != length){
         _length = 0;
         _lengthBit = 0;
         
         if(null != length){
             _length = Integer.decode(length);
         }

         if(null != lengthBit){
             _length += Integer.decode(lengthBit) / 8;
             _lengthBit = Integer.decode(lengthBit) % 8;
         }
     }
}
 
Example 15
Source File: FvoField.java    From mts with GNU General Public License v3.0 4 votes vote down vote up
public void parseElement(Element root) throws Exception {
    String name = root.attributeValue("name");
    if(name == null)
    {
    	//throw new ExecutionException("A name is required for fvo fields\n" + root.asXML());
    }
    else
    {
        _name = name;
    }
    String value = root.attributeValue("value");
    // test for errors (value is mandatory attribute)
    if (value == null) 
    {
        throw new ExecutionException("A value is required for fvo fields\n" + root.asXML());
    }
    else
    {
    	this._value = value;
    }

    String format = root.attributeValue("format");
    // field info should already have been completed from dicionary, using constructor that clones
    // test for errors (value is mandatory attribute)
    if (format != null &&
       !format.equalsIgnoreCase(FvoField.formatDigit) &&
       !format.equalsIgnoreCase(FvoField.formatBinary) &&
       !format.equalsIgnoreCase(FvoField.formatInteger) &&
       !format.equalsIgnoreCase(FvoField.formatString)) {
        throw new ExecutionException("The format of a FvoField must be one of " +
                FvoField.formatBinary + ", " +
                FvoField.formatDigit + ", " +
                FvoField.formatInteger + ", " +
                FvoField.formatString + ". Error in XML :\n" + root.asXML());
    }
    else{
        this._format = format;
    }

    String length = root.attributeValue("length");
    String lengthBit = root.attributeValue("lengthBit");
    if(null != length || null != lengthBit){
        _lengthBit = 0;
        if(null != length){
            _lengthBit += Integer.decode(length) * 8;
        }

        if(null != lengthBit){
            _lengthBit += Integer.decode(lengthBit);
        }
    }
    
    String littleEndian = root.attributeValue("littleEndian");
    if (littleEndian != null) {
        setLittleEndian(Utils.parseBoolean(littleEndian, "littleEndian"));
    }        
}
 
Example 16
Source File: HttpBindServlet.java    From Openfire with Apache License 2.0 4 votes vote down vote up
protected static String createErrorBody(String type, String condition) {
    final Element body = DocumentHelper.createElement( QName.get( "body", "http://jabber.org/protocol/httpbind" ) );
    body.addAttribute("type", type);
    body.addAttribute("condition", condition);
    return body.asXML();
}