Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
32bf12eb
Commit
32bf12eb
authored
Sep 24, 2000
by
Lars Gustäbel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Updated to final Attributes interface (patch 101632).
parent
e84bf751
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
72 additions
and
13 deletions
+72
-13
Lib/xml/sax/expatreader.py
Lib/xml/sax/expatreader.py
+17
-4
Lib/xml/sax/xmlreader.py
Lib/xml/sax/xmlreader.py
+55
-9
No files found.
Lib/xml/sax/expatreader.py
View file @
32bf12eb
...
...
@@ -20,6 +20,9 @@ from xml.sax._exceptions import *
from
xml.parsers
import
expat
from
xml.sax
import
xmlreader
AttributesImpl
=
xmlreader
.
AttributesImpl
AttributesNSImpl
=
xmlreader
.
AttributesNSImpl
# --- ExpatParser
class
ExpatParser
(
xmlreader
.
IncrementalParser
,
xmlreader
.
Locator
):
...
...
@@ -31,7 +34,6 @@ class ExpatParser(xmlreader.IncrementalParser, xmlreader.Locator):
self
.
_parser
=
None
self
.
_namespaces
=
namespaceHandling
self
.
_parsing
=
0
self
.
_attrs
=
xmlreader
.
AttributesImpl
({},
{})
# XMLReader methods
...
...
@@ -137,7 +139,7 @@ class ExpatParser(xmlreader.IncrementalParser, xmlreader.Locator):
# event handlers
def
start_element
(
self
,
name
,
attrs
):
self
.
_cont_handler
.
startElement
(
name
,
self
.
_attrs
)
self
.
_cont_handler
.
startElement
(
name
,
AttributesImpl
(
attrs
)
)
def
end_element
(
self
,
name
):
self
.
_cont_handler
.
endElement
(
name
)
...
...
@@ -147,12 +149,23 @@ class ExpatParser(xmlreader.IncrementalParser, xmlreader.Locator):
if
len
(
pair
)
==
1
:
pair
=
(
None
,
name
)
self
.
_cont_handler
.
startElementNS
(
pair
,
None
,
self
.
_attrs
)
newattrs
=
{}
for
(
aname
,
value
)
in
attrs
.
items
():
apair
=
aname
.
split
()
if
len
(
apair
)
==
1
:
apair
=
(
None
,
aname
)
else
:
apair
=
tuple
(
apair
)
newattrs
[
apair
]
=
value
self
.
_cont_handler
.
startElementNS
(
pair
,
None
,
AttributesNSImpl
(
newattrs
,
{}))
def
end_element_ns
(
self
,
name
):
pair
=
name
.
split
()
if
len
(
pair
)
==
1
:
name
=
(
None
,
name
)
pair
=
(
None
,
name
)
self
.
_cont_handler
.
endElementNS
(
pair
,
None
)
...
...
Lib/xml/sax/xmlreader.py
View file @
32bf12eb
...
...
@@ -151,6 +151,7 @@ class IncrementalParser(XMLReader):
raise
NotImplementedError
(
"This method must be implemented!"
)
# ===== LOCATOR =====
class
Locator
:
"""Interface for associating a SAX event with a document
location. A locator object will return valid results only during
...
...
@@ -173,11 +174,15 @@ class Locator:
"Return the system identifier for the current event."
return
None
# --- AttributesImpl
# ===== ATTRIBUTESIMPL =====
class
AttributesImpl
:
def
__init__
(
self
,
attrs
,
rawnames
):
def
__init__
(
self
,
attrs
):
"""Non-NS-aware implementation.
attrs should be of the form {name : value}."""
self
.
_attrs
=
attrs
self
.
_rawnames
=
rawnames
def
getLength
(
self
):
return
len
(
self
.
_attrs
)
...
...
@@ -189,16 +194,23 @@ class AttributesImpl:
return
self
.
_attrs
[
name
]
def
getValueByQName
(
self
,
name
):
return
self
.
_attrs
[
self
.
_rawnames
[
name
]
]
return
self
.
_attrs
[
name
]
def
getNameByQName
(
self
,
name
):
return
self
.
_rawnames
[
name
]
if
not
self
.
_attrs
.
has_key
(
name
):
raise
KeyError
return
name
def
getQNameByName
(
self
,
name
):
if
not
self
.
_attrs
.
has_key
(
name
):
raise
KeyError
return
name
def
getNames
(
self
):
return
self
.
_attrs
.
keys
()
def
getQNames
(
self
):
return
self
.
_
rawname
s
.
keys
()
return
self
.
_
attr
s
.
keys
()
def
__len__
(
self
):
return
len
(
self
.
_attrs
)
...
...
@@ -216,7 +228,7 @@ class AttributesImpl:
return
self
.
_attrs
.
get
(
name
,
alternative
)
def
copy
(
self
):
return
self
.
__class__
(
self
.
_attrs
,
self
.
_rawnames
)
return
self
.
__class__
(
self
.
_attrs
)
def
items
(
self
):
return
self
.
_attrs
.
items
()
...
...
@@ -224,12 +236,46 @@ class AttributesImpl:
def
values
(
self
):
return
self
.
_attrs
.
values
()
# ===== ATTRIBUTESNSIMPL =====
class
AttributesNSImpl
(
AttributesImpl
):
def
__init__
(
self
,
attrs
,
qnames
):
"""NS-aware implementation.
attrs should be of the form {(ns_uri, lname): value, ...}.
qnames of the form {(ns_uri, lname): qname, ...}."""
self
.
_attrs
=
attrs
self
.
_qnames
=
qnames
def
getValueByQName
(
self
,
name
):
for
(
nsname
,
qname
)
in
self
.
_qnames
.
items
():
if
qname
==
name
:
return
self
.
_attrs
[
nsname
]
raise
KeyError
def
getNameByQName
(
self
,
name
):
for
(
nsname
,
qname
)
in
self
.
_qnames
.
items
():
if
qname
==
name
:
return
nsname
raise
KeyError
def
getQNameByName
(
self
,
name
):
return
self
.
_qnames
[
name
]
def
getQNames
(
self
):
return
self
.
_qnames
.
values
()
def
copy
(
self
):
return
self
.
__class__
(
self
.
_attrs
,
self
.
_qnames
)
def
_test
():
XMLReader
()
IncrementalParser
()
Locator
()
AttributesImpl
()
if
__name__
==
"__main__"
:
_test
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment