wxtutils
Class accessutils

java.lang.Object
  extended bywxtutils.accessutils

public class accessutils
extends java.lang.Object

accessutils has only static methods and serve as a library for utilities that loads and saves data, calculate addresses and do some parsing.


Constructor Summary
accessutils()
          Creates a new instance of accessutils
 
Method Summary
static boolean appendToTextFile(java.net.URI theUri, java.lang.String text, long maxFileSize)
          Append to a text file
static java.net.URI calculateLocalUri(java.net.URI theUri, java.net.URI thePageUri)
          Find where theUri should be backed up.
static org.w3c.dom.DocumentFragment expandFragmentFromAnyNode(org.w3c.dom.Node n)
          Makes a DocumentFragment from the given node.
static java.lang.String expandStringWithProperty(java.lang.String theValue, java.util.HashMap props)
          Identifies a _wx_property in a string and expands it.
static boolean fetchByteFile(java.net.URI fromUri, java.net.URI toUri)
          Copy a file from one URI to an other, which must be a file
static java.lang.String findEncoding(java.lang.String S, java.lang.String defaultEncoding)
          Investigate a string and find encoding This is not a full-blown version.
static java.lang.String getAllText(org.w3c.dom.Node n)
          Get content of all text-elements which are direct children of a node
static java.lang.String getAllTextAndCdata(org.w3c.dom.Node n)
          Get content of all text-elements and CDATA-elements which are direct children of a node
static java.net.URI getCorrectedLink(java.lang.String path, java.net.URI baseUri, java.util.Vector altUris)
          Produce an relative URI to a given anchor from a relative path to an other anchor.
static java.net.URI getExistingAbsLink(java.lang.String path, java.util.Vector altUris)
          Try to find an existing absolute URI from a relative path and alternative (absolute) anchors
static long getModificationTime(java.net.URI theUri)
          Find out when a file identified by an URI was last modified
static java.lang.String getNormalizedFilePath(java.lang.String path)
          Identify absolute paths and correct paths to neutral form.
static java.util.Vector getPIs(org.w3c.dom.Node n)
          Find a list of all Processing Instructions below a node in a tree
static java.util.Vector getPIs(org.w3c.dom.Node n, java.lang.String target)
          Produce a list of all Processing Instructions with a certain name below a node in a tree
static java.net.URI getRelativeUri(java.net.URI newBase, java.net.URI theUri)
          Find reference from one URI to another
static java.lang.String getTextFile(java.net.URI uri)
          Read a String from an URI Throws IOException , URISyntaxException
static java.lang.String getTextFileFromPath(java.lang.String path)
          Read a String from a path Throws IOException , URISyntaxException
static boolean isCommonAttribute(java.lang.String att, java.lang.String common)
          Find out if a string is starting with common Allow any number of ../ before common, but not common as other part of the string is identified
static boolean legalDating(java.lang.String firstdate, java.lang.String lastdate)
          Control dates
static boolean makeCatalog(java.net.URI theUri)
          Check if a file exists, and make necessary catalogpath if it does not
static org.w3c.dom.DocumentFragment makeDate(org.w3c.dom.Document doc, command cmd)
          Produce a Documentfragment with a single textnode telling the date
static org.w3c.dom.DocumentFragment makeFragement(org.w3c.dom.Node n)
          Makes a DocumentFragment with all the children of the given node, or the node itself if it is has no children.
static boolean makeResource(java.net.URI theUri)
          Establish a file
static org.w3c.dom.DocumentFragment makeStamp(org.w3c.dom.Document doc, command cmd)
          Produce a Documentfragment with a referene, a-Element, to WXT as its only child.
static org.w3c.dom.DocumentFragment makeTime(org.w3c.dom.Document doc, command cmd)
          Produce a Documentfragment with a single textnode telling the time
static java.util.HashMap parseNameValues(java.lang.String data, char quoteMark)
          Parse for name-value pairs in a string and return them in a HashMap Expected input variants: (name1='value1',name2='value2') name1='value1',name2='value2' name1='value1' name2='value2' name1="value1" name2="value2"
static boolean resourceExists(java.net.URI theUri)
          Find out if a certain URI really address an existing resource, file or webpage
static boolean saveTextFile(java.net.URI theUri, java.lang.String text)
          Save a text file
static void setToDay()
          Set the day to today
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

accessutils

public accessutils()
Creates a new instance of accessutils

Method Detail

setToDay

public static void setToDay()
Set the day to today


getAllText

public static java.lang.String getAllText(org.w3c.dom.Node n)
Get content of all text-elements which are direct children of a node

Parameters:
n - The node whose children are searched
Returns:
the collected string

getAllTextAndCdata

public static java.lang.String getAllTextAndCdata(org.w3c.dom.Node n)
Get content of all text-elements and CDATA-elements which are direct children of a node

Parameters:
n - The node whose children are searched
Returns:
the collected string

parseNameValues

public static java.util.HashMap parseNameValues(java.lang.String data,
                                                char quoteMark)
Parse for name-value pairs in a string and return them in a HashMap

Expected input variants:

(name1='value1',name2='value2')

name1='value1',name2='value2'

name1='value1' name2='value2'

name1="value1" name2="value2"

Parameters:
data - The string to parse
quoteMark - How values are quoted: ' or "
Returns:
a HashMap with names as keys

expandStringWithProperty

public static java.lang.String expandStringWithProperty(java.lang.String theValue,
                                                        java.util.HashMap props)
Identifies a _wx_property in a string and expands it. Expect: "//image[position()='_wxt_property(index)']/description"

Parameters:
theValue - The string to search
props - The candidate properties
Returns:
the expanded string

getNormalizedFilePath

public static java.lang.String getNormalizedFilePath(java.lang.String path)
Identify absolute paths and correct paths to neutral form. Add scheme file to absolutes without scheme

c\:a\b\c.xxx - to - file:/c:/a/b/c.xxx

/a/b/c.xxx - to - file://a/b/c.xxx

FILE:/a/b/c.xxx - to - file://a/b/c.xxx

Parameters:
path - The path to investigate
Returns:
the new form if absolute, else path as input

isCommonAttribute

public static boolean isCommonAttribute(java.lang.String att,
                                        java.lang.String common)
Find out if a string is starting with common

Allow any number of ../ before common, but not common as other part of the string is identified

Parameters:
att - The string to investigate
common - The name to look for
Returns:
true if common is found, else false

getTextFile

public static java.lang.String getTextFile(java.net.URI uri)
                                    throws java.io.IOException,
                                           java.net.MalformedURLException
Read a String from an URI Throws IOException , URISyntaxException

Parameters:
uri - The URI to read from
Returns:
the string read
Throws:
java.io.IOException - when the text cannot be loaded
java.net.MalformedURLException - when the URI is not wellformed

getTextFileFromPath

public static java.lang.String getTextFileFromPath(java.lang.String path)
                                            throws java.io.IOException,
                                                   java.net.URISyntaxException
Read a String from a path Throws IOException , URISyntaxException

Parameters:
path - The path to read from
Returns:
the string read
Throws:
java.io.IOException - when text is not loaded
java.net.URISyntaxException - when path has bad form

makeCatalog

public static boolean makeCatalog(java.net.URI theUri)
Check if a file exists, and make necessary catalogpath if it does not

Parameters:
theUri - The URI for the file
Returns:
true if the catalogpath exists when we leave, false otherwise

saveTextFile

public static boolean saveTextFile(java.net.URI theUri,
                                   java.lang.String text)
Save a text file

Parameters:
theUri - The absolute URI to the file
text - The text to write
Returns:
true if save goes ok, false otherwise

appendToTextFile

public static boolean appendToTextFile(java.net.URI theUri,
                                       java.lang.String text,
                                       long maxFileSize)
Append to a text file

Parameters:
theUri - The absolute URI to the file
text - The text to write
maxFileSize - When file reach this size in bytes it is reset, -1 keeps it going forever
Returns:
true if save goes ok, false otherwise

getModificationTime

public static long getModificationTime(java.net.URI theUri)
Find out when a file identified by an URI was last modified

Parameters:
theUri - The resource we will investigate
Returns:
the actual point in time if we can get it, 0 otherwise

findEncoding

public static java.lang.String findEncoding(java.lang.String S,
                                            java.lang.String defaultEncoding)
Investigate a string and find encoding

This is not a full-blown version. It depends on the ability to read and parse the first line in the file as an XML-heading with an encoding attribute as "normal" String.

Parameters:
defaultEncoding - The encoding to use if we are not able to detect encoding in the string
S - The string to investigate
Returns:
the encoding found

resourceExists

public static boolean resourceExists(java.net.URI theUri)
Find out if a certain URI really address an existing resource, file or webpage

Parameters:
theUri - The URI we are looking for
Returns:
true if the file exists,false otherwise

makeResource

public static boolean makeResource(java.net.URI theUri)
Establish a file

Parameters:
theUri - The URI describing the file we want to establish
Returns:
true if we can establish the file, false else

getPIs

public static java.util.Vector getPIs(org.w3c.dom.Node n,
                                      java.lang.String target)
Produce a list of all Processing Instructions with a certain name below a node in a tree

Parameters:
n - The node defining the subtree we will investigate
target - The name of the PIs we are looking for
Returns:
a list of PI-nodes

getPIs

public static java.util.Vector getPIs(org.w3c.dom.Node n)
Find a list of all Processing Instructions below a node in a tree

Parameters:
n - The node defining the subtree
Returns:
a list of PI-nodes

getRelativeUri

public static java.net.URI getRelativeUri(java.net.URI newBase,
                                          java.net.URI theUri)
Find reference from one URI to another

Parameters:
newBase - An absolute URI
theUri - An absolute URI
Returns:
the address of theUri as seen from newBase

calculateLocalUri

public static java.net.URI calculateLocalUri(java.net.URI theUri,
                                             java.net.URI thePageUri)
Find where theUri should be backed up. A path is constructed in a unique way from the given URI.

Parameters:
theUri - The absolute address of a file
thePageUri - The absolute uri for the page that address the resource at theUri
Returns:
The uri for a proper URI for local copies.

makeFragement

public static org.w3c.dom.DocumentFragment makeFragement(org.w3c.dom.Node n)
                                                  throws java.lang.Exception
Makes a DocumentFragment with all the children of the given node, or the node itself if it is has no children.

Parameters:
n - The node that defines the subtree
Returns:
the produced fragment
Throws:
java.lang.Exception - when for some reason we could not establish the fragment

expandFragmentFromAnyNode

public static org.w3c.dom.DocumentFragment expandFragmentFromAnyNode(org.w3c.dom.Node n)
                                                              throws java.lang.Exception
Makes a DocumentFragment from the given node. If the node is a CDATA-node, the value is expanded and parsed as a string If not see: makeFragement

Parameters:
n - The node that defines the subtree
Returns:
the produced DocumentFragment
Throws:
java.lang.Exception - when for some reason we could not establish the fragment

makeDate

public static org.w3c.dom.DocumentFragment makeDate(org.w3c.dom.Document doc,
                                                    command cmd)
Produce a Documentfragment with a single textnode telling the date

Parameters:
doc - The Document that will own the element
cmd - The command object that idenities the form we want
Returns:
The ready prepared DocumentFragment wrapping a Text node

makeTime

public static org.w3c.dom.DocumentFragment makeTime(org.w3c.dom.Document doc,
                                                    command cmd)
Produce a Documentfragment with a single textnode telling the time

Parameters:
doc - The Document that will own the element
cmd - The command object that idenities the form we want
Returns:
The ready prepared DocumentFragment wrapping a Text node

makeStamp

public static org.w3c.dom.DocumentFragment makeStamp(org.w3c.dom.Document doc,
                                                     command cmd)
Produce a Documentfragment with a referene, a-Element, to WXT as its only child.

Parameters:
doc - The Document that will own the element
cmd - The command object that decribes what we want
Returns:
The ready prepared DocumentFragment wrapping an a-element

fetchByteFile

public static boolean fetchByteFile(java.net.URI fromUri,
                                    java.net.URI toUri)
Copy a file from one URI to an other, which must be a file

Parameters:
fromUri - Where to copy from
toUri - Where to copy to, must be file
Returns:
true if this went ok, else false

getCorrectedLink

public static java.net.URI getCorrectedLink(java.lang.String path,
                                            java.net.URI baseUri,
                                            java.util.Vector altUris)
Produce an relative URI to a given anchor from a relative path to an other anchor.

Parameters:
path - The string defining the URI we want to correct
baseUri - The uri we will use as an anchor and relativize against
altUris - An array of uris we suspect may be current base
Returns:
The relative URI, relative to baseUri. An URI based on path if path is absolute or if we cannot find an existing relative URI.

getExistingAbsLink

public static java.net.URI getExistingAbsLink(java.lang.String path,
                                              java.util.Vector altUris)
                                       throws java.lang.Exception
Try to find an existing absolute URI from a relative path and alternative (absolute) anchors

Parameters:
path - The path that we will test
altUris - The list of alternative absolute URIs
Returns:
The absolute URI based on path and one of he alternatives if we succed ,null if path does not make sence, URI from path if path is absolute already
Throws:
java.lang.Exception - when no existing file can be found by any alternative or the path is misformed

legalDating

public static boolean legalDating(java.lang.String firstdate,
                                  java.lang.String lastdate)
                           throws java.lang.NumberFormatException
Control dates

Parameters:
firstdate - First legal day
lastdate - Last legal day
Returns:
true if today is within span
Throws:
java.lang.NumberFormatException - when the dateformats is wrong. Should be:yyyymmdd