"Fossies" - the Fresh Open Source Software Archive

Member "OpenSP-1.5.2/lib/Entity.cxx" (21 Jul 2005, 15248 Bytes) of package /linux/misc/old/OpenSP-1.5.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "Entity.cxx" see the Fossies "Dox" file reference documentation.

    1 // Copyright (c) 1994 James Clark
    2 // See the file COPYING for copying permission.
    3 
    4 #ifdef __GNUG__
    5 #pragma implementation
    6 #endif
    7 #include "splib.h"
    8 #include "Entity.h"
    9 #include "ParserState.h"
   10 #include "macros.h"
   11 #include "InternalInputSource.h"
   12 #include "MessageArg.h"
   13 #include "ParserMessages.h"
   14 
   15 #ifdef SP_NAMESPACE
   16 namespace SP_NAMESPACE {
   17 #endif
   18 
   19 Entity::Entity(const StringC &name, DeclType declType, DataType dataType,
   20            const Location &defLocation)
   21 : EntityDecl(name, declType, dataType, defLocation),
   22   used_(0), defaulted_(0)
   23 {
   24 }
   25 
   26 void Entity::generateSystemId(ParserState &)
   27 {
   28 }
   29 
   30 InternalEntity::InternalEntity(const StringC &name,
   31                    DeclType declType,
   32                    DataType dataType,
   33                    const Location &defLocation,
   34                    Text &text)
   35 : Entity(name, declType, dataType, defLocation)
   36 {
   37   text.swap(text_);
   38 }
   39 
   40 PiEntity::PiEntity(const StringC &name, DeclType declType,
   41            const Location &defLocation, Text &text)
   42 : InternalEntity(name, declType, pi, defLocation, text)
   43 {
   44 }
   45 
   46 Entity *PiEntity::copy() const
   47 {
   48   return new PiEntity(*this);
   49 }
   50 
   51 InternalDataEntity::InternalDataEntity(const StringC &name, DataType dataType,
   52                        const Location &defLocation, Text &text)
   53 : InternalEntity(name, generalEntity, dataType, defLocation, text)
   54 {
   55 }
   56 
   57 
   58 InternalCdataEntity::InternalCdataEntity(const StringC &name,
   59                      const Location &defLocation,
   60                      Text &text)
   61 : InternalDataEntity(name, cdata, defLocation, text)
   62 {
   63 }
   64 
   65 Entity *InternalCdataEntity::copy() const
   66 {
   67   return new InternalCdataEntity(*this);
   68 }
   69 
   70 InternalSdataEntity::InternalSdataEntity(const StringC &name,
   71                      const Location &defLocation,
   72                      Text &text)
   73 : InternalDataEntity(name, sdata, defLocation, text)
   74 {
   75 }
   76 
   77 Entity *InternalSdataEntity::copy() const
   78 {
   79   return new InternalSdataEntity(*this);
   80 }
   81 
   82 InternalTextEntity::InternalTextEntity(const StringC &name, DeclType declType,
   83                        const Location &defLocation, Text &text,
   84                        Bracketed bracketed)
   85 : InternalEntity(name, declType, sgmlText, defLocation, text),
   86   bracketed_(bracketed)
   87 {
   88 }
   89 
   90 Entity *InternalTextEntity::copy() const
   91 {
   92   return new InternalTextEntity(*this);
   93 }
   94 
   95 
   96 ExternalEntity::ExternalEntity(const StringC &name,
   97                    DeclType declType,
   98                    DataType dataType,
   99                    const Location &defLocation,
  100                    const ExternalId &id)
  101 : Entity(name, declType, dataType, defLocation), externalId_(id)
  102 {
  103 }
  104 
  105 const ExternalEntity *ExternalEntity::asExternalEntity() const
  106 {
  107   return this;
  108 }
  109 
  110 const StringC *ExternalEntity::systemIdPointer() const
  111 {
  112   return externalId_.systemIdString();
  113 }
  114 
  115 const StringC *ExternalEntity::effectiveSystemIdPointer() const
  116 {
  117   if (externalId_.effectiveSystemId().size() > 0)
  118     return &externalId_.effectiveSystemId();
  119   return 0;
  120 }
  121 
  122 const StringC *ExternalEntity::publicIdPointer() const
  123 {
  124   return externalId_.publicIdString();
  125 }
  126 
  127 void ExternalEntity::generateSystemId(ParserState &parser)
  128 {
  129   StringC str;
  130   if (parser.entityCatalog().lookup(*this,
  131                     parser.syntax(),
  132                     parser.sd().docCharset(),
  133                     parser.messenger(),
  134                     str))
  135     externalId_.setEffectiveSystem(str);
  136   // Don't generate warning when declType == sgml.
  137   else if (externalId_.publicIdString()) {
  138     if (declType() != sgml)
  139       parser.message(ParserMessages::cannotGenerateSystemIdPublic,
  140              StringMessageArg(*externalId_.publicIdString()));
  141   }
  142   else {
  143     switch (declType()) {
  144     case generalEntity:
  145       parser.message(ParserMessages::cannotGenerateSystemIdGeneral,
  146              StringMessageArg(name()));
  147       break;
  148     case parameterEntity:
  149       parser.message(ParserMessages::cannotGenerateSystemIdParameter,
  150              StringMessageArg(name()));
  151       break;
  152     case doctype:
  153       parser.message(ParserMessages::cannotGenerateSystemIdDoctype,
  154              StringMessageArg(name()));
  155       break;
  156     case linktype:
  157       parser.message(ParserMessages::cannotGenerateSystemIdLinktype,
  158              StringMessageArg(name()));
  159       break;
  160     case sgml:
  161       break;
  162     default:
  163       CANNOT_HAPPEN();
  164     }
  165   }
  166 }
  167 
  168 ExternalTextEntity::ExternalTextEntity(const StringC &name,
  169                        DeclType declType,
  170                        const Location &defLocation,
  171                        const ExternalId &id)
  172 : ExternalEntity(name, declType, sgmlText, defLocation, id)
  173 {
  174 }
  175 
  176 Entity *ExternalTextEntity::copy() const
  177 {
  178   return new ExternalTextEntity(*this);
  179 }
  180 
  181 ExternalNonTextEntity::ExternalNonTextEntity(const StringC &name,
  182                          DeclType declType,
  183                          DataType dataType,
  184                          const Location &defLocation,
  185                          const ExternalId &id)
  186 : ExternalEntity(name, declType, dataType, defLocation, id)
  187 {
  188 }
  189 
  190 ExternalDataEntity::ExternalDataEntity(const StringC &name,
  191                        DataType dataType,
  192                        const Location &defLocation,
  193                        const ExternalId &id,
  194                        const ConstPtr<Notation> &nt,
  195                        AttributeList &attributes,
  196                        DeclType declType)
  197 : ExternalNonTextEntity(name, declType, dataType, defLocation, id),
  198   notation_(nt)
  199 {
  200   attributes.swap(attributes_);
  201 }
  202 
  203 void ExternalDataEntity::setNotation(const ConstPtr<Notation> &notation,
  204                      AttributeList &attributes)
  205 {
  206   notation_ = notation;
  207   attributes.swap(attributes_);
  208 }
  209 
  210 Entity *ExternalDataEntity::copy() const
  211 {
  212   return new ExternalDataEntity(*this);
  213 }
  214 
  215 SubdocEntity::SubdocEntity(const StringC &name,
  216                const Location &defLocation,
  217                const ExternalId &id)
  218 : ExternalNonTextEntity(name, generalEntity, subdoc, defLocation, id)
  219 {
  220 }
  221 
  222 Entity *SubdocEntity::copy() const
  223 {
  224   return new SubdocEntity(*this);
  225 }
  226 
  227 Boolean Entity::isDataOrSubdoc() const
  228 {
  229   return 0;
  230 }
  231 
  232 Boolean Entity::isCharacterData() const
  233 {
  234   return 0;
  235 }
  236 
  237 const ExternalEntity *Entity::asExternalEntity() const
  238 {
  239   return 0;
  240 }
  241 
  242 const ExternalDataEntity *Entity::asExternalDataEntity() const
  243 {
  244   return 0;
  245 }
  246 
  247 const SubdocEntity *Entity::asSubdocEntity() const
  248 {
  249   return 0;
  250 }
  251 
  252 const InternalEntity *Entity::asInternalEntity() const
  253 {
  254   return 0;
  255 }
  256 
  257 void Entity::dsReference(ParserState &parser,
  258              const Ptr<EntityOrigin> &origin)
  259      const
  260 {
  261   normalReference(parser, origin, 1);
  262 }
  263 
  264 void Entity::declReference(ParserState &parser,
  265                const Ptr<EntityOrigin> &origin)
  266      const
  267 {
  268   normalReference(parser, origin, 0);
  269   if (parser.currentMarkup())
  270     parser.currentMarkup()->addEntityStart(origin);
  271 }
  272 
  273 void Entity::contentReference(ParserState &parser,
  274                   const Ptr<EntityOrigin> &origin)
  275      const
  276 {
  277   normalReference(parser, origin, 1);
  278 }
  279 
  280 void Entity::rcdataReference(ParserState &parser,
  281                const Ptr<EntityOrigin> &origin)
  282      const
  283 {
  284   normalReference(parser, origin, 1);
  285 }
  286 
  287 void Entity::litReference(Text &, ParserState &parser,
  288               const Ptr<EntityOrigin> &origin,
  289               Boolean)
  290      const
  291 {
  292   normalReference(parser, origin, 0);
  293 }
  294 
  295 const InternalEntity *InternalEntity::asInternalEntity() const
  296 {
  297   return this;
  298 }
  299 
  300 void PiEntity::litReference(Text &, ParserState &parser,
  301                 const Ptr<EntityOrigin> &,
  302                 Boolean) const
  303 {
  304   parser.message(ParserMessages::piEntityReference);
  305 }
  306 
  307 void PiEntity::normalReference(ParserState &parser,
  308                    const Ptr<EntityOrigin> &origin,
  309                    Boolean) const
  310 {
  311   parser.noteMarkup();
  312   parser.eventHandler().pi(new (parser.eventAllocator())
  313                PiEntityEvent(this, origin.pointer()));
  314 }
  315 
  316 void PiEntity::declReference(ParserState &parser,
  317                  const Ptr<EntityOrigin> &) const
  318 {
  319   parser.message(ParserMessages::piEntityReference);
  320 }
  321 
  322 void PiEntity::rcdataReference(ParserState &parser,
  323                    const Ptr<EntityOrigin> &) const
  324 {
  325   parser.message(ParserMessages::piEntityRcdata);
  326 }
  327 
  328 void InternalDataEntity::declReference(ParserState &parser,
  329                        const Ptr<EntityOrigin> &) const
  330 {
  331   parser.message(ParserMessages::internalDataEntityReference);
  332 }
  333 
  334 Boolean InternalDataEntity::isDataOrSubdoc() const
  335 {
  336   return 1;
  337 }
  338 
  339 void InternalCdataEntity::normalReference(ParserState &parser,
  340                       const Ptr<EntityOrigin> &origin,
  341                       Boolean) const
  342 {
  343   checkRef(parser);
  344   checkEntlvl(parser);
  345   if (string().size() > 0) {
  346     parser.noteData();
  347     parser.eventHandler().data(new (parser.eventAllocator())
  348                    CdataEntityEvent(this, origin.pointer()));
  349   }
  350 }
  351 
  352 Boolean InternalCdataEntity::isCharacterData() const
  353 {
  354   return string().size() > 0;
  355 }
  356 
  357 void InternalCdataEntity::litReference(Text &text,
  358                        ParserState &parser,
  359                        const Ptr<EntityOrigin> &origin,
  360                        Boolean squeeze) const
  361 {
  362   checkRef(parser);
  363   checkEntlvl(parser);
  364   if (squeeze) {
  365     Location loc(origin.pointer(), 0);
  366     text.addEntityStart(loc);
  367     text.addCharsTokenize(text_.string(), loc, parser.syntax().space());
  368     loc += text_.size();
  369     text.addEntityEnd(loc);
  370   }
  371   else
  372     text.addCdata(string(), origin.pointer());
  373 }
  374 
  375 
  376 void InternalSdataEntity::normalReference(ParserState &parser,
  377                       const Ptr<EntityOrigin> &origin,
  378                       Boolean) const
  379 {
  380   checkRef(parser);
  381   checkEntlvl(parser);
  382   parser.noteData();
  383   parser.eventHandler().sdataEntity(new (parser.eventAllocator())
  384                     SdataEntityEvent(this,
  385                              origin.pointer()));
  386 }
  387 
  388 Boolean InternalSdataEntity::isCharacterData() const
  389 {
  390   return 1;
  391 }
  392 
  393 void InternalSdataEntity::litReference(Text &text,
  394                        ParserState &parser,
  395                        const Ptr<EntityOrigin> &origin,
  396                        Boolean squeeze) const
  397 {
  398   checkRef(parser);
  399   checkEntlvl(parser);
  400   if (squeeze) {
  401     Location loc(origin.pointer(), 0);
  402     text.addEntityStart(loc);
  403     text.addCharsTokenize(text_.string(), loc, parser.syntax().space());
  404     loc += text_.size();
  405     text.addEntityEnd(loc);
  406   }
  407   else
  408     text.addSdata(string(), origin.pointer());
  409 }
  410 
  411 void InternalTextEntity::normalReference(ParserState &parser,
  412                      const Ptr<EntityOrigin> &origin,
  413                      Boolean generateEvent) const
  414 {
  415   checkRef(parser);
  416   checkEntlvl(parser);
  417   if (checkNotOpen(parser)) {
  418     if (generateEvent && parser.wantMarkup())
  419       parser.eventHandler().entityStart(new (parser.eventAllocator())
  420                     EntityStartEvent(origin));
  421     parser.pushInput(new (parser.internalAllocator())
  422              InternalInputSource(text_.string(), origin.pointer()));
  423   }
  424 }
  425 
  426 void InternalTextEntity::litReference(Text &text,
  427                       ParserState &parser,
  428                       const Ptr<EntityOrigin> &origin,
  429                       Boolean) const
  430 {
  431   text.addEntityStart(Location(origin.pointer(), 0));
  432   normalReference(parser, origin, 0);
  433 }
  434 
  435 void ExternalTextEntity::normalReference(ParserState &parser,
  436                      const Ptr<EntityOrigin> &origin,
  437                      Boolean generateEvent) const
  438 {
  439   checkRef(parser);
  440   checkEntlvl(parser);
  441   if (checkNotOpen(parser)) {
  442     if (generateEvent && parser.wantMarkup())
  443       parser.eventHandler().entityStart(new (parser.eventAllocator())
  444                     EntityStartEvent(origin));
  445     if (externalId().effectiveSystemId().size())
  446       parser.pushInput(parser.entityManager()
  447                .open(externalId().effectiveSystemId(),
  448                  parser.sd().docCharset(),
  449                  origin.pointer(),
  450                  0,
  451                  parser.messenger()));
  452     else
  453       parser.message(ParserMessages::nonExistentEntityRef,
  454              StringMessageArg(name()),
  455              defLocation());
  456   }
  457 }
  458 
  459 void ExternalTextEntity::litReference(Text &text,
  460                       ParserState &parser,
  461                       const Ptr<EntityOrigin> &origin,
  462                       Boolean) const
  463 {
  464   if (parser.options().warnAttributeValueExternalEntityRef
  465       && declType() == generalEntity)
  466     parser.message(ParserMessages::attributeValueExternalEntityRef);
  467   text.addEntityStart(Location(origin.pointer(), 0));
  468   normalReference(parser, origin, 0);
  469 }
  470 
  471 const ExternalDataEntity *ExternalDataEntity::asExternalDataEntity() const
  472 {
  473   return this;
  474 }
  475 
  476 void ExternalDataEntity::contentReference(ParserState &parser,
  477                       const Ptr<EntityOrigin> &origin) const
  478 {
  479   if (parser.options().warnExternalDataEntityRef)
  480     parser.message(ParserMessages::externalDataEntityRef);
  481   checkRef(parser);
  482   checkEntlvl(parser);
  483   parser.noteData();
  484   parser.eventHandler().externalDataEntity(new (parser.eventAllocator())
  485                        ExternalDataEntityEvent(this, origin.pointer()));
  486 }
  487 
  488 Boolean ExternalNonTextEntity::isDataOrSubdoc() const
  489 {
  490   return 1;
  491 }
  492 
  493 Boolean ExternalNonTextEntity::isCharacterData() const
  494 {
  495   return 1;
  496 }
  497 
  498 
  499 void ExternalNonTextEntity::dsReference(ParserState &parser,
  500                     const Ptr<EntityOrigin> &) const
  501 {
  502   parser.message(ParserMessages::dtdDataEntityReference);
  503 }
  504 
  505 void ExternalNonTextEntity::normalReference(ParserState &parser,
  506                         const Ptr<EntityOrigin> &,
  507                         Boolean) const
  508 {
  509   parser.message(ParserMessages::externalNonTextEntityReference);
  510 }
  511 
  512 void ExternalNonTextEntity::litReference(Text &,
  513                      ParserState &parser,
  514                      const Ptr<EntityOrigin> &,
  515                      Boolean) const
  516 {
  517   parser.message(ParserMessages::externalNonTextEntityRcdata);
  518 }
  519 
  520 void ExternalNonTextEntity::rcdataReference(ParserState &parser,
  521                         const Ptr<EntityOrigin> &) const
  522 {
  523   parser.message(ParserMessages::externalNonTextEntityRcdata);
  524 }
  525 
  526 void SubdocEntity::contentReference(ParserState &parser,
  527                     const Ptr<EntityOrigin> &origin) const
  528 {
  529   checkRef(parser);
  530   checkEntlvl(parser);
  531   parser.noteData();
  532   parser.eventHandler().subdocEntity(new (parser.eventAllocator())
  533                      SubdocEntityEvent(this, origin.pointer()));
  534 }
  535 
  536 const SubdocEntity *SubdocEntity::asSubdocEntity() const
  537 {
  538   return this;
  539 }
  540 
  541 IgnoredEntity::IgnoredEntity(const StringC &name, DeclType declType)
  542 : Entity(name, declType, sgmlText, Location())
  543 {
  544 }
  545 
  546 Entity *IgnoredEntity::copy() const
  547 {
  548   return new IgnoredEntity(*this);
  549 }
  550 
  551 void IgnoredEntity::declReference(ParserState &parser,
  552                   const Ptr<EntityOrigin> &origin)
  553      const
  554 {
  555   if (parser.currentMarkup()) {
  556     parser.currentMarkup()->addEntityStart(origin);
  557     parser.currentMarkup()->addEntityEnd();
  558   }
  559 }
  560 
  561 void IgnoredEntity::litReference(Text &text,
  562                  ParserState &,
  563                  const Ptr<EntityOrigin> &origin,
  564                  Boolean) const
  565 {
  566   text.addEntityStart(Location(origin.pointer(), 0));
  567   text.addEntityEnd(Location(origin.pointer(), 0));
  568 }
  569 
  570 void IgnoredEntity::normalReference(ParserState &parser,
  571                     const Ptr<EntityOrigin> &origin,
  572                     Boolean generateEvent) const
  573 {
  574   if (generateEvent && parser.wantMarkup()) {
  575     parser.eventHandler().entityStart(new (parser.eventAllocator())
  576                       EntityStartEvent(origin));
  577     Location loc(origin.pointer(), 0);
  578     parser.eventHandler().entityEnd(new (parser.eventAllocator())
  579                     EntityEndEvent(loc));
  580   }
  581 }
  582 
  583 void Entity::checkEntlvl(ParserState &parser)
  584 {
  585   // -1 because document entity isn't counted
  586   if (parser.inputLevel() - 1 == parser.syntax().entlvl())
  587     parser.message(ParserMessages::entlvl, 
  588                    NumberMessageArg(parser.syntax().entlvl()));
  589 }
  590 
  591 Boolean Entity::checkNotOpen(ParserState &parser) const
  592 {
  593   if (parser.entityIsOpen(this)) {
  594     parser.message(ParserMessages::recursiveEntityReference,
  595            StringMessageArg(name()));
  596     return 0;
  597   }
  598   return 1;
  599 }
  600 
  601 void InternalEntity::checkRef(ParserState &parser) const
  602 {
  603   if (parser.sd().entityRef() == Sd::entityRefNone)
  604     parser.message(ParserMessages::entityRefNone);
  605 }
  606 
  607 void ExternalEntity::checkRef(ParserState &parser) const
  608 {
  609   if (parser.sd().entityRef() != Sd::entityRefAny)
  610     parser.message(ParserMessages::entityRefInternal);
  611 }
  612 
  613 void PredefinedEntity::checkRef(ParserState &parser) const
  614 {
  615 }
  616 
  617 void Entity::checkRef(ParserState &parser) const
  618 {
  619 }
  620 
  621 
  622 #ifdef SP_NAMESPACE
  623 }
  624 #endif