csxmldom.pas

来自「Delphi XML & XPATH源代码」· PAS 代码 · 共 1,589 行 · 第 1/3 页

PAS
1,589
字号
  Result := FChildNodes;
end;

function TCSDOMNode.get_firstChild: IDOMNode;
begin
  Result := MakeNode(Node.FirstChild, FOwnerDocument);
end;

function TCSDOMNode.get_lastChild: IDOMNode;
begin
  Result := MakeNode(Node.LastChild, FOwnerDocument);
end;

function TCSDOMNode.get_localName: DOMString;
begin
  Result := Node.BaseName;
end;

function TCSDOMNode.get_namespaceURI: DOMString;
begin
  Result := Node.Namespace;
end;

function TCSDOMNode.get_nextSibling: IDOMNode;
begin
  Result := MakeNode(Node.NextSibling, FOwnerDocument);
end;

function TCSDOMNode.get_nodeName: DOMString;
begin
  Result := Node.NodeName;
end;

function TCSDOMNode.get_nodeType: DOMNodeType;
begin
  Result := Node.NodeType;
end;

function TCSDOMNode.get_nodeValue: DOMString;
begin
  Result := Node.NodeValue;
end;

function TCSDOMNode.get_ownerDocument: IDOMDocument;
begin
  Result := FOwnerDocument;
end;

function TCSDOMNode.get_parentNode: IDOMNode;
begin
  Result := MakeNode(Node.ParentNode, FOwnerDocument);
end;

function TCSDOMNode.get_prefix: DOMString;
begin
  Result := Node.Prefix;
end;

function TCSDOMNode.get_previousSibling: IDOMNode;
begin
  Result := MakeNode(Node.PreviousSibling, FOwnerDocument);
end;

function TCSDOMNode.hasChildNodes: WordBool;
begin
  Result := Node.HasChildNodes;
end;

function TCSDOMNode.insertBefore(const newChild, refChild: IDOMNode): IDOMNode;
begin
  Node.InsertBefore(GetNode(newChild), GetNode(refChild));
  Result := newChild;
end;

procedure TCSDOMNode.normalize;
begin
  if Node is TXmlElement then
    TXmlElement(Node).Normalize
  else
    DOMVendorNotSupported('normalize', sCUEXml); { Do not localize }
end;

function TCSDOMNode.removeChild(const childNode: IDOMNode): IDOMNode;
begin
  Result := MakeNode(Node.RemoveChild(GetNode(childNode)), FOwnerDocument);
end;

function TCSDOMNode.replaceChild(const newChild, oldChild: IDOMNode): IDOMNode;
begin
  Result := MakeNode(Node.ReplaceChild(GetNode(newChild), GetNode(oldChild)),
    FOwnerDocument);
end;

procedure TCSDOMNode.set_nodeValue(value: DOMString);
begin
  Node.NodeValue := value;
end;

function TCSDOMNode.supports(const feature, version: DOMString): WordBool;
begin
  Result := CSXML_DOM.DOMImplementation.hasFeature(feature, version);
end;

function TCSDOMNode.GetXMLDOMNode: TXmlNode;
begin
  Result := Node;
end;

{ IDOMNodeEx Interface }

function TCSDOMNode.get_text: DOMString;
begin
  Result := Node.Text;
end;

procedure TCSDOMNode.set_text(const Value: DOMString);
var
  Index: Integer;
begin
  for Index := Node.ChildNodes.Length - 1 downto 0 do
    Node.RemoveChild(Node.ChildNodes.Item(0));
  Node.AppendChild(Node.OwnerDocument.CreateTextNode(Value));
end;

function TCSDOMNode.get_xml: DOMString;
begin
  Result := Node.XmlDocument;
  if Result[Length(Result)] = #0 then
    Delete(Result, Length(Result), 1);
end;

procedure TCSDOMNode.transformNode(const stylesheet: IDOMNode;
  var output: WideString);
var
  XSLProc: TXslProcessor;
begin
  XSLProc := TXslProcessor.Create(nil);
  with XSLProc do
    try
      try
        Filter      := TXslFilterHTML.Create(XSLProc);
        RaiseErrors := True;
        StyleData   := GetNode(stylesheet).XmlDocument;
        XmlObjModel := FOwnerDocument.FXMLObjModel;
        ApplyStyle;
        output      := FixLineBreaks(TXslFilterHTML(Filter).HTMLDocument);
      except on E: Exception do
        raise DOMException.Create(E.Message);
      end;
    finally
      Free;
    end;
end;

procedure TCSDOMNode.transformNode(const stylesheet: IDOMNode;
  const output: IDOMDocument);
var
  XSLOutput: WideString;
  XMLObjModel: TXmlObjModel;
begin
  transformNode(stylesheet, XSLOutput);
  XMLObjModel := TXmlObjModel.Create(nil);
  with XMLObjModel do
    try
      LoadMemory(@XSLOutput[1]);
      output.importNode(MakeNode(Document, FOwnerDocument), True);
    finally
      Free;
    end;
end;

{ IDOMNodeSelect }

function TCSDOMNode.selectNode(const nodePath: WideString): IDOMNode;
begin
  if (Node is TXmlDocument) and (TXmlDocument(Node).DocumentElement <> nil) then
    Result := MakeNode(TXmlDocument(Node).DocumentElement.
      SelectSingleNode(nodePath), FOwnerDocument)
  else if Node is TXmlElement then
    Result := MakeNode(TXmlElement(Node).SelectSingleNode(nodePath),
      FOwnerDocument)
  else
    DOMVendorNotSupported('selectNode', sCUEXml); { Do not localize }
end;

function TCSDOMNode.selectNodes(const nodePath: WideString): IDOMNodeList;
begin
  if (Node is TXmlDocument) and (TXmlDocument(Node).DocumentElement <> nil) then
    Result := MakeNodeList(TXmlDocument(Node).DocumentElement.
      SelectNodes(nodePath), FOwnerDocument)
  else if Node is TXmlElement then
    Result := MakeNodeList(TXmlElement(Node).SelectNodes(nodePath),
      FOwnerDocument)
  else
    DOMVendorNotSupported('selectNodes', sCUEXml); { Do not localize }
end;

{ TCSDOMNodeList ------------------------------------------------------------}

constructor TCSDOMNodeList.Create(const NodeList: TXmlNodeList;
      const Document: TCSDOMDocument);
begin
  inherited Create;
  FNodeList      := NodeList;
  FOwnerDocument := Document;
end;

function TCSDOMNodeList.get_item(index: Integer): IDOMNode;
begin
  Result := MakeNode(NodeList.Item(index), FOwnerDocument);
end;

function TCSDOMNodeList.get_length: Integer;
begin
  Result := NodeList.Length;
end;

{ TCSDOMNamedNodeMap --------------------------------------------------------}

constructor TCSDOMNamedNodeMap.Create(const NamedNodeMap: TXmlNamedNodeMap;
      const Document: TCSDOMDocument);
begin
  inherited Create;
  FNamedNodeMap  := NamedNodeMap;
  FOwnerDocument := Document;
end;

function TCSDOMNamedNodeMap.get_item(index: Integer): IDOMNode;
begin
  Result := MakeNode(NamedNodeMap.Item(index), FOwnerDocument);
end;

function TCSDOMNamedNodeMap.get_length: Integer;
begin
  Result := NamedNodeMap.Length;
end;

function TCSDOMNamedNodeMap.getNamedItem(const name: DOMString): IDOMNode;
begin
  Result := MakeNode(NamedNodeMap.GetNamedItem(name), FOwnerDocument);
end;

function TCSDOMNamedNodeMap.getNamedItemNS(
  const namespaceURI, localName: DOMString): IDOMNode;
var
  Index: Integer;
begin
  Result := nil;
  for Index := 0 to NamedNodeMap.Length - 1 do
    if (NamedNodeMap.Item(Index).BaseName = localName) and
        (NamedNodeMap.Item(Index).Namespace = namespaceURI) then
    begin
      Result := MakeNode(NamedNodeMap.Item(Index), FOwnerDocument);
      Exit;
    end;
end;

function TCSDOMNamedNodeMap.removeNamedItem(const name: DOMString): IDOMNode;
begin
  Result := MakeNode(NamedNodeMap.RemoveNamedItem(name), FOwnerDocument);
end;

function TCSDOMNamedNodeMap.removeNamedItemNS(
  const namespaceURI, localName: DOMString): IDOMNode;
var
  Index: Integer;
begin
  Result := nil;
  for Index := 0 to NamedNodeMap.Length - 1 do
    if (NamedNodeMap.Item(Index).BaseName = localName) and
        (NamedNodeMap.Item(Index).Namespace = namespaceURI) then
    begin
      Result := MakeNode(NamedNodeMap.RemoveNamedItem(
        NamedNodeMap.Item(Index).NodeName), FOwnerDocument);
      Exit;
    end;
end;

function TCSDOMNamedNodeMap.setNamedItem(const newItem: IDOMNode): IDOMNode;
begin
  Result := MakeNode(NamedNodeMap.SetNamedItem(GetNode(newItem)),
    FOwnerDocument);
end;

function TCSDOMNamedNodeMap.setNamedItemNS(const arg: IDOMNode): IDOMNode;
begin
  Result := MakeNode(NamedNodeMap.SetNamedItem(GetNode(arg)), FOwnerDocument);
end;

{ TCSDOMCharacterData -------------------------------------------------------}

function TCSDOMCharacterData.GetCharacterData: TXmlCharacterData;
begin
  Result := Node as TXmlCharacterData;
end;

procedure TCSDOMCharacterData.appendData(const data: DOMString);
begin
  CharacterData.AppendData(data);
end;

procedure TCSDOMCharacterData.deleteData(offset, count: Integer);
begin
  CharacterData.DeleteData(offset, count);
end;

function TCSDOMCharacterData.get_data: DOMString;
begin
  Result := CharacterData.Data;
end;

function TCSDOMCharacterData.get_length: Integer;
begin
  Result := CharacterData.Length;
end;

procedure TCSDOMCharacterData.insertData(offset: Integer;
  const data: DOMString);
begin
  CharacterData.InsertData(offset, data);
end;

procedure TCSDOMCharacterData.replaceData(offset, count: Integer;
  const data: DOMString);
begin
  CharacterData.ReplaceData(offset, count, data);
end;

procedure TCSDOMCharacterData.set_data(const data: DOMString);
begin
  CharacterData.Data := data;
end;

function TCSDOMCharacterData.substringData(offset, count: Integer): DOMString;
begin
  Result := CharacterData.SubStringData(offset, count);
end;

{ TCSDOMAttr ----------------------------------------------------------------}

function TCSDOMAttr.GetAttribute: TXmlAttribute;
begin
  Result := Node as TXmlAttribute;
end;

function TCSDOMAttr.get_name: DOMString;
begin
  Result := Attribute.Name;
end;

function TCSDOMAttr.get_ownerElement: IDOMElement;
begin
  Result := nil;
end;

function TCSDOMAttr.get_specified: WordBool;
begin
  Result := Attribute.Specified;
end;

function TCSDOMAttr.get_value: DOMString;
begin
  Result := Attribute.Value;
end;

procedure TCSDOMAttr.set_value(const attributeValue: DOMString);
begin
  Attribute.Value := attributeValue;
end;

{ TCSDOMElement -------------------------------------------------------------}

function TCSDOMElement.GetElement: TXmlElement;
begin
  Result := Node as TXmlElement;
end;

function TCSDOMElement.get_tagName: DOMString;
begin
  Result := Element.TagName;
end;

function TCSDOMElement.getAttribute(const name: DOMString): DOMString;
begin
  Result := Element.GetAttribute(name);
end;

function TCSDOMElement.getAttributeNS(
  const namespaceURI, localName: DOMString): DOMString;
var
  Index: Integer;
begin
  Result := '';
  for Index := 0 to Element.Attributes.Length - 1 do
    if (Element.Attributes.Item(Index).BaseName = localName) and
        (Element.Attributes.Item(Index).Namespace = namespaceURI) then
    begin
      Result := Element.GetAttribute(Element.Attributes.Item(Index).NodeName);
      Exit;
    end;
end;

function TCSDOMElement.getAttributeNode(const name: DOMString): IDOMAttr;
begin
  Result := MakeNode(Element.GetAttributeNode(name), FOwnerDocument) as IDOMAttr;
end;

function TCSDOMElement.getAttributeNodeNS(
  const namespaceURI, localName: DOMString): IDOMAttr;
var
  Index: Integer;
begin
  Result := nil;
  for Index := 0 to Element.Attributes.Length - 1 do
    if (Element.Attributes.Item(Index).BaseName = localName) and
        (Element.Attributes.Item(Index).Namespace = namespaceURI) then
    begin
      Result := MakeNode(Element.Attributes.Item(Index), FOwnerDocument)
        as IDOMAttr;
      Exit;
    end;
end;

function TCSDOMElement.getElementsByTagName(const name: DOMString):
  IDOMNodeList;
begin
  Result := MakeNodeList(Element.GetElementsByTagName(name), FOwnerDocument);
end;

function TCSDOMElement.getElementsByTagNameNS(
  const namespaceURI, localName: DOMString): IDOMNodeList;
begin
  DOMVendorNotSupported('getElementsByTagNameNS', sCUEXml); { Do not localize }
end;

function TCSDOMElement.hasAttribute(const name: DOMString): WordBool;
begin
  Result := (getAttributeNode(name) <> nil);
end;

function TCSDOMElement.hasAttributeNS(
  const namespaceURI, localName: DOMString): WordBool;
begin
  Result := (getAttributeNodeNS(namespaceURI, localName) <> nil);
end;

procedure TCSDOMElement.removeAttribute(const name: DOMString);
begin
  Element.RemoveAttribute(name);
end;

function TCSDOMElement.removeAttributeNode(const oldAttr: IDOMAttr): IDOMAttr;
begin
  Result := MakeNode(Element.RemoveAttributeNode(
    GetNode(oldAttr) as TXmlAttribute), FOwnerDocument) as IDOMAttr;
end;

procedure TCSDOMElement.removeAttributeNS(
  const namespaceURI, localName: DOMString);
var
  Attr: IDOMAttr;
begin
  Attr := getAttributeNodeNS(localName, namespaceURI);
  if Attr <> nil then
    removeAttributeNode(Attr);
end;

procedure TCSDOMElement.setAttribute(const name, value: DOMString);
begin
  Element.SetAttribute(name, value);
end;

function TCSDOMElement.setAttributeNode(const newAttr: IDOMAttr): IDOMAttr;
begin
  Result := MakeNode(Element.SetAttributeNode(
    GetNode(newAttr) as TXmlAttribute), FOwnerDocument) as IDOMAttr;
end;

function TCSDOMElement.setAttributeNodeNS(const newAttr: IDOMAttr): IDOMAttr;
begin
  Result := MakeNode(Element.SetAttributeNode(
    GetNode(newAttr) as TXmlAttribute), FOwnerDocument) as IDOMAttr;
end;

procedure TCSDOMElement.setAttributeNS(
  const namespaceURI, qualifiedName, value: DOMString);
var
  Attr: IDOMAttr;
begin
  Attr := getAttributeNodeNS(namespaceURI, qualifiedName);
  if Attr <> nil then
    Attr.nodeValue := value
  else
    setAttribute(qualifiedName, value);
end;

procedure TCSDOMElement.normalize;
begin
  Element.Normalize;
end;

{ TCSDOMText ----------------------------------------------------------------}

function TCSDOMText.splitText(offset: Integer): IDOMText;
begin
  Result := MakeNode((Node as TXmlText).SplitText(offset), FOwnerDocument)
    as IDOMText;
end;

{ TCSDOMDocumentType --------------------------------------------------------}

function TCSDOMDocumentType.GetDocumentType: TXmlDocumentType;
begin
  Result := Node as TXmlDocumentType;
end;

function TCSDOMDocumentType.get_entities: IDOMNamedNodeMap;
begin
  Result := MakeNamedNodeMap(DocumentType.Entities, FOwnerDocument);
end;

function TCSDOMDocumentType.get_internalSubset: DOMString;
begin
  Result := DocumentType.XmlDocument;
end;

function TCSDOMDocumentType.get_name: DOMString;
begin
  Result := DocumentType.Name;
end;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?