Example #1
0
void ATemplateNodeHandler_DADA::_loadStaticData()
{
  AXmlElement::CONST_CONTAINER nodes;

  m_Services.useConfiguration().getConfigRoot().find("AOS_DadaData/dataset", nodes);
  AXmlElement::CONST_CONTAINER::iterator it;
  for (it = nodes.begin(); it != nodes.end(); ++it)
  {
    AString strSet;
    (*it)->getAttributes().get(ASW("name",4), strSet);
    if (strSet.isEmpty())
      ATHROW_EX(*it, AException::InvalidData, ASWNL("AOS_DadaData/dataset missing 'name' parameter"));

    ADadaDataHolder *pddh = new ADadaDataHolder();
    pddh->readData(m_Services, *it);
    m_Objects.insert(strSet, pddh, true);
  }

  nodes.clear();
  m_Services.useConfiguration().getConfigRoot().find(ASW("AOS_DadaData/template",21), nodes);
  it = nodes.begin();
  while (it != nodes.end())
  {
    AString str;
    (*it)->emitContent(str);
    
    AString strName;
    if ((*it)->getAttributes().get(ASW("name",4), strName))
    {
      AFilename filename(m_Services.useConfiguration().getAosBaseDataDirectory(), str, false);
      if (AFileSystem::exists(filename))
      {
        AFile_Physical file(filename, ASW("r", 1));
        file.open();

        str.clear();
        while (AConstant::npos != file.readLine(str))
        {
          if ('#' != str.at(0, '\x0'))
          {
            m_Templates[strName].push_back(str);
          }
          str.clear();
        }
      }
      else
        m_Services.useLog().add(ARope("AOS_DadaData: Missing file: ")+filename, ALog::EVENT_WARNING);
    }
    else
      m_Services.useLog().add(ASWNL("AOS_DadaData: AOS_DadaData/template missing 'name' attribute"), ALog::EVENT_FAILURE);

    ++it;
  }
}
Example #2
0
AXmlElement *AXmlElement::_addElement(const AString& path, bool overwrite, bool insertIntoFront)
{
  if (m_Name.isEmpty())
    ATHROW_EX(this, AException::InvalidObject, ASWNL("AXmlElement does not have a name"));

  LIST_AString xparts;
  path.split(xparts, '/');
  if (!xparts.size())
    ATHROW(this, AException::InvalidParameter);

  //a_Check is absolute is used and if root name matches this element
  if ('/' == path.at(0) && xparts.size() > 0)
  {
    //a_xpath starts with /, make sure names match
    if (isNameEquals(xparts.front()))
      xparts.pop_front();
    else
    {
      AString str("Path specified (");
      str.append(path);
      str.append(") is absolute and does not match this element's name: ");
      str.append(m_Name);
      ATHROW_EX(this, AException::InvalidPath, str);
    }
  }

  //a_Skipped over root or relative path specified
  AXmlElement *p = NULL;
  if (xparts.size() > 0)
  { 
    if (overwrite)
      p = _getOrCreate(xparts, this, insertIntoFront);
    else
      p = _createAndAppend(xparts, this, insertIntoFront);
  }
  else
    p = this;

  return p;
}
Example #3
0
void ATemplateNodeHandler_RESOURCE::Node::process(ATemplateContext& ctx, AOutputBuffer& output)
{
  AAutoPtr<AEventVisitor::ScopedEvent> scoped;
  if (ctx.useEventVisitor().isLogging(AEventVisitor::EL_DEBUG))
  {
    scoped.reset(new AEventVisitor::ScopedEvent(ctx.useEventVisitor(), ASW("ATemplateNodeHandler_RESOURCE",28), m_BlockData, AEventVisitor::EL_DEBUG));
  }

  if (m_BlockData.isEmpty())
    return;

  // Get resource
  AOSContext *pContext = ctx.useObjects().useAsPtr<AOSContext>(AOSContext::OBJECTNAME);
  if (!pContext)
  {
    ARope rope("Must have AOSContext object named: ",35);
    rope.append(AOSContext::OBJECTNAME);
    ATHROW_EX(this, AException::NotFound, rope);
  }

  LIST_AString languages;
  pContext->useRequestHeader().getAcceptLanguageList(languages);
  const AXmlDocument *pDoc = pContext->useServices().useResourceManager().getResources(languages);
  if (!pDoc)
  {
    if (pContext->useEventVisitor().isLogging(AEventVisitor::EL_WARN))
    {
      pContext->useEventVisitor().startEvent(ASW("Unable to find resource container for locales in Accept-Languages",65), AEventVisitor::EL_WARN);
    }
    return;
  }
  
  const AXmlElement *pElement = pDoc->getRoot().findElement(m_BlockData);
  if (pElement)
  {
    //a_Found object
    pElement->emitContent(output);
  }
  else
  {
    if (pContext->useEventVisitor().isLogging(AEventVisitor::EL_WARN))
    {
      ARope rope("Unable to find resource element: ",33);
      rope.append(m_BlockData);
      pContext->useEventVisitor().startEvent(rope, AEventVisitor::EL_WARN);
    }
  }
}
Example #4
0
size_t AXmlElement::_find(const AString& path, AXmlElement::CONTAINER& result)
{
  AString strAttribute;
  AString strPath(path);
  bool leadingSlash = false;

  if ('/' == strPath.at(0))
  {
    leadingSlash = true;  //a_Signals that slash leads the path so much include current name
  }

  //a_Remove the leading name (which should be this element's name)
  LIST_AString listPath;
  strPath.split(listPath, '/');
  if (leadingSlash)
  {
    if (0 == listPath.size())
    {
      //a_ "/" selects current node
      result.push_back(this);
      return 1;
    }
    else if (0 != listPath.front().compare(m_Name))
    {
      //a_First token MUST be the name of this element if / leads
      AString str("Absolute path must start with the name of the current root element: root=/");
      str.append(m_Name);
      str.append(" while path=");
      str.append(path);
      ATHROW_EX(this, AException::ProgrammingError, str);
    }
  }
  else if (0 == listPath.size())
  {
    //a_ "/" selects current node
    result.push_back(this);
    return 1;
  }
  else
  {
    //a_Relative path implies this node is first
    listPath.push_front(m_Name);
  }

  return _nonconst_find(listPath, result);
}
Example #5
0
void AFilename::removeBasePath(
  const AFilename& base, 
  bool makeResultAbsolute // = false
)
{
  //a_Verify that base is actually in this
  LIST_AString::iterator it = m_PathNames.begin();
  for (LIST_AString::const_iterator cit = base.m_PathNames.begin(); cit != base.m_PathNames.end(); ++cit, ++it)
  {
    if (it == m_PathNames.end() || !(*it).equals(*cit))
      ATHROW_EX(this, AException::InvalidData, *cit);
  }
  
  for (size_t i = base.m_PathNames.size(); i > 0; --i)
    m_PathNames.pop_front();

  m_RelativePath = !makeResultAbsolute;
}
Example #6
0
AXmlElement *AXmlElement::_getOrCreate(LIST_AString& xparts, AXmlElement* pParent, bool insertIntoFront)
{
  AString& strName = xparts.front();
  CONTAINER::iterator it = m_Content.begin();
  while (it != m_Content.end())
  {
    if ((*it)->isNameEquals(strName))
    {
      AXmlElement *p = dynamic_cast<AXmlElement *>(*it);
      if (!xparts.size())
      {
        return p;
      }
      else
      {
        if (p)
        {
          xparts.pop_front();
          if (!xparts.size())
            return p;
          else
            return p->_getOrCreate(xparts, this, insertIntoFront);
        }
        else
          ATHROW_EX(this, AException::DataConflict, strName);  //a_ Not AXmlElement type
      }
    }
    ++it;
  }
  
  //a_strName not found, create it
  AXmlElement *p = new AXmlElement(strName, pParent);
  addContent(p, AConstant::ASTRING_EMPTY, insertIntoFront);
  xparts.pop_front();
  if (xparts.size() == 0)
  {
    return p;
  }
  else
  {
    return p->_getOrCreate(xparts, this, insertIntoFront);
  }
}
Example #7
0
size_t AFile_Physical::access(
  AOutputBuffer& target, 
  size_t index,          //= 0 
  size_t bytes           // = AConstant::npos
) const
{
  AASSERT_EX(this, m_fid >= 0, ASWNL("Probably forgot to call open() or invalid file handle after call to open"));   // m_fid == -1? forgot to call open()?
  if (!mp_file)
    ATHROW(this, AException::NotOpen);

  //a_Get original position
  u8 originalIndex = 0;
#ifdef _ftelli64
  originalIndex = _ftelli64(mp_file);
#else
  originalIndex = (u8)ftell(mp_file);
#endif

  //a_Seek to new position
#ifdef _fseeki64
  if (_fseeki64(mp_file, index, 0))
    ATHROW_EX(this, AException::InvalidParameter, AString("Unable to seek position: ")+AString::fromSize_t(index));
#else
  if (fseek(mp_file, (long)index, 0))
    ATHROW_EX(this, AException::InvalidParameter, AString("Unable to seek position: ")+AString::fromSize_t(index));
#endif

  //a_Peek some data
  const size_t BUFFER_SIZE = 10240;
  char buffer[BUFFER_SIZE];
  size_t totalBytes = 0;
  while (bytes)
  {
    size_t bytesToRead = (bytes > BUFFER_SIZE ? BUFFER_SIZE : bytes);
    size_t bytesRead = ::_read(m_fid, buffer, bytesToRead);

    //a_EOF or unavail
    if (AConstant::npos == bytesRead || AConstant::unavail == bytesRead)
      return bytesRead;
    
    //a_Partial read
    if (bytesRead < bytesToRead)
    {
      totalBytes += bytesRead;
      break;
    }

    size_t written = target.append(buffer, bytesRead);
    if (AConstant::unavail == written || AConstant::npos == written)
    {
      return (totalBytes > 0 ? totalBytes : written);
    }
    else
    {
      bytes -= bytesRead;
      totalBytes += bytesRead;
    }
  }
  
#ifdef _fseeki64
  if (_fseeki64(mp_file, originalIndex, 0))
    ATHROW_EX(this, AException::InvalidParameter, AString("Unable to restore seek position: ")+AString::fromSize_t(originalIndex));
#else
  if (fseek(mp_file, (long)originalIndex, 0))
    ATHROW_EX(this, AException::InvalidParameter, AString("Unable to restore seek position: ")+AString::fromSize_t(originalIndex));
#endif

  return totalBytes;
}
Example #8
0
void AXmlElement::fromAFile(AFile& file)
{
  AString str(1024, 256);

  //a_Find <
  if (AConstant::npos == file.skipUntilOneOf('<'))
    return;

  //a_Skip over whitespace between < and tagname
  file.skipUntilNotOneOf();

  //a_Extract name and skip over whitespace
  m_Name.clear();
  file.readUntilOneOf(m_Name, AXmlElement::sstr_EndOrWhitespace, false);
  file.skipUntilNotOneOf();
  
  //a_Find /> or >
  char c = ' ';
  if (0 == m_Name.find(ASW("!--",3)))
  {
    if (m_Name.rfind("--", 2) == m_Name.getSize() - 2)  //TODO:
    //a_Special case for comment, must end with '-->' and may contain '>' inside
    file.readUntil(str, AXmlElement::sstr_EndComment);
    addComment(str);
    return;
  }
  else
    file.readUntilOneOf(str, AXmlElement::sstr_End);

  if (!str.isEmpty() && str.at(str.getSize() - 1) == '/')
  {
    c = '/';  //a_Singular mode
    str.setSize(str.getSize() - 1);
  }
  str.stripTrailing();

  //a_Parse attributes
  if (!str.isEmpty())
    m_Attributes.parse(str);

  bool boolEndTagFound = (c == '/');
  while (!boolEndTagFound)
  {
    //a_Read data until next tag starts
    file.skipUntilNotOneOf();                //a_Skip over whitespace
    str.clear();
    if (AConstant::npos == file.readUntilOneOf(str, AXmlElement::sstr_Start))
      break;

    str.stripTrailing();
    if (!str.isEmpty())
    {
      addData(str);
    }

    //a_Skip over whitespace between < and start of the tag name
    file.skipUntilNotOneOf();

    file.peek(c);
    switch(c)
    {
      case '/':
      {
        //a_End of tag found
        file.read(c);
        str.clear();
        file.readUntilOneOf(str, AXmlElement::sstr_EndOrWhitespace);
        file.skipUntilNotOneOf(AXmlElement::sstr_EndOrWhitespace);                //a_Skip over whitespace and >
        if (str != m_Name)
          ATHROW_EX(&file, AException::InvalidData, AString("Close tag </")+str+"> does not match opened tag <"+m_Name+">");
        
        boolEndTagFound = true;
      }
      break;

      //a_Handle embedded CDATA, comments and such
      case '!':
      {
        file.putBack('<');
        str.clear();
        
        file.peek(str, 9);    //a_ either "<!--*--"  or "<![CDATA["
        if (0 == str.findNoCase(AXmlData::sstr_StartCDATA))
        {
          //a_CDATA found
          file.skip(AXmlData::sstr_StartCDATA.getSize());

          AString data;
          file.readUntil(data, AXmlData::sstr_EndCDATA, true, true);
          addData(data, AXmlElement::ENC_CDATADIRECT);
        }
        else if (0 == str.findNoCase(AXmlElement::sstr_StartComment))
        {
          //a_Comment (!--) found
          file.skip(AXmlElement::sstr_StartComment.getSize());

          AString comment;
          file.readUntil(comment, AXmlElement::sstr_EndComment, true, true);
          addComment(comment);
        }
        else
        {
          addData(str);
        }
      }
      break;

      default:
      {
        //a_Put the start tag back
        file.putBack('<');

        //a_Another element
        AAutoPtr<AXmlElement> psubElement(new AXmlElement(), true);
        psubElement->fromAFile(file);
        psubElement.setOwnership(false);
        addContent(psubElement.use());
      }
      break;
    }
  }
}