📄 xmltokenizerlibxml2.cpp
字号:
if (m_view && !newNode->attached()) newNode->attach();}void XMLTokenizer::comment(const xmlChar* s){ if (m_parserStopped) return; if (m_parserPaused) { m_pendingCallbacks->appendCommentCallback(s); return; } exitText(); RefPtr<Node> newNode = new Comment(m_doc, toString(s)); m_currentNode->addChild(newNode.get()); if (m_view && !newNode->attached()) newNode->attach();}void XMLTokenizer::startDocument(const xmlChar* version, const xmlChar* encoding, int standalone){ ExceptionCode ec = 0; if (version) m_doc->setXMLVersion(toString(version), ec); m_doc->setXMLStandalone(standalone == 1, ec); // possible values are 0, 1, and -1 if (encoding) m_doc->setXMLEncoding(toString(encoding));}void XMLTokenizer::endDocument(){ exitText();}void XMLTokenizer::internalSubset(const xmlChar* name, const xmlChar* externalID, const xmlChar* systemID){ if (m_parserStopped) return; if (m_parserPaused) { m_pendingCallbacks->appendInternalSubsetCallback(name, externalID, systemID); return; } if (m_doc) {#if ENABLE(WML) String extId = toString(externalID); if (isWMLDocument() && extId != "-//WAPFORUM//DTD WML 1.3//EN" && extId != "-//WAPFORUM//DTD WML 1.2//EN" && extId != "-//WAPFORUM//DTD WML 1.1//EN" && extId != "-//WAPFORUM//DTD WML 1.0//EN") handleError(fatal, "Invalid DTD Public ID", lineNumber(), columnNumber());#endif m_doc->addChild(DocumentType::create(m_doc, toString(name), toString(externalID), toString(systemID))); }}static inline XMLTokenizer* getTokenizer(void* closure){ xmlParserCtxtPtr ctxt = static_cast<xmlParserCtxtPtr>(closure); return static_cast<XMLTokenizer*>(ctxt->_private);}// This is a hack around http://bugzilla.gnome.org/show_bug.cgi?id=159219// Otherwise libxml seems to call all the SAX callbacks twice for any replaced entity.static inline bool hackAroundLibXMLEntityBug(void* closure){#if LIBXML_VERSION >= 20627 UNUSED_PARAM(closure); // This bug has been fixed in libxml 2.6.27. return false;#else return static_cast<xmlParserCtxtPtr>(closure)->node;#endif}static void startElementNsHandler(void* closure, const xmlChar* localname, const xmlChar* prefix, const xmlChar* uri, int nb_namespaces, const xmlChar** namespaces, int nb_attributes, int nb_defaulted, const xmlChar** libxmlAttributes){ if (hackAroundLibXMLEntityBug(closure)) return; getTokenizer(closure)->startElementNs(localname, prefix, uri, nb_namespaces, namespaces, nb_attributes, nb_defaulted, libxmlAttributes);}static void endElementNsHandler(void* closure, const xmlChar*, const xmlChar*, const xmlChar*){ if (hackAroundLibXMLEntityBug(closure)) return; getTokenizer(closure)->endElementNs();}static void charactersHandler(void* closure, const xmlChar* s, int len){ if (hackAroundLibXMLEntityBug(closure)) return; getTokenizer(closure)->characters(s, len);}static void processingInstructionHandler(void* closure, const xmlChar* target, const xmlChar* data){ if (hackAroundLibXMLEntityBug(closure)) return; getTokenizer(closure)->processingInstruction(target, data);}static void cdataBlockHandler(void* closure, const xmlChar* s, int len){ if (hackAroundLibXMLEntityBug(closure)) return; getTokenizer(closure)->cdataBlock(s, len);}static void commentHandler(void* closure, const xmlChar* comment){ if (hackAroundLibXMLEntityBug(closure)) return; getTokenizer(closure)->comment(comment);}WTF_ATTRIBUTE_PRINTF(2, 3)static void warningHandler(void* closure, const char* message, ...){ va_list args; va_start(args, message); getTokenizer(closure)->error(XMLTokenizer::warning, message, args); va_end(args);}WTF_ATTRIBUTE_PRINTF(2, 3)static void fatalErrorHandler(void* closure, const char* message, ...){ va_list args; va_start(args, message); getTokenizer(closure)->error(XMLTokenizer::fatal, message, args); va_end(args);}WTF_ATTRIBUTE_PRINTF(2, 3)static void normalErrorHandler(void* closure, const char* message, ...){ va_list args; va_start(args, message); getTokenizer(closure)->error(XMLTokenizer::nonFatal, message, args); va_end(args);}// Using a global variable entity and marking it XML_INTERNAL_PREDEFINED_ENTITY is// a hack to avoid malloc/free. Using a global variable like this could cause trouble// if libxml implementation details were to changestatic xmlChar sharedXHTMLEntityResult[5] = {0,0,0,0,0};static xmlEntity sharedXHTMLEntity = { 0, XML_ENTITY_DECL, 0, 0, 0, 0, 0, 0, 0, sharedXHTMLEntityResult, sharedXHTMLEntityResult, 0, XML_INTERNAL_PREDEFINED_ENTITY, 0, 0, 0, 0, 0,#if LIBXML_VERSION >= 20627 // xmlEntity gained an extra member in 2.6.27. 1#endif};static xmlEntityPtr getXHTMLEntity(const xmlChar* name){ UChar c = decodeNamedEntity(reinterpret_cast<const char*>(name)); if (!c) return 0; CString value = String(&c, 1).utf8(); ASSERT(value.length() < 5); sharedXHTMLEntity.length = value.length(); sharedXHTMLEntity.name = name; memcpy(sharedXHTMLEntityResult, value.data(), sharedXHTMLEntity.length + 1); return &sharedXHTMLEntity;}static xmlEntityPtr getEntityHandler(void* closure, const xmlChar* name){ xmlParserCtxtPtr ctxt = static_cast<xmlParserCtxtPtr>(closure); xmlEntityPtr ent = xmlGetPredefinedEntity(name); if (ent) { ent->etype = XML_INTERNAL_PREDEFINED_ENTITY; return ent; } ent = xmlGetDocEntity(ctxt->myDoc, name); if (!ent && (getTokenizer(closure)->isXHTMLDocument()#if ENABLE(WML) || getTokenizer(closure)->isWMLDocument()#endif )) { ent = getXHTMLEntity(name); if (ent) ent->etype = XML_INTERNAL_GENERAL_ENTITY; } return ent;}static void startDocumentHandler(void* closure){ xmlParserCtxt* ctxt = static_cast<xmlParserCtxt*>(closure); getTokenizer(closure)->startDocument(ctxt->version, ctxt->encoding, ctxt->standalone); xmlSAX2StartDocument(closure);}static void endDocumentHandler(void* closure){ getTokenizer(closure)->endDocument(); xmlSAX2EndDocument(closure);}static void internalSubsetHandler(void* closure, const xmlChar* name, const xmlChar* externalID, const xmlChar* systemID){ getTokenizer(closure)->internalSubset(name, externalID, systemID); xmlSAX2InternalSubset(closure, name, externalID, systemID);}static void externalSubsetHandler(void* closure, const xmlChar*, const xmlChar* externalId, const xmlChar*){ String extId = toString(externalId); if ((extId == "-//W3C//DTD XHTML 1.0 Transitional//EN") || (extId == "-//W3C//DTD XHTML 1.1//EN") || (extId == "-//W3C//DTD XHTML 1.0 Strict//EN") || (extId == "-//W3C//DTD XHTML 1.0 Frameset//EN") || (extId == "-//W3C//DTD XHTML Basic 1.0//EN") || (extId == "-//W3C//DTD XHTML 1.1 plus MathML 2.0//EN") || (extId == "-//W3C//DTD XHTML 1.1 plus MathML 2.0 plus SVG 1.1//EN") || (extId == "-//WAPFORUM//DTD XHTML Mobile 1.0//EN")) getTokenizer(closure)->setIsXHTMLDocument(true); // controls if we replace entities or not.}static void ignorableWhitespaceHandler(void*, const xmlChar*, int){ // nothing to do, but we need this to work around a crasher // http://bugzilla.gnome.org/show_bug.cgi?id=172255 // http://bugs.webkit.org/show_bug.cgi?id=5792}void XMLTokenizer::initializeParserContext(const char* chunk){ xmlSAXHandler sax; memset(&sax, 0, sizeof(sax)); sax.error = normalErrorHandler; sax.fatalError = fatalErrorHandler; sax.characters = charactersHandler; sax.processingInstruction = processingInstructionHandler; sax.cdataBlock = cdataBlockHandler; sax.comment = commentHandler; sax.warning = warningHandler; sax.startElementNs = startElementNsHandler; sax.endElementNs = endElementNsHandler; sax.getEntity = getEntityHandler; sax.startDocument = startDocumentHandler; sax.endDocument = endDocumentHandler; sax.internalSubset = internalSubsetHandler; sax.externalSubset = externalSubsetHandler; sax.ignorableWhitespace = ignorableWhitespaceHandler; sax.entityDecl = xmlSAX2EntityDecl; sax.initialized = XML_SAX2_MAGIC; m_parserStopped = false; m_sawError = false; m_sawXSLTransform = false; m_sawFirstElement = false; if (m_parsingFragment) m_context = createMemoryParser(&sax, this, chunk); else m_context = createStringParser(&sax, this);}void XMLTokenizer::doEnd(){#if ENABLE(XSLT) if (m_sawXSLTransform) { m_doc->setTransformSource(xmlDocPtrForString(m_doc->docLoader(), m_originalSourceForTransform, m_doc->url().string())); m_doc->setParsing(false); // Make the doc think it's done, so it will apply xsl sheets. m_doc->updateStyleSelector(); m_doc->setParsing(true); m_parserStopped = true; }#endif if (m_context) { // Tell libxml we're done. xmlParseChunk(m_context, 0, 0, 1); if (m_context->myDoc) xmlFreeDoc(m_context->myDoc); xmlFreeParserCtxt(m_context); m_context = 0; }}#if ENABLE(XSLT)void* xmlDocPtrForString(DocLoader* docLoader, const String& source, const String& url){ if (source.isEmpty()) return 0; // Parse in a single chunk into an xmlDocPtr // FIXME: Hook up error handlers so that a failure to parse the main document results in // good error messages. const UChar BOM = 0xFEFF; const unsigned char BOMHighByte = *reinterpret_cast<const unsigned char*>(&BOM); xmlGenericErrorFunc oldErrorFunc = xmlGenericError; void* oldErrorContext = xmlGenericErrorContext; setLoaderForLibXMLCallbacks(docLoader); xmlSetGenericErrorFunc(0, errorFunc); xmlDocPtr sourceDoc = xmlReadMemory(reinterpret_cast<const char*>(source.characters()), source.length() * sizeof(UChar), url.latin1().data(), BOMHighByte == 0xFF ? "UTF-16LE" : "UTF-16BE", XSLT_PARSE_OPTIONS); setLoaderForLibXMLCallbacks(0); xmlSetGenericErrorFunc(oldErrorContext, oldErrorFunc); return sourceDoc;}#endifint XMLTokenizer::lineNumber() const{ return m_context ? m_context->input->line : 1;}int XMLTokenizer::columnNumber() const{ return m_context ? m_context->input->col : 1;}void XMLTokenizer::stopParsing(){ Tokenizer::stopParsing(); xmlStopParser(m_context);}void XMLTokenizer::resumeParsing(){ ASSERT(m_parserPaused); m_parserPaused = false; // First, execute any pending callbacks while (!m_pendingCallbacks->isEmpty()) { m_pendingCallbacks->callAndRemoveFirstCallback(this); // A callback paused the parser if (m_parserPaused) return; } // Then, write any pending data SegmentedString rest = m_pendingSrc; m_pendingSrc.clear(); write(rest, false); // Finally, if finish() has been called and write() didn't result // in any further callbacks being queued, call end() if (m_finishCalled && m_pendingCallbacks->isEmpty()) end();}bool parseXMLDocumentFragment(const String& chunk, DocumentFragment* fragment, Element* parent){ if (!chunk.length()) return true; XMLTokenizer tokenizer(fragment, parent); tokenizer.initializeParserContext(chunk.utf8().data()); xmlParseContent(tokenizer.m_context); tokenizer.endDocument(); // Check if all the chunk has been processed. long bytesProcessed = xmlByteConsumed(tokenizer.m_context); if (bytesProcessed == -1 || ((unsigned long)bytesProcessed) == sizeof(UChar) * chunk.length()) return false; // No error if the chunk is well formed or it is not but we have no error. return tokenizer.m_context->wellFormed || xmlCtxtGetLastError(tokenizer.m_context) == 0;}// --------------------------------struct AttributeParseState { HashMap<String, String> attributes; bool gotAttributes;};static void attributesStartElementNsHandler(void* closure, const xmlChar* xmlLocalName, const xmlChar* /*xmlPrefix*/, const xmlChar* /*xmlURI*/, int /*nb_namespaces*/, const xmlChar** /*namespaces*/, int nb_attributes, int /*nb_defaulted*/, const xmlChar** libxmlAttributes){ if (strcmp(reinterpret_cast<const char*>(xmlLocalName), "attrs") != 0) return; xmlParserCtxtPtr ctxt = static_cast<xmlParserCtxtPtr>(closure); AttributeParseState* state = static_cast<AttributeParseState*>(ctxt->_private); state->gotAttributes = true; xmlSAX2Attributes* attributes = reinterpret_cast<xmlSAX2Attributes*>(libxmlAttributes); for(int i = 0; i < nb_attributes; i++) { String attrLocalName = toString(attributes[i].localname); int valueLength = (int) (attributes[i].end - attributes[i].value); String attrValue = toString(attributes[i].value, valueLength); String attrPrefix = toString(attributes[i].prefix); String attrQName = attrPrefix.isEmpty() ? attrLocalName : attrPrefix + ":" + attrLocalName; state->attributes.set(attrQName, attrValue); }}HashMap<String, String> parseAttributes(const String& string, bool& attrsOK){ AttributeParseState state; state.gotAttributes = false; xmlSAXHandler sax; memset(&sax, 0, sizeof(sax)); sax.startElementNs = attributesStartElementNsHandler; sax.initialized = XML_SAX2_MAGIC; xmlParserCtxtPtr parser = createStringParser(&sax, &state); String parseString = "<?xml version=\"1.0\"?><attrs " + string + " />"; xmlParseChunk(parser, reinterpret_cast<const char*>(parseString.characters()), parseString.length() * sizeof(UChar), 1); if (parser->myDoc) xmlFreeDoc(parser->myDoc); xmlFreeParserCtxt(parser); attrsOK = state.gotAttributes; return state.attributes;}}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -