Debian Codegraph @

demo/fo/src/com/idiominc/ws/opentopic/xsl/extension/DitaVersion.java

raw symbols SCIP
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 Copyright (c) 2004-2006 by Idiom Technologies, Inc. All rights reserved.
16 IDIOM is a registered trademark of Idiom Technologies, Inc. and WORLDSERVER
17 and WORLDSTART are trademarks of Idiom Technologies, Inc. All other
18 trademarks are the property of their respective owners.
19
20 IDIOM TECHNOLOGIES, INC. IS DELIVERING THE SOFTWARE "AS IS," WITH
21 ABSOLUTELY NO WARRANTIES WHATSOEVER, WHETHER EXPRESS OR IMPLIED, AND IDIOM
22 TECHNOLOGIES, INC. DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING
23 BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 PURPOSE AND WARRANTY OF NON-INFRINGEMENT. IDIOM TECHNOLOGIES, INC. SHALL NOT
25 BE LIABLE FOR INDIRECT, INCIDENTAL, SPECIAL, COVER, PUNITIVE, EXEMPLARY,
26 RELIANCE, OR CONSEQUENTIAL DAMAGES (INCLUDING BUT NOT LIMITED TO LOSS OF
27 ANTICIPATED PROFIT), ARISING FROM ANY CAUSE UNDER OR RELATED TO OR ARISING
28 OUT OF THE USE OF OR INABILITY TO USE THE SOFTWARE, EVEN IF IDIOM
29 TECHNOLOGIES, INC. HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
30
31 Idiom Technologies, Inc. and its licensors shall not be liable for any
32 damages suffered by any person as a result of using and/or modifying the
33 Software or its derivatives. In no event shall Idiom Technologies, Inc.'s
34 liability for any damages hereunder exceed the amounts received by Idiom
35 Technologies, Inc. as a result of this transaction.
36
37 These terms and conditions supersede the terms and conditions in any
38 licensing agreement to the extent that such terms and conditions conflict
39 with those set forth herein.
40
41 This file is part of the DITA Open Toolkit project hosted on Sourceforge.net.
42 See the accompanying license.txt file for applicable licenses.
43
44 Parts copyright by Suite Solutions, released under the same terms as the DITA-OT.
45 */
46 public class DitaVersion extends Task {
47
48 private String documentPath;
49
50 /**
51 * Sets the path to the _MERGED file which will be parsed.
52 *
53 * @param path The path to set
54 */
55 public void setDocumentPath(final String path) {
56 this.documentPath = path;
57 }
58
59 /**
60 * Executes the Ant task.
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 /* Since an exception is used to stop parsing when the search
85 * is successful, catch the exception.
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 /* Successfully found ditaarch, so stop parsing. */
113 throw new SAXException("Search finished");
114 }
115
116 }
117
118 }
119
120 }
121
122 /**
123 * Sets property in active ant project with name specified inpropertyName,
124 * and value specified in propertyValue parameter
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 }