~ubuntu-branches/ubuntu/trusty/scilab/trusty

« back to all changes in this revision

Viewing changes to modules/helptools/src/java/org/scilab/modules/helptools/DocbookTagConverter.java

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2012-08-02 11:02:49 UTC
  • mfrom: (1.4.6)
  • Revision ID: package-import@ubuntu.com-20120802110249-0v5953emkp25geuz
Tags: 5.4.0-beta-2-1~exp1
* New upstream release
* Remove libscilab-java (remove upstream). Use libscilab2-java instead.

Show diffs side-by-side

added added

removed removed

Lines of Context:
33
33
import org.xml.sax.helpers.DefaultHandler;
34
34
 
35
35
import org.scilab.modules.helptools.external.ExternalXMLHandler;
 
36
import org.scilab.modules.helptools.external.HTMLScilabHandler;
36
37
 
37
38
/**
38
39
 * Class the convert a DocBook xml file
41
42
public class DocbookTagConverter extends DefaultHandler {
42
43
 
43
44
    private static final String DOCBOOKURI = "http://docbook.org/ns/docbook";
44
 
    private static final Class[] argsType = new Class[]{Map.class, String.class};
 
45
    private static final Class[] argsType = new Class[] {Map.class, String.class};
45
46
 
46
47
    private Map<String, Method> mapMeth = new HashMap();
47
48
    private Map<String, ExternalXMLHandler> externalHandlers = new HashMap();
48
49
    private List<DocbookTagConverter> converters;
49
50
    private final File in;
50
 
    private DocbookElement baseElement = new DocbookElement(null, null);
 
51
    private DocbookElement baseElement = new DocbookElement(null, null, null);
51
52
    private Stack<DocbookElement> stack = new Stack();
52
53
    private String errors = "";
53
54
 
109
110
                }
110
111
            }
111
112
            try {
112
 
                return (String) method.invoke(this, new Object[]{attributes, contents});
 
113
                return (String) method.invoke(this, new Object[] {attributes, contents});
113
114
            } catch (IllegalAccessException e) {
114
115
                throw new UnhandledDocbookTagException(tag);
115
116
            } catch (InvocationTargetException e) {
120
121
        }
121
122
    }
122
123
 
 
124
    public String getCurrentFileName() {
 
125
        return currentFileName;
 
126
    }
 
127
 
123
128
    /**
124
129
     * Register an Docbook tag converter. The aim is to parse the xml one time.
125
130
     * @param c the converter to register
160
165
    public void registerExternalXMLHandler(ExternalXMLHandler h) {
161
166
        if (externalHandlers.get(h.getURI()) == null) {
162
167
            externalHandlers.put(h.getURI(), h);
 
168
            h.setConverter(this);
163
169
        }
164
170
    }
165
171
 
167
173
     * @param tagName the tag name
168
174
     * @return true if the contents of the tag must be escaped
169
175
     */
170
 
    public boolean isEscapable(String tagName) {
 
176
    public boolean isEscapable(String tagName, String uri) {
171
177
        return true;
172
178
    }
173
179
 
225
231
     */
226
232
    public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
227
233
        currentFileName = systemId;
 
234
        HTMLScilabHandler.getInstance().resetCompt();
228
235
        if (converters != null) {
229
236
            for (DocbookTagConverter conv : converters) {
230
237
                conv.resolveEntity(publicId, systemId);
238
245
     * {@inheritDoc}
239
246
     */
240
247
    public void startDocument() throws SAXException {
241
 
        stack.push(baseElement.getNewInstance(null, null));
 
248
        stack.push(baseElement.getNewInstance(null, null, null));
242
249
        if (converters != null) {
243
250
            for (DocbookTagConverter conv : converters) {
244
251
                conv.startDocument();
269
276
            for (int i = 0; i < len; i++) {
270
277
                map.put(attributes.getLocalName(i), attributes.getValue(i));
271
278
            }
272
 
            stack.push(baseElement.getNewInstance(localName, map));
 
279
            stack.push(baseElement.getNewInstance(localName, uri, map));
273
280
        } else {
274
281
            ExternalXMLHandler h = externalHandlers.get(uri);
275
282
            if (h == null) {
278
285
            }
279
286
            StringBuilder buf = h.startExternalXML(localName, attributes);
280
287
            if (buf != null) {
281
 
                DocbookElement elem = baseElement.getNewInstance(localName, null);
 
288
                DocbookElement elem = baseElement.getNewInstance(localName, uri, null);
282
289
                elem.setStringBuilder(buf);
283
290
                stack.push(elem);
284
291
            }
342
349
    public void characters(char[] ch, int start, int length) throws SAXException {
343
350
        int end = start + length;
344
351
 
345
 
        if (isEscapable(stack.peek().getName())) {
 
352
        if (isEscapable(stack.peek().getName(), stack.peek().getURI())) {
346
353
            StringBuilder buf = stack.peek().getStringBuilder();
347
354
            int save = start;
348
355
            for (int i = start; i < end; i++) {
349
356
                switch (ch[i]) {
350
 
                case '\'' :
351
 
                    buf.append(ch, save, i - save);
352
 
                    buf.append("&#0039;");
353
 
                    save = i + 1;
354
 
                    break;
355
 
                case '\"' :
356
 
                    buf.append(ch, save, i - save);
357
 
                    buf.append("&#0034;");
358
 
                    save = i + 1;
359
 
                    break;
360
 
                case '<' :
361
 
                    buf.append(ch, save, i - save);
362
 
                    buf.append("&lt;");
363
 
                    save = i + 1;
364
 
                    break;
365
 
                case '>' :
366
 
                    buf.append(ch, save, i - save);
367
 
                    buf.append("&gt;");
368
 
                    save = i + 1;
369
 
                    break;
370
 
                case '&' :
371
 
                    buf.append(ch, save, i - save);
372
 
                    buf.append("&amp;");
373
 
                    save = i + 1;
374
 
                    break;
375
 
                default :
376
 
                    break;
 
357
                    case '\'' :
 
358
                        buf.append(ch, save, i - save);
 
359
                        buf.append("&#0039;");
 
360
                        save = i + 1;
 
361
                        break;
 
362
                    case '\"' :
 
363
                        buf.append(ch, save, i - save);
 
364
                        buf.append("&#0034;");
 
365
                        save = i + 1;
 
366
                        break;
 
367
                    case '<' :
 
368
                        buf.append(ch, save, i - save);
 
369
                        buf.append("&lt;");
 
370
                        save = i + 1;
 
371
                        break;
 
372
                    case '>' :
 
373
                        buf.append(ch, save, i - save);
 
374
                        buf.append("&gt;");
 
375
                        save = i + 1;
 
376
                        break;
 
377
                    case '&' :
 
378
                        buf.append(ch, save, i - save);
 
379
                        buf.append("&amp;");
 
380
                        save = i + 1;
 
381
                        break;
 
382
                    default :
 
383
                        break;
377
384
                }
378
385
            }
379
386
 
399
406
    }
400
407
 
401
408
    /**
 
409
     * @return the document locator
 
410
     */
 
411
    public Locator getDocumentLocator() {
 
412
        return locator;
 
413
    }
 
414
 
 
415
    /**
402
416
     * @return the used stack
403
417
     */
404
418
    protected Stack<DocbookElement> getStack() {
419
433
     * @param e the exception to handle
420
434
     * @throws SAXException if problem
421
435
     */
422
 
    protected void exceptionOccured(Exception e){
 
436
    protected void exceptionOccured(Exception e) {
423
437
        if (!hasError) {
424
438
            hasError = true;
425
439
        }