View Javadoc

1   package org.opensync.tools;
2   
3   import java.io.CharArrayWriter;
4   import java.io.File;
5   import java.io.FileNotFoundException;
6   import java.io.IOException;
7   
8   import javax.xml.parsers.ParserConfigurationException;
9   import javax.xml.parsers.SAXParserFactory;
10  
11  import org.xml.sax.Attributes;
12  import org.xml.sax.InputSource;
13  import org.xml.sax.SAXException;
14  import org.xml.sax.XMLReader;
15  import org.xml.sax.helpers.DefaultHandler;
16  
17  public class Help extends DefaultHandler{
18      private CharArrayWriter contents;
19      private StringBuffer InfoContent;
20      private StringBuffer ParagraphContent;
21      private String helpContentXML;
22      private String keyword;
23      private String TempKeyword;
24      private String title;
25      private boolean myParagraph;
26      private boolean bKeywordFound;
27      private String fileName;
28  
29    /***
30     * Constructor
31     * pFileName = Chemin + Nom du fichier d'aide
32     * Help toto = new Help("file:///m:/jakarta-tomcat-3.2.2/webapps/SophiewebServlet/SOPHIEHELP.xml");
33     */
34    public Help(String pFileName) {
35      fileName=pFileName;
36    }
37  
38    public String getParagraph(String lang,String pKeyWord) throws IOException,FileNotFoundException,SAXException,ParserConfigurationException {
39    /***
40    * Récupère le contenu d'un paragraphe d'aide
41    * pKeyWord = le keyWord du PARAGRAPH a trouvé
42    * myParagraph = indique que le bon paragraphe a été trouvé
43    */
44  
45      keyword=pKeyWord;
46      myParagraph=false;
47      processHelpXml(lang);
48  
49      return helpContentXML;
50    }
51  
52    public String getParagraph(String lang,
53                               String pKeyWord1,
54                               String pKeyWord2) throws IOException,FileNotFoundException,SAXException,ParserConfigurationException {
55      keyword=pKeyWord2;
56      myParagraph=false;
57      bKeywordFound=false;
58      processHelpXml(lang);
59      if (Utils.debug) System.out.println("Help::getParagraph - looking for pKeyWord1"+pKeyWord2);
60      if (!bKeywordFound){
61        if (Utils.debug) System.out.println("Help::getParagraph - looking for pKeyWord2"+pKeyWord1);
62        keyword=pKeyWord1;
63        myParagraph=false;
64        processHelpXml(lang);
65  	 // helpContentXML+="<IMG1 Label=\""+DicoReader.getLabelDicoByKey(lang,"BACK")+"\"/><IMG2 Label=\""+DicoReader.getLabelDicoByKey(lang,"CLOSE")+"\"/><IMG3 Label=\""+DicoReader.getLabelDicoByKey(lang,"INDEX")+"\"/><IMG4 Label=\""+DicoReader.getLabelDicoByKey(lang,"PRINT")+"\"/></HELPCONTENT>";
66      }
67  	/*else{
68  
69  		keyword=pKeyWord2;
70        myParagraph=true;
71        processHelpXml(lang);
72  	  helpContentXML+="<IMG1 Label=\""+DicoReader.getLabelDicoByKey(lang,"BACK")+"\"/><IMG2 Label=\""+DicoReader.getLabelDicoByKey(lang,"CLOSE")+"\"/><IMG3 Label=\""+DicoReader.getLabelDicoByKey(lang,"INDEX")+"\"/><IMG4 Label=\""+DicoReader.getLabelDicoByKey(lang,"PRINT")+"\"/></HELPCONTENT>";
73      }*/
74      helpContentXML+="<IMG1 Label=\""+DicoReader.getLabelDicoByKey(lang,"BACK")+"\"/><IMG2 Label=\""+DicoReader.getLabelDicoByKey(lang,"CLOSE")+"\"/><IMG3 Label=\""+DicoReader.getLabelDicoByKey(lang,"INDEX")+"\"/><IMG4 Label=\""+DicoReader.getLabelDicoByKey(lang,"PRINT")+"\"/></HELPCONTENT>";
75  
76      return helpContentXML;
77    }
78  
79    public void processHelpXml(String lang) throws IOException,FileNotFoundException,SAXException,ParserConfigurationException {
80  
81    contents = new CharArrayWriter();
82    ParagraphContent = new StringBuffer();
83    InfoContent = new StringBuffer();
84  
85        /*XMLReader xr = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
86        xr.setContentHandler( this );
87        //xr.parse(  new InputSource(new FileInputStream(fileName+lang+".xml")));
88        //PV 05-03-2002 because of weblogic 6.1 war deployement...
89       xr.parse( new InputSource(fileName+lang+".xml"));
90       */
91  
92       /* JAXP */
93        // If a validating parsing is needed, then Piccolo will use the following property to create
94        // a validating parser: here, we specify Xerces.
95        System.setProperty("com.bluecast.xml.ValidatingSAXParserFactory", "org.apache.xerces.jaxp.SAXParserFactoryImpl");
96        SAXParserFactory factory = SAXParserFactory.newInstance();
97        factory.setNamespaceAware(false);
98        factory.setValidating(true);
99        XMLReader xmlReader = factory.newSAXParser().getXMLReader();
100       xmlReader.setContentHandler(this);
101       org.opensync.tools.EntityResolver entityResolver = new org.opensync.tools.EntityResolver();
102       if (new File(fileName+lang+".xml").exists()){
103         entityResolver.setURI(new File(fileName+lang+".xml").getParentFile().toURL().toExternalForm());
104         xmlReader.setEntityResolver(entityResolver);
105         xmlReader.parse(new InputSource(new File(fileName+lang+".xml").toURL().toExternalForm()));
106       }else{
107         entityResolver.setURI(fileName.substring(0,fileName.lastIndexOf("/")+1));
108         xmlReader.setEntityResolver(entityResolver);
109         xmlReader.parse(new InputSource(fileName+lang+".xml"));
110       }
111       //xmlReader.setErrorHandler(this);
112   }
113 
114     public void startElement( String namespaceURI, String localName, String qName, Attributes attr ) throws SAXException {
115 
116       if (qName.equals("CONFIG")) {
117           contents.reset();
118           InfoContent.append("<CONFIG Language=\""+attr.getValue("Language")+"\" TutorialCaption=\""+attr.getValue("TutorialCaption")+"\" FirstTutorial=\""+attr.getValue("FirstTutorial")+"\" SeealsoCaption=\""+attr.getValue("SeealsoCaption")+"\" IntroductionCaption=\""+attr.getValue("IntroductionCaption")+"\" StepCaption=\""+attr.getValue("StepCaption")+"\" DefaultTitle=\""+attr.getValue("DefaultTitle")+"\" DefaultMessage = \""+attr.getValue("DefaultMessage")+"\" HelpListTitle = \""+attr.getValue("HelpListTitle"+"\" TutorialListTitle = \""+attr.getValue("TutorialListTitle"))+"\"/>");
119       }
120 
121       if (qName.equals("PARAGRAPH")) {
122           ParagraphContent.delete(0,ParagraphContent.length());
123           TempKeyword = attr.getValue("KeyWord");
124           if ((TempKeyword.equals(keyword)) || (TempKeyword.equals("default"))) {
125             title   = attr.getValue("Title");
126             myParagraph = true;
127             if (TempKeyword.equals(keyword)){
128               bKeywordFound=true;
129             }
130           }
131        }
132 
133        if (myParagraph == true) {
134           if (qName.equals("ONLINE")) {
135               contents.reset();
136               ParagraphContent.append("<ONLINE>");
137           }
138 
139           if (qName.equals("TUTORIAL")) {
140               contents.reset();
141               ParagraphContent.append("<TUTORIAL KeyWord=\""+attr.getValue("KeyWord")+"\" Title=\""+attr.getValue("Title")+"\">");
142           }
143 
144           if (qName.equals("TEXT")) {
145               contents.reset();
146               ParagraphContent.append("<TEXT>");
147           }
148 
149           if (qName.equals("IMAGE")) {
150               contents.reset();
151               ParagraphContent.append("<IMAGE src = \""+attr.getValue("src")+"\"/>");
152           }
153 
154           if (qName.equals("SEEALSO")) {
155               contents.reset();
156               if (attr.getValue("TutorialKeyWord") == null)
157                 ParagraphContent.append("<SEEALSO ParagraphKeyWord = \""+attr.getValue("ParagraphKeyWord")+"\" Title=\""+attr.getValue("Title")+"\" />");
158               else
159                 ParagraphContent.append("<SEEALSO ParagraphKeyWord = \""+attr.getValue("ParagraphKeyWord")+"\" TutorialKeyWord=\""+attr.getValue("TutorialKeyWord")+"\" Title=\""+attr.getValue("Title")+"\" />");
160           }
161     }
162     }
163 
164    public void endElement( String namespaceURI, String localName, String qName ) throws SAXException {
165 
166           if (myParagraph == true) {
167               if (qName.equals("PARAGRAPH")) {
168              StringBuffer contentTemporaire = new StringBuffer();
169              contentTemporaire.append("<?xml version=\"1.0\" encoding=\"iso-8859-1\" ?><HELPCONTENT>");
170              contentTemporaire.append(InfoContent.toString());
171              contentTemporaire.append("<PARAGRAPH KeyWord=\""+TempKeyword+"\" Title = \""+title+"\"> ");
172              contentTemporaire.append(ParagraphContent.toString());
173              contentTemporaire.append("</PARAGRAPH>");
174              helpContentXML=contentTemporaire.toString();
175              myParagraph = false;
176               }
177 
178 
179               if (qName.equals("ONLINE")) {
180                   ParagraphContent.append(contents.toString());
181                   contents.reset();
182                   ParagraphContent.append("</ONLINE>");
183               }
184 
185               if (qName.equals("TUTORIAL")) {
186                   ParagraphContent.append(contents.toString());
187                   contents.reset();
188                   ParagraphContent.append("</TUTORIAL>");
189               }
190 
191               if (qName.equals("TEXT")) {
192                   ParagraphContent.append(contents.toString());
193                   contents.reset();
194                   ParagraphContent.append("</TEXT>");
195               }
196           }
197    }
198 
199    public void characters( char[] ch, int start, int length ) throws SAXException {
200       contents.write( ch, start, length );
201    }
202 }
203