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