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

Last change on this file since 22300 was 22300, checked in by ak19, 14 years ago
  1. Changes to get Fedora to work with Greenstone3: to let the Greenstone3 Reader Interface work with a Fedora Repository behind the scenes. 2. No longer returns XML Strings formatted for display, but unformatted, since when it's converted to XML DOM on the Greenstone end, new lines introduced due to whitespace interfere with Greenstone 3's default parsing of the XML.
File size: 4.4 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 a DOM node, this will return its String representation properly
91 * indented for display.
92 * @return a string representation of n, formatted for display.
93 * @param n is the DOM node to be converted to its string representation.
94 */
95 public static String displayAsString(Node n)
96 {
97 try {
98
99 DOMSource domSource = new DOMSource(n);
100 Transformer transformer = TransformerFactory.newInstance().newTransformer();
101 transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
102 //transformer.setOutputProperty(OutputKeys.METHOD, "xml");
103 //transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
104 transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
105 transformer.setOutputProperty(OutputKeys.INDENT, "yes");
106
107 StringWriter sw = new StringWriter();
108 transformer.transform(domSource, new StreamResult(sw));
109 return sw.toString();
110 } catch(Exception e) {
111 System.err.println("Couldn't display node as String.");
112 return "";
113 }
114
115 }
116
117
118}
Note: See TracBrowser for help on using the repository browser.