Beispiel #1
0
std::string XMLWriter::nameToString(const XMLString& localName, const XMLString& qname)
{
	if (qname.empty())
		return fromXMLString(localName);
	else
		return fromXMLString(qname);
}
Beispiel #2
0
bool DOMSerializer::getFeature(const XMLString& featureId) const
{
	if (featureId == XMLReader::FEATURE_NAMESPACES)
		throw SAXNotSupportedException(fromXMLString(XMLReader::FEATURE_NAMESPACES));
	else if (featureId == XMLReader::FEATURE_NAMESPACE_PREFIXES)
		throw SAXNotSupportedException(fromXMLString(XMLReader::FEATURE_NAMESPACE_PREFIXES));
	else
		throw SAXNotRecognizedException(fromXMLString(featureId));
}
int ParserEngine::handleExternalEntityRef(XML_Parser parser, const XML_Char* context, const XML_Char* base, const XML_Char* systemId, const XML_Char* publicId)
{
	ParserEngine* pThis = reinterpret_cast<ParserEngine*>(XML_GetUserData(parser));

	if (!context && !pThis->_externalParameterEntities) return XML_STATUS_ERROR;
	if (context && !pThis->_externalGeneralEntities) return XML_STATUS_ERROR;

	InputSource* pInputSource = 0;
	EntityResolver* pEntityResolver = 0;
	EntityResolverImpl defaultResolver;

	XMLString sysId(systemId);
	XMLString pubId;
	if (publicId) pubId.assign(publicId);
	
	URI uri(fromXMLString(pThis->_context.back()->getSystemId()));
	uri.resolve(fromXMLString(sysId));

	if (pThis->_pEntityResolver)
	{
		pEntityResolver = pThis->_pEntityResolver;
		pInputSource = pEntityResolver->resolveEntity(publicId ? &pubId : 0, toXMLString(uri.toString()));
	}
	if (!pInputSource && pThis->_externalGeneralEntities)
	{
		pEntityResolver = &defaultResolver;
		pInputSource = pEntityResolver->resolveEntity(publicId ? &pubId : 0, toXMLString(uri.toString()));
	}

	if (pInputSource)
	{
		XML_Parser extParser = XML_ExternalEntityParserCreate(pThis->_parser, context, 0);
		if (!extParser) throw XMLException("Cannot create external entity parser");

		try
		{
			pThis->parseExternal(extParser, pInputSource);
		}
		catch (XMLException&)
		{
			pEntityResolver->releaseInputSource(pInputSource);
			XML_ParserFree(extParser);
			throw;
		}
		pEntityResolver->releaseInputSource(pInputSource);
		XML_ParserFree(extParser);
		return XML_STATUS_OK;
	}
	else return XML_STATUS_ERROR;
}
Beispiel #4
0
void* XMLFilterImpl::getProperty(const XMLString& propertyId) const
{
	if (_pParent)
		return _pParent->getProperty(propertyId);
	else
		throw SAXNotRecognizedException(fromXMLString(propertyId));
}
Beispiel #5
0
void XMLFilterImpl::setProperty(const XMLString& propertyId, void* value)
{
	if (_pParent)
		_pParent->setProperty(propertyId, value);
	else
		throw SAXNotRecognizedException(fromXMLString(propertyId));
}
Beispiel #6
0
bool XMLFilterImpl::getFeature(const XMLString& featureId) const
{
	if (_pParent)
		return _pParent->getFeature(featureId);
	else
		throw SAXNotRecognizedException(fromXMLString(featureId));
}
Beispiel #7
0
void XMLFilterImpl::setFeature(const XMLString& featureId, bool state)
{
	if (_pParent)
		_pParent->setFeature(featureId, state);
	else
		throw SAXNotRecognizedException(fromXMLString(featureId));
}
int ParserEngine::handleUnknownEncoding(void* encodingHandlerData, const XML_Char* name, XML_Encoding* info)
{
	ParserEngine* pThis = reinterpret_cast<ParserEngine*>(encodingHandlerData);
	
	XMLString encoding(name);
	TextEncoding* knownEncoding = 0;

	EncodingMap::const_iterator it = pThis->_encodings.find(encoding);
	if (it != pThis->_encodings.end())
		knownEncoding = it->second;
	else
		knownEncoding = Poco::TextEncoding::find(fromXMLString(encoding));

	if (knownEncoding)
	{
		const TextEncoding::CharacterMap& map = knownEncoding->characterMap();
		for (int i = 0; i < 256; ++i)
			info->map[i] = map[i];
			
		info->data    = knownEncoding;
		info->convert = &ParserEngine::convert;
		info->release = 0;
		return XML_STATUS_OK;
	}
	else return XML_STATUS_ERROR;
}
Beispiel #9
0
void DOMSerializer::setProperty(const XMLString& propertyId, const XMLString& value)
{
	if (propertyId == XMLReader::PROPERTY_DECLARATION_HANDLER || propertyId == XMLReader::PROPERTY_LEXICAL_HANDLER)
		throw SAXNotSupportedException(std::string("property does not take a string value: ") + fromXMLString(propertyId));
	else
		throw SAXNotRecognizedException(fromXMLString(propertyId));
}
Beispiel #10
0
bool SAXParser::getFeature(const XMLString& featureId) const
{
	if (featureId == XMLReader::FEATURE_VALIDATION || featureId == XMLReader::FEATURE_STRING_INTERNING)
		throw SAXNotSupportedException(fromXMLString(XMLReader::FEATURE_VALIDATION));
	else if (featureId == XMLReader::FEATURE_EXTERNAL_GENERAL_ENTITIES)
		return _engine.getExternalGeneralEntities();
	else if (featureId == XMLReader::FEATURE_EXTERNAL_PARAMETER_ENTITIES)
		return _engine.getExternalParameterEntities();
	else if (featureId == XMLReader::FEATURE_NAMESPACES)
		return _namespaces;
	else if (featureId == XMLReader::FEATURE_NAMESPACE_PREFIXES)
		return _namespacePrefixes;
	else if (featureId == FEATURE_PARTIAL_READS)
		return _engine.getEnablePartialReads();
	else throw SAXNotRecognizedException(fromXMLString(featureId));
}
Beispiel #11
0
void* DOMSerializer::getProperty(const XMLString& propertyId) const
{
	if (propertyId == XMLReader::PROPERTY_DECLARATION_HANDLER)
		return _pDeclHandler;
	else if (propertyId == XMLReader::PROPERTY_LEXICAL_HANDLER)
		return _pLexicalHandler;
	else throw SAXNotSupportedException(fromXMLString(propertyId));
}
Beispiel #12
0
void DOMSerializer::setProperty(const XMLString& propertyId, void* value)
{
	if (propertyId == XMLReader::PROPERTY_DECLARATION_HANDLER)
		_pDeclHandler = reinterpret_cast<DeclHandler*>(value);
	else if (propertyId == XMLReader::PROPERTY_LEXICAL_HANDLER)
		_pLexicalHandler = reinterpret_cast<LexicalHandler*>(value);
	else throw SAXNotRecognizedException(fromXMLString(propertyId));
}
Beispiel #13
0
void* SAXParser::getProperty(const XMLString& propertyId) const
{
	if (propertyId == XMLReader::PROPERTY_DECLARATION_HANDLER)
		return _engine.getDeclHandler();
	else if (propertyId == XMLReader::PROPERTY_LEXICAL_HANDLER)
		return _engine.getLexicalHandler();
	else throw SAXNotSupportedException(fromXMLString(propertyId));
}
Beispiel #14
0
void SAXParser::parse(const XMLString& systemId)
{
	setupParse();
	EntityResolverImpl entityResolver;
	InputSource* pInputSource = entityResolver.resolveEntity(0, systemId);
	if (pInputSource)
	{
		try
		{
			_engine.parse(pInputSource);
		}
		catch (...)
		{
			entityResolver.releaseInputSource(pInputSource);
			throw;
		}
		entityResolver.releaseInputSource(pInputSource);
	}
	else throw XMLException("Cannot resolve system identifier", fromXMLString(systemId));
}
Beispiel #15
0
void WhitespaceFilter::setProperty(const XMLString& propertyId, const XMLString& value)
{
	if (propertyId == XMLReader::PROPERTY_LEXICAL_HANDLER)
		throw SAXNotSupportedException(std::string("property does not take a string value: ") + fromXMLString(propertyId));
	else
		XMLFilterImpl::setProperty(propertyId, value);

}
Beispiel #16
0
/**
 * Recursively parse an XML element.
 */
bool
XMLNode::ParseXMLElement(XML *pXML)
{
  const TCHAR *lpszTemp = NULL;
  size_t cbTemp;
  unsigned nDeclaration;
  const TCHAR *lpszText = NULL;
  XMLNode pNew;
  enum Status status; // inside or outside a tag
  enum Attrib attrib = eAttribName;

  assert(pXML);

  // If this is the first call to the function
  if (pXML->nFirst) {
    // Assume we are outside of a tag definition
    pXML->nFirst = false;
    status = eOutsideTag;
  } else {
    // If this is not the first call then we should only be called when inside a tag.
    status = eInsideTag;
  }

  // Iterate through the tokens in the document
  while (true) {
    // Obtain the next token
    size_t cbToken;
    enum TokenTypeTag type;
    NextToken token = GetNextToken(pXML, &cbToken, &type);
    if (gcc_unlikely(type == eTokenError))
      return false;

    // Check the current status
    switch (status) {
      // If we are outside of a tag definition
    case eOutsideTag:

      // Check what type of token we obtained
      switch (type) {
        // If we have found text or quoted text
      case eTokenText:
      case eTokenQuotedText:
      case eTokenEquals:
        if (!lpszText)
          lpszText = token.pStr;

        break;

        // If we found a start tag '<' and declarations '<?'
      case eTokenTagStart:
      case eTokenDeclaration:
        // Cache whether this new element is a declaration or not
        nDeclaration = type == eTokenDeclaration;

        // If we have node text then add this to the element
        if (lpszText) {
          cbTemp = token.pStr - lpszText;
          FindEndOfText(lpszText, &cbTemp);
          AddText(lpszText, cbTemp);
          lpszText = NULL;
        }

        // Find the name of the tag
        token = GetNextToken(pXML, &cbToken, &type);

        // Return an error if we couldn't obtain the next token or
        // it wasnt text
        if (type != eTokenText) {
          pXML->error = eXMLErrorMissingTagName;
          return false;
        }

        // If we found a new element which is the same as this
        // element then we need to pass this back to the caller..

#ifdef APPROXIMATE_PARSING
        if (d->lpszName && myTagCompare(d->lpszName, token.pStr)) {
          // Indicate to the caller that it needs to create a
          // new element.
          pXML->lpNewElement = token.pStr;
          pXML->cbNewElement = cbToken;
          return true;
        }
#endif

        // If the name of the new element differs from the name of
        // the current element we need to add the new element to
        // the current one and recurse
        pNew = AddChild(stringDup(token.pStr, cbToken), nDeclaration);

        while (true) {
          // Callself to process the new node.  If we return
          // FALSE this means we dont have any more
          // processing to do...

          if (!pNew.ParseXMLElement(pXML)) {
            return false;
          } else {
            // If the call to recurse this function
            // evented in a end tag specified in XML then
            // we need to unwind the calls to this
            // function until we find the appropriate node
            // (the element name and end tag name must
            // match)
            if (pXML->cbEndTag) {
              // If we are back at the root node then we
              // have an unmatched end tag
              if (!d->lpszName) {
                pXML->error = eXMLErrorUnmatchedEndTag;
                return false;
              }

              // If the end tag matches the name of this
              // element then we only need to unwind
              // once more...

              if (myTagCompare(d->lpszName, pXML->lpEndTag)) {
                pXML->cbEndTag = 0;
              }

              return true;
            } else if (pXML->cbNewElement) {
              // If the call indicated a new element is to
              // be created on THIS element.

              // If the name of this element matches the
              // name of the element we need to create
              // then we need to return to the caller
              // and let it process the element.

              if (myTagCompare(d->lpszName, pXML->lpNewElement))
                return true;

              // Add the new element and recurse
              pNew = AddChild(stringDup(pXML->lpNewElement,
                                        pXML->cbNewElement), false);
              pXML->cbNewElement = 0;
            } else {
              // If we didn't have a new element to create
              break;
            }
          }
        }
        break;

        // If we found an end tag
      case eTokenTagEnd:

        // If we have node text then add this to the element
        if (lpszText) {
          cbTemp = token.pStr - lpszText;
          FindEndOfText(lpszText, &cbTemp);
          TCHAR *text = fromXMLString(lpszText, cbTemp);
          if (text == NULL) {
            pXML->error = eXMLErrorUnexpectedToken;
            return false;
          }

          AddText(text);
          free(text);
          lpszText = NULL;
        }

        // Find the name of the end tag
        token = GetNextToken(pXML, &cbTemp, &type);

        // The end tag should be text
        if (type != eTokenText) {
          pXML->error = eXMLErrorMissingEndTagName;
          return false;
        }
        lpszTemp = token.pStr;

        // After the end tag we should find a closing tag
        token = GetNextToken(pXML, &cbToken, &type);
        if (type != eTokenCloseTag) {
          pXML->error = eXMLErrorMissingEndTagName;
          return false;
        }

        // We need to return to the previous caller.  If the name
        // of the tag cannot be found we need to keep returning to
        // caller until we find a match
        if (!myTagCompare(d->lpszName, lpszTemp)) {
          pXML->lpEndTag = lpszTemp;
          pXML->cbEndTag = cbTemp;
        }

        // Return to the caller
        return true;

        // Errors...
      case eTokenCloseTag: /* '>'         */
      case eTokenShortHandClose: /* '/>'        */
        pXML->error = eXMLErrorUnexpectedToken;
        return false;
      default:
        break;
      }
      break;

      // If we are inside a tag definition we need to search for attributes
    case eInsideTag:
      // Check what part of the attribute (name, equals, value) we
      // are looking for.
      switch (attrib) {
        // If we are looking for a new attribute
      case eAttribName:
        // Check what the current token type is
        switch (type) {
          // If the current type is text...
          // Eg.  'attribute'
        case eTokenText:
          // Cache the token then indicate that we are next to
          // look for the equals
          lpszTemp = token.pStr;
          cbTemp = cbToken;
          attrib = eAttribEquals;
          break;

          // If we found a closing tag...
          // Eg.  '>'
        case eTokenCloseTag:
          // We are now outside the tag
          status = eOutsideTag;
          break;

          // If we found a short hand '/>' closing tag then we can
          // return to the caller
        case eTokenShortHandClose:
          return true;

          // Errors...
        case eTokenQuotedText: /* '"SomeText"'   */
        case eTokenTagStart: /* '<'            */
        case eTokenTagEnd: /* '</'           */
        case eTokenEquals: /* '='            */
        case eTokenDeclaration: /* '<?'           */
          pXML->error = eXMLErrorUnexpectedToken;
          return false;
        default:
          break;
        }
        break;

        // If we are looking for an equals
      case eAttribEquals:
        // Check what the current token type is
        switch (type) {
          // If the current type is text...
          // Eg.  'Attribute AnotherAttribute'
        case eTokenText:
          // Add the unvalued attribute to the list
          AddAttribute(stringDup(lpszTemp, cbTemp), NULL);
          // Cache the token then indicate.  We are next to
          // look for the equals attribute
          lpszTemp = token.pStr;
          cbTemp = cbToken;
          break;

          // If we found a closing tag 'Attribute >' or a short hand
          // closing tag 'Attribute />'
        case eTokenShortHandClose:
        case eTokenCloseTag:
          // If we are a declaration element '<?' then we need
          // to remove extra closing '?' if it exists
          if (d->isDeclaration && (lpszTemp[cbTemp - 1]) == _T('?'))
            cbTemp--;

          if (cbTemp)
            // Add the unvalued attribute to the list
            AddAttribute(stringDup(lpszTemp, cbTemp), NULL);

          // If this is the end of the tag then return to the caller
          if (type == eTokenShortHandClose)
            return true;

          // We are now outside the tag
          status = eOutsideTag;
          break;

          // If we found the equals token...
          // Eg.  'Attribute ='
        case eTokenEquals:
          // Indicate that we next need to search for the value
          // for the attribute
          attrib = eAttribValue;
          break;

          // Errors...
        case eTokenQuotedText: /* 'Attribute "InvalidAttr"'*/
        case eTokenTagStart: /* 'Attribute <'            */
        case eTokenTagEnd: /* 'Attribute </'           */
        case eTokenDeclaration: /* 'Attribute <?'           */
          pXML->error = eXMLErrorUnexpectedToken;
          return false;
        default:
          break;
        }
        break;

        // If we are looking for an attribute value
      case eAttribValue:
        // Check what the current token type is
        switch (type) {
          // If the current type is text or quoted text...
          // Eg.  'Attribute = "Value"' or 'Attribute = Value' or
          // 'Attribute = 'Value''.
        case eTokenText:
        case eTokenQuotedText:
          // If we are a declaration element '<?' then we need
          // to remove extra closing '?' if it exists
          if (d->isDeclaration && (token.pStr[cbToken - 1]) == _T('?')) {
            cbToken--;
          }

          if (cbTemp) {
            // Add the valued attribute to the list
            if (type == eTokenQuotedText) {
              token.pStr++;
              cbToken -= 2;
            }
            AddAttribute(stringDup(lpszTemp, cbTemp), fromXMLString(
                                                                    token.pStr, cbToken));
          }

          // Indicate we are searching for a new attribute
          attrib = eAttribName;
          break;

          // Errors...
        case eTokenTagStart: /* 'Attr = <'          */
        case eTokenTagEnd: /* 'Attr = </'         */
        case eTokenCloseTag: /* 'Attr = >'          */
        case eTokenShortHandClose: /* "Attr = />"         */
        case eTokenEquals: /* 'Attr = ='          */
        case eTokenDeclaration: /* 'Attr = <?'         */
          pXML->error = eXMLErrorUnexpectedToken;
          return false;
        default:
          break;
        }
      }
    }
  }
}