source: other-projects/gs3-webservices-java-client/trunk/src/GS3Fedora/org/greenstone/fedora/services/FedoraCommons.java@ 21573

Last change on this file since 21573 was 15222, checked in by ak19, 16 years ago

Greenstone3 web services demo-clientadded to GS3's other-projects

File size: 4.3 KB
Line 
1/**
2 *#########################################################################
3 * FedoraCommons.java - works with the demo-client for Greenstone 3, of
4 * the Greenstone digital library suite from the New Zealand Digital
5 * Library Project at the * University of Waikato, New Zealand.
6 * <BR><BR>
7 * Copyright (C) 2008 New Zealand Digital Library Project
8 * <BR><BR>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 * <BR><BR>
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *########################################################################
19 */
20
21package org.greenstone.fedora.services;
22
23import java.io.IOException;
24import java.io.StringReader;
25import java.io.StringWriter;
26
27import javax.xml.parsers.DocumentBuilder;
28import javax.xml.transform.OutputKeys;
29import javax.xml.transform.Transformer;
30import javax.xml.transform.TransformerException;
31import javax.xml.transform.TransformerFactory;
32import javax.xml.transform.dom.DOMSource;
33import javax.xml.transform.stream.StreamResult;
34
35import org.w3c.dom.Document;
36import org.w3c.dom.Element;
37import org.w3c.dom.Node;
38import org.xml.sax.InputSource;
39import org.xml.sax.SAXException;
40
41/** Methods common to both Fedora(GS3)Connection and GSearchConnection.
42 * Made static here so they can be reused by these classes.
43 * @author ak19
44*/
45public class FedoraCommons {
46 /** Extract the text from an element, if any.
47 * @return the text that's nested in an element's body or ""
48 * if there's none.
49 * @param e is the element whose value is to be extracted.*/
50 public static String getValue(Element e) {
51 Node child = e.getFirstChild();
52 return (child == null) ? "" : child.getNodeValue();
53 }
54
55 /** Turns the XML String into a DOM tree and returns it.
56 * @param XML the string to be converted into a DOM tree
57 * @param builder is the DocumentBuilder to use to parse the string XML
58 * @return the root element of the document by parsing the string XML
59 */
60 public static Element getResponseAsDOM(DocumentBuilder builder, String XML)
61 throws SAXException, IOException
62 {
63 Element docEl = null;
64 // turn the String xml into a DOM tree:
65 Document doc = builder.parse(new InputSource(new StringReader(XML)));
66 docEl = doc.getDocumentElement();
67 return docEl;
68 }
69
70 /** Given an Element, this will return its String representation without
71 * indenting it for display.
72 * @return a string representation of e without formatting it for display.
73 * @param e is the element to be converted to its string representation.
74 */
75 public static String elementToString(Element e)
76 throws TransformerException
77 {
78 DOMSource domSource = new DOMSource(e);
79 Transformer transformer
80 = TransformerFactory.newInstance().newTransformer();
81 transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
82 //transformer.setOutputProperty(OutputKeys.METHOD, "xml");
83 //transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
84
85 StringWriter sw = new StringWriter();
86 transformer.transform(domSource, new StreamResult(sw));
87 return sw.toString();
88 }
89
90 /** Given an Element, this will return its String representation properly
91 * indented for display.
92 * @return a string representation of e, formatted for display.
93 * @param e is the element to be converted to its string representation.
94 */
95 public static String elementToFormattedString(Element e)
96 throws TransformerException
97 {
98 DOMSource domSource = new DOMSource(e);
99 Transformer transformer = TransformerFactory.newInstance().newTransformer();
100 transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
101 //transformer.setOutputProperty(OutputKeys.METHOD, "xml");
102 //transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
103 transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
104 transformer.setOutputProperty(OutputKeys.INDENT, "yes");
105
106 StringWriter sw = new StringWriter();
107 transformer.transform(domSource, new StreamResult(sw));
108 return sw.toString();
109 }
110}
Note: See TracBrowser for help on using the repository browser.