hotspot/src/share/vm/utilities/xmlstream.hpp
changeset 1 489c9b5090e2
child 5547 f4b087cbb361
equal deleted inserted replaced
0:fd16c54261b3 1:489c9b5090e2
       
     1 /*
       
     2  * Copyright 2002-2005 Sun Microsystems, Inc.  All Rights Reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    21  * have any questions.
       
    22  *
       
    23  */
       
    24 
       
    25 class xmlStream;
       
    26 class defaultStream;
       
    27 
       
    28 // Sub-stream for writing quoted text, as opposed to markup.
       
    29 // Characters written to this stream are subject to quoting,
       
    30 // as '<' => "&lt;", etc.
       
    31 class xmlTextStream : public outputStream {
       
    32   friend class xmlStream;
       
    33   friend class defaultStream; // tty
       
    34  private:
       
    35 
       
    36   xmlStream* _outer_xmlStream;
       
    37 
       
    38   xmlTextStream() { _outer_xmlStream = NULL; }
       
    39 
       
    40  public:
       
    41    virtual void flush(); // _outer.flush();
       
    42    virtual void write(const char* str, size_t len); // _outer->write_text()
       
    43 };
       
    44 
       
    45 
       
    46 // Output stream for writing XML-structured logs.
       
    47 // To write markup, use special calls elem, head/tail, etc.
       
    48 // Use the xmlStream::text() stream to write unmarked text.
       
    49 // Text written that way will be quoted as necessary using '&lt;', etc.
       
    50 // Characters written directly to an xmlStream via print_cr, etc.,
       
    51 // are directly written to the encapsulated stream, xmlStream::out().
       
    52 // This can be used to produce markup directly, character by character.
       
    53 // (Such writes are not checked for markup syntax errors.)
       
    54 
       
    55 class xmlStream : public outputStream {
       
    56   friend class defaultStream; // tty
       
    57  public:
       
    58   enum MarkupState { BODY,       // after end_head() call, in text
       
    59                      HEAD,       // after begin_head() call, in attrs
       
    60                      ELEM };     // after begin_elem() call, in attrs
       
    61 
       
    62  protected:
       
    63   outputStream* _out;            // file stream by which it goes
       
    64   julong        _last_flush;     // last position of flush
       
    65   MarkupState   _markup_state;   // where in the elem/head/tail dance
       
    66   outputStream* _text;           // text stream
       
    67   xmlTextStream _text_init;
       
    68 
       
    69   // for subclasses
       
    70   xmlStream() {}
       
    71   void initialize(outputStream* out);
       
    72 
       
    73   // protect this from public use:
       
    74   outputStream* out()                            { return _out; }
       
    75 
       
    76   // helpers for writing XML elements
       
    77   void          va_tag(bool push, const char* format, va_list ap);
       
    78   virtual void see_tag(const char* tag, bool push) NOT_DEBUG({});
       
    79   virtual void pop_tag(const char* tag) NOT_DEBUG({});
       
    80 
       
    81 #ifdef ASSERT
       
    82   // in debug mode, we verify matching of opening and closing tags
       
    83   int   _element_depth;              // number of unfinished elements
       
    84   char* _element_close_stack_high;   // upper limit of down-growing stack
       
    85   char* _element_close_stack_low;    // upper limit of down-growing stack
       
    86   char* _element_close_stack_ptr;    // pointer of down-growing stack
       
    87 #endif
       
    88 
       
    89  public:
       
    90   // creation
       
    91   xmlStream(outputStream* out) { initialize(out); }
       
    92   DEBUG_ONLY(virtual ~xmlStream();)
       
    93 
       
    94   bool is_open() { return _out != NULL; }
       
    95 
       
    96   // text output
       
    97   bool inside_attrs() { return _markup_state != BODY; }
       
    98 
       
    99   // flushing
       
   100   virtual void flush();  // flushes out, sets _last_flush = count()
       
   101   virtual void write(const char* s, size_t len);
       
   102   void    write_text(const char* s, size_t len);  // used by xmlTextStream
       
   103   int unflushed_count() { return (int)(out()->count() - _last_flush); }
       
   104 
       
   105   // writing complete XML elements
       
   106   void          elem(const char* format, ...);
       
   107   void    begin_elem(const char* format, ...);
       
   108   void      end_elem(const char* format, ...);
       
   109   void      end_elem();
       
   110   void          head(const char* format, ...);
       
   111   void    begin_head(const char* format, ...);
       
   112   void      end_head(const char* format, ...);
       
   113   void      end_head();
       
   114   void          done(const char* format, ...);  // xxx_done event, plus tail
       
   115   void          done_raw(const char * kind);
       
   116   void          tail(const char* kind);
       
   117 
       
   118   // va_list versions
       
   119   void       va_elem(const char* format, va_list ap);
       
   120   void va_begin_elem(const char* format, va_list ap);
       
   121   void       va_head(const char* format, va_list ap);
       
   122   void va_begin_head(const char* format, va_list ap);
       
   123   void       va_done(const char* format, va_list ap);
       
   124 
       
   125   // write text (with quoting of special XML characters <>&'" etc.)
       
   126   outputStream* text() { return _text; }
       
   127   void          text(const char* format, ...);
       
   128   void       va_text(const char* format, va_list ap) {
       
   129     text()->vprint(format, ap);
       
   130   }
       
   131 
       
   132   // commonly used XML attributes
       
   133   void          stamp();                 // stamp='1.234'
       
   134   void          method(methodHandle m);  // method='k n s' ...
       
   135   void          klass(KlassHandle k);    // klass='name'
       
   136   void          name(symbolHandle s);    // name='name'
       
   137   void          object(const char* attr, Handle val);
       
   138 
       
   139   // print the text alone (sans ''):
       
   140   void          method_text(methodHandle m);
       
   141   void          klass_text(KlassHandle k);    // klass='name'
       
   142   void          name_text(symbolHandle s);    // name='name'
       
   143   void          object_text(Handle x);
       
   144 
       
   145   /*  Example uses:
       
   146 
       
   147       // Empty element, simple case.
       
   148       elem("X Y='Z'");          <X Y='Z'/> \n
       
   149 
       
   150       // Empty element, general case.
       
   151       begin_elem("X Y='Z'");    <X Y='Z'
       
   152       ...attrs...               ...attrs...
       
   153       end_elem();               />
       
   154 
       
   155       // Compound element, simple case.
       
   156       head("X Y='Z'");          <X Y='Z'> \n
       
   157       ...body...                ...body...
       
   158       tail("X");                </X> \n
       
   159 
       
   160       // Compound element, general case.
       
   161       begin_head("X Y='Z'");    <X Y='Z'
       
   162       ...attrs...               ...attrs...
       
   163       end_head();               > \n
       
   164       ...body...                ...body...
       
   165       tail("X");                </X> \n
       
   166 
       
   167       // Printf-style formatting:
       
   168       elem("X Y='%s'", "Z");    <X Y='Z'/> \n
       
   169 
       
   170    */
       
   171 
       
   172 };
       
   173 
       
   174 // Standard log file, null if no logging is happening.
       
   175 extern xmlStream* xtty;
       
   176 
       
   177 // Note:  If ::xtty != NULL, ::tty == ::xtty->text().