buildframework/helium/sf/java/legacy/src/com/nokia/ant/ModelPropertiesParser.java
changeset 587 85df38eb4012
child 628 7c4a911dc066
equal deleted inserted replaced
217:0f5e3a7fb6af 587:85df38eb4012
       
     1 /*
       
     2 * Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17  
       
    18 package com.nokia.ant;
       
    19 
       
    20 import info.bliki.wiki.model.WikiModel;
       
    21 
       
    22 import java.io.File;
       
    23 import java.io.FileNotFoundException;
       
    24 import java.io.FileOutputStream;
       
    25 import java.io.IOException;
       
    26 import java.io.UnsupportedEncodingException;
       
    27 import java.util.Iterator;
       
    28 import java.util.List;
       
    29 
       
    30 import org.dom4j.Document;
       
    31 import org.dom4j.DocumentException;
       
    32 import org.dom4j.Element;
       
    33 import org.dom4j.io.OutputFormat;
       
    34 import org.dom4j.io.SAXReader;
       
    35 import org.dom4j.io.XMLWriter;
       
    36 import org.apache.log4j.Logger;
       
    37 
       
    38 
       
    39 /**
       
    40  * Renders model property and group description to Wiki Model Syntax
       
    41  * @author Helium Team
       
    42  */
       
    43 public class ModelPropertiesParser
       
    44 {
       
    45     private File inputPath;
       
    46     private File outputPath;
       
    47     private Document doc;
       
    48     private Logger log = Logger.getLogger(ModelPropertiesParser.class);
       
    49     
       
    50     public ModelPropertiesParser(File inputPath, File outputPath)
       
    51     {
       
    52         this.inputPath = inputPath;
       
    53         this.outputPath = outputPath;
       
    54     }
       
    55 
       
    56     /**
       
    57      * Reads model xml file, changes description format.
       
    58      * @throws DocumentException
       
    59      * @throws IOException
       
    60      */
       
    61     public void parsePropertiesDescription() throws IOException, DocumentException
       
    62     {
       
    63         SAXReader xmlReader = new SAXReader();
       
    64         doc = xmlReader.read(inputPath);
       
    65         List importNodes = doc.selectNodes("//description");
       
    66         for (Iterator iterator = importNodes.iterator(); iterator.hasNext();)
       
    67         {
       
    68             Element importCurrentNode = (Element) iterator.next();
       
    69             importCurrentNode.setText(renderWikiModel(importCurrentNode.getText()));
       
    70             writeXMLFile();
       
    71         }
       
    72     }
       
    73     
       
    74      /**
       
    75      * Writes Document object as xml file
       
    76      */
       
    77     private void writeXMLFile()
       
    78     {
       
    79         try {
       
    80             if (outputPath != null) {
       
    81             XMLWriter out = new XMLWriter(new FileOutputStream(outputPath), OutputFormat.createPrettyPrint());
       
    82             out.write(doc);
       
    83           }
       
    84         } catch (FileNotFoundException  e) {
       
    85             //We are Ignoring the errors as no need to fail the build.
       
    86             log.debug("Not able to write into XML Document " + e.getMessage());
       
    87         } catch (UnsupportedEncodingException e) {
       
    88             //We are Ignoring the errors as no need to fail the build.
       
    89             log.debug("Not able to write into XML Document " + e.getMessage());
       
    90         } catch (IOException e) {
       
    91             //We are Ignoring the errors as no need to fail the build.
       
    92             log.debug("Not able to write into XML Document " + e.getMessage());
       
    93         }
       
    94     }
       
    95     
       
    96      /**
       
    97      * Render the description as Wiki model
       
    98      * @param String descriptionText
       
    99      * @return String
       
   100      */
       
   101     private String renderWikiModel(String descriptionText) throws IOException, DocumentException
       
   102     {
       
   103         if (descriptionText != null)
       
   104         {
       
   105             WikiModel wikiModel = new WikiModel("", "");
       
   106             //If description contains unwanted symbols like "**", "==" and "- -", remove those from description.
       
   107             if (descriptionText.contains("**") || descriptionText.contains("==") || descriptionText.contains("- -"))
       
   108             {
       
   109                 descriptionText = descriptionText.replace("**", "").replace("==", "").replace("- -", "").trim();
       
   110             }
       
   111             //If description starts with "-", remove it. As wiki have special meaning for this symbol
       
   112             if (descriptionText.startsWith("-"))
       
   113                 descriptionText = descriptionText.replace("-", "");
       
   114             descriptionText = descriptionText.trim();
       
   115             //Render description with wiki model syntax
       
   116             descriptionText = wikiModel.render(descriptionText);
       
   117         }
       
   118         else
       
   119         {
       
   120             descriptionText = "";
       
   121         }
       
   122       return descriptionText;
       
   123      }
       
   124 }
       
   125