587
|
1 |
/*
|
628
|
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 |
|
587
|
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 |
|
628
|
30 |
import org.apache.log4j.Logger;
|
587
|
31 |
import org.dom4j.Document;
|
|
32 |
import org.dom4j.DocumentException;
|
|
33 |
import org.dom4j.Element;
|
|
34 |
import org.dom4j.io.OutputFormat;
|
|
35 |
import org.dom4j.io.SAXReader;
|
|
36 |
import org.dom4j.io.XMLWriter;
|
|
37 |
|
|
38 |
/**
|
|
39 |
* Renders model property and group description to Wiki Model Syntax
|
628
|
40 |
*
|
587
|
41 |
* @author Helium Team
|
|
42 |
*/
|
628
|
43 |
public class ModelPropertiesParser {
|
587
|
44 |
private File inputPath;
|
|
45 |
private File outputPath;
|
|
46 |
private Document doc;
|
|
47 |
private Logger log = Logger.getLogger(ModelPropertiesParser.class);
|
628
|
48 |
|
|
49 |
public ModelPropertiesParser(File inputPath, File outputPath) {
|
587
|
50 |
this.inputPath = inputPath;
|
|
51 |
this.outputPath = outputPath;
|
|
52 |
}
|
|
53 |
|
|
54 |
/**
|
|
55 |
* Reads model xml file, changes description format.
|
628
|
56 |
*
|
587
|
57 |
* @throws DocumentException
|
|
58 |
* @throws IOException
|
|
59 |
*/
|
628
|
60 |
public void parsePropertiesDescription() throws IOException, DocumentException {
|
587
|
61 |
SAXReader xmlReader = new SAXReader();
|
|
62 |
doc = xmlReader.read(inputPath);
|
|
63 |
List importNodes = doc.selectNodes("//description");
|
628
|
64 |
for (Iterator iterator = importNodes.iterator(); iterator.hasNext();) {
|
587
|
65 |
Element importCurrentNode = (Element) iterator.next();
|
|
66 |
importCurrentNode.setText(renderWikiModel(importCurrentNode.getText()));
|
|
67 |
writeXMLFile();
|
|
68 |
}
|
|
69 |
}
|
628
|
70 |
|
|
71 |
/**
|
587
|
72 |
* Writes Document object as xml file
|
|
73 |
*/
|
628
|
74 |
private void writeXMLFile() {
|
587
|
75 |
try {
|
|
76 |
if (outputPath != null) {
|
628
|
77 |
XMLWriter out = new XMLWriter(new FileOutputStream(outputPath), OutputFormat.createPrettyPrint());
|
|
78 |
out.write(doc);
|
|
79 |
}
|
|
80 |
}
|
|
81 |
catch (FileNotFoundException e) {
|
|
82 |
// We are Ignoring the errors as no need to fail the build.
|
587
|
83 |
log.debug("Not able to write into XML Document " + e.getMessage());
|
628
|
84 |
}
|
|
85 |
catch (UnsupportedEncodingException e) {
|
|
86 |
// We are Ignoring the errors as no need to fail the build.
|
587
|
87 |
log.debug("Not able to write into XML Document " + e.getMessage());
|
628
|
88 |
}
|
|
89 |
catch (IOException e) {
|
|
90 |
// We are Ignoring the errors as no need to fail the build.
|
587
|
91 |
log.debug("Not able to write into XML Document " + e.getMessage());
|
|
92 |
}
|
|
93 |
}
|
628
|
94 |
|
|
95 |
/**
|
587
|
96 |
* Render the description as Wiki model
|
628
|
97 |
*
|
587
|
98 |
* @param String descriptionText
|
|
99 |
* @return String
|
|
100 |
*/
|
628
|
101 |
private String renderWikiModel(String descriptionText) throws IOException, DocumentException {
|
|
102 |
if (descriptionText != null) {
|
587
|
103 |
WikiModel wikiModel = new WikiModel("", "");
|
628
|
104 |
// If description contains unwanted symbols like "**", "==" and "- -", remove those from
|
|
105 |
// description.
|
|
106 |
if (descriptionText.contains("**") || descriptionText.contains("==")
|
|
107 |
|| descriptionText.contains("- -")) {
|
587
|
108 |
descriptionText = descriptionText.replace("**", "").replace("==", "").replace("- -", "").trim();
|
|
109 |
}
|
628
|
110 |
// If description starts with "-", remove it. As wiki have special meaning for this
|
|
111 |
// symbol
|
|
112 |
if (descriptionText.startsWith("-")) {
|
587
|
113 |
descriptionText = descriptionText.replace("-", "");
|
628
|
114 |
}
|
587
|
115 |
descriptionText = descriptionText.trim();
|
628
|
116 |
// Render description with wiki model syntax
|
587
|
117 |
descriptionText = wikiModel.render(descriptionText);
|
|
118 |
}
|
628
|
119 |
else {
|
587
|
120 |
descriptionText = "";
|
|
121 |
}
|
628
|
122 |
return descriptionText;
|
|
123 |
}
|
587
|
124 |
}
|