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 Tutorial extends DefaultHandler{
18      private CharArrayWriter contents;
19      private StringBuffer InfoContent;
20      private StringBuffer ParagraphContent;
21      private String tutorialContentXML;
22      private String keyword;
23      private String keywordTutorial;
24      private String title;
25      private boolean myParagraph;
26      private boolean myTutorial;
27      private String fileName;
28  
29    /***
30     * Constructor
31     * pFileName = Chemin + Nom du fichier d'aide
32     * Tutorial toto = new Tutorial("file:///m:/jakarta-tomcat-3.2.2/webapps/SophiewebServlet/SOPHIETUTORIAL.xml");
33     */
34    public Tutorial(String pFileName) {
35      fileName=pFileName;
36    }
37  
38    public String getParagraph(String lang,String pKeyWord, String pKeyWordTutorial) 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      keywordTutorial=pKeyWordTutorial;
47      myParagraph=false;
48      processTutorialXml(lang);
49  	tutorialContentXML+="<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>";
50      return tutorialContentXML;
51    }
52  
53    public void processTutorialXml(String lang) throws IOException,FileNotFoundException,SAXException,ParserConfigurationException {
54  
55        contents = new CharArrayWriter();
56        ParagraphContent = new StringBuffer();
57        InfoContent = new StringBuffer();
58  
59        /*
60        XMLReader xr = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
61        xr.setContentHandler( this );
62        //xr.parse(  new InputSource(new FileInputStream(fileName+lang+".xml")));
63        //PV 05-03-2002 because of weblogic 6.1 war deployement...
64  	   xr.parse( new InputSource(fileName+lang+".xml"));
65        */
66  
67         /* JAXP */
68        // If a validating parsing is needed, then Piccolo will use the following property to create
69        // a validating parser: here, we specify Xerces.
70        System.setProperty("com.bluecast.xml.ValidatingSAXParserFactory", "org.apache.xerces.jaxp.SAXParserFactoryImpl");
71        SAXParserFactory factory = SAXParserFactory.newInstance();
72        factory.setNamespaceAware(false);
73        factory.setValidating(true);
74        XMLReader xmlReader = factory.newSAXParser().getXMLReader();
75        xmlReader.setContentHandler(this);
76        org.opensync.tools.EntityResolver entityResolver = new org.opensync.tools.EntityResolver();
77        if (new File(fileName+lang+".xml").exists()){
78          entityResolver.setURI(new File(fileName+lang+".xml").getParentFile().toURL().toExternalForm());
79          xmlReader.setEntityResolver(entityResolver);
80          //xmlReader.setErrorHandler(this);
81          xmlReader.parse(new InputSource(new File(fileName+lang+".xml").toURL().toExternalForm()));
82        }else{
83          entityResolver.setURI(fileName.substring(0,fileName.lastIndexOf("/")+1));
84          xmlReader.setEntityResolver(entityResolver);
85          xmlReader.parse(new InputSource(fileName+lang+".xml"));
86        }
87  
88    }
89  
90    public void startElement( String namespaceURI, String localName, String qName, Attributes attr ) throws SAXException {
91  
92        if (qName.equals("CONFIG")) {
93            contents.reset();
94            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"))+"\"/>");
95        }
96  
97        if (qName.equals("PARAGRAPH")) {
98            ParagraphContent.delete(0,ParagraphContent.length());
99            String tempKeyword = attr.getValue("KeyWord");
100           if ((tempKeyword.equals(keyword)) || (tempKeyword.equals("default"))) {
101             title = attr.getValue("Title");
102             myParagraph = true;
103           }
104        }
105 
106        if (myParagraph == true) {
107           if (qName.equals("TUTORIAL")) {
108             String tempKeyword = attr.getValue("KeyWord");
109             if (tempKeyword.equals(keywordTutorial)) {
110               contents.reset();
111               ParagraphContent.append("<TUTORIAL KeyWord=\""+attr.getValue("KeyWord")+"\" Title=\""+attr.getValue("Title")+"\">");
112               myTutorial = true;
113             }
114           }
115 
116           if (myTutorial == true) {
117             if (qName.equals("TEXT")) {
118                 contents.reset();
119                 ParagraphContent.append("<TEXT>");
120             }
121 
122             if (qName.equals("INTRODUCTION")) {
123                 contents.reset();
124                 ParagraphContent.append("<INTRODUCTION>");
125             }
126 
127             if (qName.equals("STEP")) {
128                 contents.reset();
129                 ParagraphContent.append("<STEP>");
130             }
131 
132             if (qName.equals("IMAGE")) {
133                 contents.reset();
134                 ParagraphContent.append("<IMAGE src = \""+attr.getValue("src")+"\"/>");
135             }
136 
137             if (qName.equals("SEEALSO")) {
138                 contents.reset();
139                 if (attr.getValue("TutorialKeyWord") == null)
140                   ParagraphContent.append("<SEEALSO ParagraphKeyWord = \""+attr.getValue("ParagraphKeyWord")+"\" Title=\""+attr.getValue("Title")+"\" />");
141                 else
142                   ParagraphContent.append("<SEEALSO ParagraphKeyWord = \""+attr.getValue("ParagraphKeyWord")+"\" TutorialKeyWord=\""+attr.getValue("TutorialKeyWord")+"\" Title=\""+attr.getValue("Title")+"\" />");
143             }
144           }
145        }
146     }
147    public void endElement( String namespaceURI, String localName, String qName ) throws SAXException {
148 
149           if (myParagraph == true) {
150               if (qName.equals("PARAGRAPH")) {
151                   StringBuffer contentTemporaire = new StringBuffer();
152                   contentTemporaire.append("<?xml version=\"1.0\" encoding=\"iso-8859-1\" ?><HELPCONTENT>");
153                   contentTemporaire.append(InfoContent.toString());
154                   contentTemporaire.append("<PARAGRAPH KeyWord=\""+keyword+"\" Title = \""+title+"\"> ");
155                   contentTemporaire.append(ParagraphContent.toString());
156                   contentTemporaire.append("</PARAGRAPH>");
157                   tutorialContentXML=contentTemporaire.toString();
158                   myParagraph = false;
159               }
160 
161               if (myTutorial == true) {
162                   if (qName.equals("TUTORIAL")) {
163                       myTutorial = false;
164                       ParagraphContent.append(contents.toString());
165                       contents.reset();
166                       ParagraphContent.append("</TUTORIAL>");
167                   }
168 
169                   if (qName.equals("TEXT")) {
170                       ParagraphContent.append(contents.toString());
171                       contents.reset();
172                       ParagraphContent.append("</TEXT>");
173                   }
174 
175                   if (qName.equals("INTRODUCTION")) {
176                       ParagraphContent.append(contents.toString());
177                       contents.reset();
178                       ParagraphContent.append("</INTRODUCTION>");
179                   }
180 
181                   if (qName.equals("STEP")) {
182                       ParagraphContent.append(contents.toString());
183                       contents.reset();
184                       ParagraphContent.append("</STEP>");
185                   }
186               }
187           }
188    }
189 
190    public void characters( char[] ch, int start, int length ) throws SAXException {
191       contents.write( ch, start, length );
192    }
193 }
194