| 1 |
package com.idiominc.ws.opentopic.xsl.extension; |
| 2 |
|
| 3 |
import org.xml.sax.SAXException; |
| 4 |
import org.xml.sax.Attributes; |
| 5 |
import org.xml.sax.helpers.DefaultHandler; |
| 6 |
|
| 7 |
import javax.xml.parsers.SAXParser; |
| 8 |
import javax.xml.parsers.SAXParserFactory; |
| 9 |
import java.io.File; |
| 10 |
|
| 11 |
import org.apache.tools.ant.Task; |
| 12 |
import org.apache.tools.ant.Project; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public class DitaVersion extends Task { |
| 47 |
|
| 48 |
private String documentPath; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public void setDocumentPath(final String path) { |
| 56 |
this.documentPath = path; |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
@Override |
| 63 |
public void execute() { |
| 64 |
|
| 65 |
try { |
| 66 |
|
| 67 |
documentPath = documentPath.replace(File.separatorChar, '/'); |
| 68 |
final File file = documentPath.startsWith("file:") ? |
| 69 |
new File(new java.net.URI(documentPath)) : |
| 70 |
new File(documentPath); |
| 71 |
if (!file.exists()) { |
| 72 |
throw new Exception("File does not exist"); |
| 73 |
} |
| 74 |
if (!file.canRead()) { |
| 75 |
throw new Exception("Can't read input file"); |
| 76 |
} |
| 77 |
|
| 78 |
final SAXParserFactory saxParserFactory = SAXParserFactory.newInstance(); |
| 79 |
final SAXParser saxParser = saxParserFactory.newSAXParser(); |
| 80 |
|
| 81 |
saxParser.parse(file, new DefaultHandlerImpl()); |
| 82 |
|
| 83 |
} catch (final Exception e) { |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
if (e.getMessage() != null && |
| 88 |
e.getMessage().equals("Search finished")) { |
| 89 |
System.out.println("Search finished"); |
| 90 |
} else { |
| 91 |
e.printStackTrace(); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
} |
| 96 |
|
| 97 |
private class DefaultHandlerImpl |
| 98 |
extends DefaultHandler { |
| 99 |
|
| 100 |
@Override |
| 101 |
public void startElement(final String uri, final String localName, final String qName, final Attributes attributes) throws SAXException { |
| 102 |
final String classAttr = attributes.getValue("class"); |
| 103 |
|
| 104 |
if(classAttr != null) { |
| 105 |
if ((classAttr.indexOf(" map/map ") > -1) || |
| 106 |
(classAttr.indexOf(" topic/topic ") > -1)) { |
| 107 |
if (attributes.getIndex("ditaarch:DITAArchVersion") > -1) { |
| 108 |
setActiveProjectProperty("ws.runtime.publishing.map.dita.version",attributes.getValue("ditaarch:DITAArchVersion")); |
| 109 |
} else { |
| 110 |
setActiveProjectProperty("ws.runtime.publishing.map.dita.version","132"); |
| 111 |
} |
| 112 |
|
| 113 |
throw new SAXException("Search finished"); |
| 114 |
} |
| 115 |
|
| 116 |
} |
| 117 |
|
| 118 |
} |
| 119 |
|
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
private void setActiveProjectProperty(final String propertyName, final String propertyValue) { |
| 127 |
final Project activeProject = getProject(); |
| 128 |
if (activeProject != null) { |
| 129 |
activeProject.setProperty(propertyName, propertyValue); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
} |