










Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!











CS 350 CS 350 –
Software Design
Lab 1 Lab 1 -
Part II
CS 350 CS 350 –
Software Design
Lab 1 Lab 1 -
Part II
CS 350 CS 350 –
Software Design
Lab 1 Lab 1 -
Part II
XmlConnection.javaXmlConnection.java
Copyright
(c)
Drexel
University.
All
rights
reserved.
Copyright
(c)
Drexel
University.
All
rights
reserved.
package
edu.drexel.cs350;
package
edu.drexel.cs350;
importimport
java.util.logging.Logger;java.util.logging.Logger
Base
class
for
all
those
that
use
an
data
source
Base
class
for
all
those
that
use
an
data
source
@author
Sunny
Huynh
@author
Sunny
Huynh
@version
@version
@since
@since
public
abstract
class
public
abstract
class
XmlConnection XmlConnection
protected
final
protected
final
XmlDataSource XmlDataSource
data;data;
protected
Logger
protected
Logger
loggerlogger;
protected protected
XmlConnection(finalXmlConnection(final
XmlDataSourceXmlDataSource
data) data)
this.data this.data
data;
data;
this.logger this.logger
Logger.getLogger(getClass().getName());Logger.getLogger(getClass().getName
CS 350 CS 350 –
Software Design
Lab 1 Lab 1 -
Part II
org.w3c.dom.Document;
import
org.w3c.dom.Document;
import
org.w3c.dom.Element;
import
org.w3c.dom.Element;
import
org.w3c.dom.Node;
import
org.w3c.dom.Node;
import
org.w3c.dom.NodeList;
import
org.w3c.dom.NodeList;
importimport
org.xml.sax.InputSourceorg.xml.sax.InputSource;
importimport
org.xml.sax.SAXException;org.xml.sax.SAXException
FascadeFascade
to
data
file
to
data
file
@author
Sunny
Huynh
@author
Sunny
Huynh
@version
@version
@since
@since
public
class
public
class
XmlDataSource XmlDataSource
protected
final
Logger
protected
final
Logger
logger logger;
protected
final
File
protected
final
File
filefile;
protected
final
Document
data;
protected
final
Document
data;
protected
final
protected
final
XPath XPath
xpathxpath;
CS 350 CS 350 –
Software Design
Lab 1 Lab 1 -
Part II
//Instantiates
a
Document
Builder
//Instantiates
a
Document
Builder
finalfinal
DocumentBuilder DocumentBuilder
builder
builder
DocumentBuilderFactory.newInstance().newDocumentBuilderDocumentBuilderFactory.newInstance().newDocumentBuilder();
//This
loads
the
file
stored
in
file
//This
loads
the
file
stored
in
file
data
data
builder.parse(filebuilder.parse(file);
CS 350 CS 350 –
Software Design
Lab 1 Lab 1 -
Part II
String
final
String
xpath xpath
String.format("%s String.format("%s/student"
/student"
"[@first "[@first-
-name
'%s'
and
@last
name
'%s'
and
@last-
-name
'%s']/@id",
name
'%s']/@id",
XmlDataSource.escapeXml(fname XmlDataSource.escapeXml(fname),
XmlDataSource.escapeXml(lname XmlDataSource.escapeXml(lname));
NodeList NodeList
nodes
nodes
data.getNodeList(xpath);data.getNodeList(xpath
intint[]
ids
new
ids
new
int[nodes.getLengthint[nodes.getLength()];
for
for
(int
int
i
i
i
i
ids.length;ids.length
++i)
++i)
ids[iids[i]
Integer.parseInt(nodes.item(i).getNodeValueInteger.parseInt(nodes.item(i).getNodeValue());
CS 350 CS 350 –
Software Design
Lab 1 Lab 1 -
Part II
Element
node
final
Element
node
getStudentNode(id getStudentNode(id);
(node
null)
if
(node
null)
throw
new
throw
new
SchedulerException("Student SchedulerException("Student
id
does
not
exist");
id
does
not
exist");
Element
root
final
Element
root
(Element)data.getNode(XML_ROOT
Element)data.getNode(XML_ROOT);
cascade
delete
cascade
delete
removeFromSchedule(idremoveFromSchedule(id);
CS 350 CS 350 –
Software Design
Lab 1 Lab 1 -
Part II
//If
a
filename
is
passed
as
a
string,
automatically
create
a
f
//If
a
filename
is
passed
as
a
string,
automatically
create
a
file
object
containing
the
ile
object
containing
the
filename
and
its
path
filename
and
its
path
//After
the
object
is
created,
the
alternate
constructor
i
//After
the
object
is
created,
the
alternate
constructor
is
called.
s
called.
public public
XmlDataSource(final XmlDataSource(final
String
path)
throws
String
path)
throws
SchedulerException SchedulerException
this(new this(new
File(pathFile(path));
public public
XmlDataSource(finalXmlDataSource(final
File
path)
throws
File
path)
throws
SchedulerExceptionSchedulerException
try
try
logger
logger
Logger.getLogger(getClass().getName());Logger.getLogger(getClass().getName
file
path;
file
path;
//Create
a
new
document
in
memory
//Create
a
new
document
in
memory
final final
DocumentBuilderDocumentBuilder
builder
builder
DocumentBuilderFactory.newInstance DocumentBuilderFactory.newInstance()
.newDocumentBuilder
newDocumentBuilder();
//Check
if
the
file
specified
in
the
file
obje
//Check
if
the
file
specified
in
the
file
object
exists
ct
exists
if
if
(file.exists
file.exists()
file.canReadfile.canRead())
//if
the
file
exists,
load
the
data
into
//if
the
file
exists,
load
the
data
into
the
document
the
document
data
data
builder.parse(file);builder.parse(file
else else
//otherwise
build
the
structure
via
//otherwise
build
the
structure
via
a
self
written
function
a
self
written
function
data
data
createDocument(builder);createDocument(builder
CS 350 CS 350 –
Software Design
Lab 1 Lab 1 -
Part II
private
Document
private
Document
createDocument(finalcreateDocument(final
DocumentBuilderDocumentBuilder
builder)builder)
throws throws
SAXException SAXException,
IOExceptionIOException
final
String
xml
"
/><courses
final
String
xml
"
/><courses
"<offerings
/><schedule
/>";
"<offerings
/><schedule
/>";
return return
builder.parse(new builder.parse(new
InputSource(newInputSource(new
StringReader(xml)));StringReader(xml
The
document
in
memory
is
stored
as
a
tree,
this
re
The
document
in
memory
is
stored
as
a
tree,
this
returns
the
root
of
the
turns
the
root
of
the
tree. tree.
public
Document
public
Document
getDocument getDocument()
return
data;
return
data;
//Creates
a
node,
given
a
name,
that
can
be
added
to
the
t
//Creates
a
node,
given
a
name,
that
can
be
added
to
the
tree
ree
public
Element
public
Element
createNode(final createNode(final
String
name)
String
name)
return return
data.createElement(name);data.createElement(name
//Some
nodes
have
values,
like
Description.
This
creates
a
//Some
nodes
have
values,
like
Description.
This
creates
a
node,
with
a
value,
that
node,
with
a
value,
that
can
be
added
to
the
tree.
can
be
added
to
the
tree.
public
Element
public
Element
createNode(final createNode(final
String
name,
final
String
value)
String
name,
final
String
value)
final
Element
node
final
Element
node
data.createElement(name);data.createElement(name
node.setTextContent(value node.setTextContent(value);
return
node;
return
node;
CS 350 CS 350 –
Software Design
Lab 1 Lab 1 -
Part II
//path
contains
the
//path
contains
the
Xpath Xpath,
aka
the
query.
aka
the
query.
getNodegetNode
returns
only
a
single
node
(from
the
returns
only
a
single
node
(from
the
root),
in
this
case
the
first
node
if
more
than
one
node
is
retu
root),
in
this
case
the
first
node
if
more
than
one
node
is
returned.
rned.
public
Node
public
Node
getNode(final getNode(final
String
path)
throws
String
path)
throws
XPathExceptionXPathException
//calls
the
"main"
//calls
the
"main"
getNode,getNode
passing
it
the
root
passing
it
the
root
return return
getNode(pathgetNode(path,
data);
data);
//path
contains
the
//path
contains
the
Xpath Xpath,
aka
the
query.
aka
the
query.
getNodegetNode
returns
only
a
single
node
(from
a
returns
only
a
single
node
(from
a
specified
node),
in
this
case
the
first
node
if
more
than
one
no
specified
node),
in
this
case
the
first
node
if
more
than
one
node
is
returned.
de
is
returned.
public
Node
public
Node
getNode(final getNode(final
String
path,
final
Node
root)
String
path,
final
Node
root)
throws throws
XPathExceptionXPathException
Compile
the
path
and
store
the
result
in
Compile
the
path
and
store
the
result
in
exprexpr
final final
XPathExpressionXPathExpression
exprexpr
xpath.compile(pathxpath.compile(path);
Perform
the
query
and
return
the
first
node
that
Perform
the
query
and
return
the
first
node
that
matches
the
criteria
matches
the
criteria
return
return
(Node)expr.evaluate(root
Node)expr.evaluate(root,
XPathConstants.NODE);XPathConstants.NODE
//path
contains
the
//path
contains
the
Xpath Xpath,
aka
the
query.
aka
the
query.
getNodegetNode
returns
only
a
collection
of
nodes
returns
only
a
collection
of
nodes
(from
the
root),
in
this
case
the
first
node
if
more
than
one
no
(from
the
root),
in
this
case
the
first
node
if
more
than
one
node
is
returned.
de
is
returned.
public public
NodeListNodeList
getNodeList(final getNodeList(final
String
path)
throws
String
path)
throws
XPathExceptionXPathException
return return
getNodeList(pathgetNodeList(path,
data);
data);