IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

AWT/Swing Java Discussion :

Swing et Class DocumentParser


Sujet :

AWT/Swing Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre éclairé
    Profil pro
    Inscrit en
    Octobre 2002
    Messages
    209
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2002
    Messages : 209
    Par défaut Swing et Class DocumentParser
    Bonjour
    Je remarque que dans la classe DocumentParser se trouve la méthode parse qui posséde comme signature

    parse(Reader in, HTMLEditorKit.ParserCallback callback, boolean ignoreCharSet)

    le premier paramètre est le document a parser
    le second paramètre est une instance du parseur
    le troisième paramètre un boolean ?????
    Ma question a quoi sert ce paramètre null part dans la doc de sun je ne trouve sa définition ( contrairement aux autres Api ou la définition des paramètres est notée )
    Pour utiliser cette méthode que dois-je mettre False ou True ???
    Merci de bien vouloir m'éclairer sur ce point

  2. #2
    Expert confirmé
    Avatar de sinok
    Profil pro
    Inscrit en
    Août 2004
    Messages
    8 765
    Détails du profil
    Informations personnelles :
    Âge : 45
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Août 2004
    Messages : 8 765
    Par défaut
    Bah en même temps le nom est plus qu'explicite, is s'agit d'un flag permettant de dire au Parser de prendre en compte le charset (aka encodage, cad UTF-8, ISO8859-1 &cie) du document (si false), ou de ne pas le faire dans le cas contraire (je suppose que dans le cas contraire il va tout faire passer pour de l'ascii ou de l'encodage par défaut en UTF-16).

    Le plus simple serait de regarder le code de la méthode, dans la méthode celà ne sert qu'à remplir un attribut de la classe.

    Puis dans une méthode appelée (indirectement par une méthode de la classe mère), suivant la valeur de cet attribut java vérifier le contentType du document, vérifie que le charset du dosument est bien celui annoncé.

    Code de la classe DocumentParser

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    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
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
     
     
    /*
     * @(#)DocumentParser.java    1.29 05/11/17
     *
     * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
     * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
     */
     
    package javax.swing.text.html.parser;
     
    import javax.swing.text.SimpleAttributeSet;
    import javax.swing.text.html.HTMLEditorKit;
    import javax.swing.text.html.HTML;
    import javax.swing.text.ChangedCharSetException;
     
    import java.util.*;
    import java.io.*;
    import java.net.*;
     
    /**
     * A Parser for HTML Documents (actually, you can specify a DTD, but
     * you should really only use this class with the html dtd in swing).
     * Reads an InputStream of HTML and
     * invokes the appropriate methods in the ParserCallback class. This
     * is the default parser used by HTMLEditorKit to parse HTML url's.
     * <p>This will message the callback for all valid tags, as well as
     * tags that are implied but not explicitly specified. For example, the
     * html string (&lt;p&gt;blah) only has a p tag defined. The callback
     * will see the following methods:
     * <ol><li><i>handleStartTag(html, ...)</i></li>
     *     <li><i>handleStartTag(head, ...)</i></li>
     *     <li><i>handleEndTag(head)</i></li>
     *     <li><i>handleStartTag(body, ...)</i></li>
     *     <li>handleStartTag(p, ...)</i></li>
     *     <li>handleText(...)</li>
     *     <li><i>handleEndTag(p)</i></li>
     *     <li><i>handleEndTag(body)</i></li>
     *     <li><i>handleEndTag(html)</i></li>
     * </ol>
     * The items in <i>italic</i> are implied, that is, although they were not
     * explicitly specified, to be correct html they should have been present
     * (head isn't necessary, but it is still generated). For tags that
     * are implied, the AttributeSet argument will have a value of
     * <code>Boolean.TRUE</code> for the key
     * <code>HTMLEditorKit.ParserCallback.IMPLIED</code>.
     * <p>HTML.Attributes defines a type safe enumeration of html attributes.
     * If an attribute key of a tag is defined in HTML.Attribute, the
     * HTML.Attribute will be used as the key, otherwise a String will be used.
     * For example &lt;p foo=bar class=neat&gt; has two attributes. foo is
     * not defined in HTML.Attribute, where as class is, therefore the
     * AttributeSet will have two values in it, HTML.Attribute.CLASS with
     * a String value of 'neat' and the String key 'foo' with a String value of
     * 'bar'.
     * <p>The position argument will indicate the start of the tag, comment
     * or text. Similiar to arrays, the first character in the stream has a
     * position of 0. For tags that are
     * implied the position will indicate
     * the location of the next encountered tag. In the first example,
     * the implied start body and html tags will have the same position as the
     * p tag, and the implied end p, html and body tags will all have the same
     * position.
     * <p>As html skips whitespace the position for text will be the position
     * of the first valid character, eg in the string '\n\n\nblah'
     * the text 'blah' will have a position of 3, the newlines are skipped.
     * <p>
     * For attributes that do not have a value, eg in the html
     * string <code>&lt;foo blah&gt;</code> the attribute <code>blah</code>
     * does not have a value, there are two possible values that will be
     * placed in the AttributeSet's value:
     * <ul>
     * <li>If the DTD does not contain an definition for the element, or the
     *     definition does not have an explicit value then the value in the
     *     AttributeSet will be <code>HTML.NULL_ATTRIBUTE_VALUE</code>.
     * <li>If the DTD contains an explicit value, as in:
     *     <code>&lt;!ATTLIST OPTION selected (selected) #IMPLIED&gt;</code>
     *     this value from the dtd (in this case selected) will be used.
     * </ul>
     * <p>
     * Once the stream has been parsed, the callback is notified of the most
     * likely end of line string. The end of line string will be one of
     * \n, \r or \r\n, which ever is encountered the most in parsing the
     * stream.
     *
     * @version     1.29 11/17/05
     * @author      Sunita Mani
     */
    public class DocumentParser extends javax.swing.text.html.parser.Parser {
     
        private int inbody;
        private int intitle;
        private int inhead;
        private int instyle;
        private int inscript;
        private boolean seentitle;
        private HTMLEditorKit.ParserCallback callback = null;
        private boolean ignoreCharSet = false;
        private static final boolean debugFlag = false;
     
        public DocumentParser(DTD dtd) {
        super(dtd);
        }
     
        public void parse(Reader in,  HTMLEditorKit.ParserCallback callback, boolean ignoreCharSet) throws IOException {
        this.ignoreCharSet = ignoreCharSet;
        this.callback = callback;
        parse(in);
        // end of line
        callback.handleEndOfLineString(getEndOfLineString());
        }
     
        /**
         * Handle Start Tag.
         */
        protected void handleStartTag(TagElement tag) {
     
        Element elem = tag.getElement();
        if (elem == dtd.body) {
            inbody++;
        } else if (elem == dtd.html) {
        } else if (elem == dtd.head) {
            inhead++;
        } else if (elem == dtd.title) {
            intitle++;
        } else if (elem == dtd.style) {
            instyle++;
        } else if (elem == dtd.script) {
                inscript++;
        }    
        if (debugFlag) {
            if (tag.fictional()) {
            debug("Start Tag: " + tag.getHTMLTag() + " pos: " + getCurrentPos());
            } else {
            debug("Start Tag: " + tag.getHTMLTag() + " attributes: " + 
                  getAttributes() + " pos: " + getCurrentPos());
            }
        }
        if (tag.fictional()) {
            SimpleAttributeSet attrs = new SimpleAttributeSet();
            attrs.addAttribute(HTMLEditorKit.ParserCallback.IMPLIED,
                       Boolean.TRUE);
            callback.handleStartTag(tag.getHTMLTag(), attrs,
                        getBlockStartPosition());
        } else {
            callback.handleStartTag(tag.getHTMLTag(), getAttributes(),
                        getBlockStartPosition());
            flushAttributes();
        }
        }
     
     
        protected void handleComment(char text[]) {
        if (debugFlag) {
            debug("comment: ->" + new String(text) + "<-"
              + " pos: " + getCurrentPos());
        }
        callback.handleComment(text, getBlockStartPosition());
        }
     
        /**
         * Handle Empty Tag.
         */
        protected void handleEmptyTag(TagElement tag) throws ChangedCharSetException {
     
        Element elem = tag.getElement();
        if (elem == dtd.meta && !ignoreCharSet) {
            SimpleAttributeSet atts = getAttributes();
            if (atts != null) {
            String content = (String)atts.getAttribute(HTML.Attribute.CONTENT);
            if (content != null) {
                if ("content-type".equalsIgnoreCase((String)atts.getAttribute(HTML.Attribute.HTTPEQUIV))) {
                if (!content.equalsIgnoreCase("text/html") && 
                    !content.equalsIgnoreCase("text/plain")) {
                    throw new ChangedCharSetException(content, false);
                }
                } else if ("charset" .equalsIgnoreCase((String)atts.getAttribute(HTML.Attribute.HTTPEQUIV))) {
                throw new ChangedCharSetException(content, true);
                }
            }
            }
        }
        if (inbody != 0 || elem == dtd.meta || elem == dtd.base || elem == dtd.isindex || elem == dtd.style || elem == dtd.link) {
            if (debugFlag) {
            if (tag.fictional()) {
                debug("Empty Tag: " + tag.getHTMLTag() + " pos: " + getCurrentPos());
            } else {
                debug("Empty Tag: " + tag.getHTMLTag() + " attributes: " 
                  + getAttributes() + " pos: " + getCurrentPos());
            }
            }
            if (tag.fictional()) {
            SimpleAttributeSet attrs = new SimpleAttributeSet();
            attrs.addAttribute(HTMLEditorKit.ParserCallback.IMPLIED,
                       Boolean.TRUE);
            callback.handleSimpleTag(tag.getHTMLTag(), attrs,
                         getBlockStartPosition());
            } else {
            callback.handleSimpleTag(tag.getHTMLTag(), getAttributes(),
                         getBlockStartPosition());
            flushAttributes();
            }
        }
        }
     
        /**
         * Handle End Tag.
         */
        protected void handleEndTag(TagElement tag) {
        Element elem = tag.getElement();
        if (elem == dtd.body) {
            inbody--;
        } else if (elem == dtd.title) {
            intitle--;
            seentitle = true;
        } else if (elem == dtd.head) {
                inhead--;
        } else if (elem == dtd.style) {
                instyle--;
        } else if (elem == dtd.script) {
                inscript--;
        }
        if (debugFlag) {
            debug("End Tag: " + tag.getHTMLTag() + " pos: " + getCurrentPos());
        }
        callback.handleEndTag(tag.getHTMLTag(), getBlockStartPosition());
     
        }
     
        /**
         * Handle Text.
         */
        protected void handleText(char data[]) {
        if (data != null) {
            if (inscript != 0) {
            callback.handleComment(data, getBlockStartPosition());
            return;
            }
            if (inbody != 0 || ((instyle != 0) ||
                    ((intitle != 0) && !seentitle))) {
            if (debugFlag) {
                debug("text:  ->" + new String(data) + "<-" + " pos: " + getCurrentPos());
            }
            callback.handleText(data, getBlockStartPosition());
            }
        }
        }
     
        /*
         * Error handling.
         */
        protected void handleError(int ln, String errorMsg) {
        if (debugFlag) {
            debug("Error: ->" + errorMsg + "<-" + " pos: " + getCurrentPos());
        }
        /* PENDING: need to improve the error string. */
        callback.handleError(errorMsg, getCurrentPos());
        }
     
     
        /*
         * debug messages
         */
        private void debug(String msg) {
        System.out.println(msg);
        }
    }

    Par contre ce que je me demande c'est pourquoi tu es en train de fouiller du coté de cette classe? Quel est le besoin derrière. Car en général ce n'est pas trop le genre de choses dans auquel il est nécessaire de porter intérêt. Et si tu cherches un Parser HTML, il y a certainement mieux sur le marché open source...

  3. #3
    Membre éclairé
    Profil pro
    Inscrit en
    Octobre 2002
    Messages
    209
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2002
    Messages : 209
    Par défaut
    Merci pour cette réponse rapide
    Le but de ma question :
    je suis entrain d'étudier le programme TreeTableExample3.java - a l'adresse suivante http://java.sun.com/products/jfc/tsc...eExample3.java
    et dans le fichier Bookmarks.java - celui qui crée l'arbre il est fait appel a la méthode
    new ParserDelegator().parse(reader, new CallbackHandler(), true);
    Le boolean passé a la valeur True
    je me demandais pourquoi cette valeur et quelle était sa signification
    Voila c'est pour comprendre le pourquoi de la chose !!!!
    Merci encore

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. interaction swing et classe
    Par phobos64 dans le forum Agents de placement/Fenêtres
    Réponses: 6
    Dernier message: 11/11/2008, 16h28
  2. Réponses: 5
    Dernier message: 11/09/2007, 11h32
  3. [Swing]Affichage classe extends JPanel
    Par Quentin D dans le forum AWT/Swing
    Réponses: 3
    Dernier message: 03/08/2007, 12h58
  4. Classe EventHandler / SWING
    Par X...FoR...X dans le forum AWT/Swing
    Réponses: 1
    Dernier message: 15/01/2007, 10h18
  5. [GEF]class Figure dans container SWING ?
    Par Albarad dans le forum Eclipse Java
    Réponses: 2
    Dernier message: 01/06/2004, 12h12

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo