01: // AdminReader.java
02: // $Id: AdminReader.java,v 1.15 2000/08/17 09:57:41 ylafon Exp $
03: // (c) COPYRIGHT MIT and INRIA, 1997.
04: // Please first read the full copyright statement in file COPYRIGHT.html
05:
06: package org.w3c.jigsaw.admin;
07:
08: import java.io.BufferedReader;
09: import java.io.IOException;
10: import java.io.InputStream;
11: import java.io.InputStreamReader;
12: import java.io.Reader;
13:
14: import java.net.URL;
15:
16: import org.w3c.tools.resources.Resource;
17:
18: import org.w3c.tools.resources.serialization.ResourceDescription;
19: import org.w3c.tools.resources.serialization.SerializationException;
20: import org.w3c.tools.resources.serialization.Serializer;
21:
22: class AdminReader implements AdminProtocol {
23: /**
24: * The RemoteResource factory to create remote resource instances.
25: */
26: RemoteResourceFactory factory = null;
27: /**
28: * The client side admin object we are attached to.
29: */
30: protected AdminContext admin = null;
31:
32: /**
33: * Our serializer.
34: */
35: protected static Serializer serializer = null;
36:
37: static {
38: serializer = new org.w3c.tools.resources.serialization.xml.XMLSerializer();
39: }
40:
41: protected RemoteResource readResource(URL parent,
42: String identifier, InputStream in) throws IOException,
43: AdminProtocolException {
44: try {
45: Reader reader = new BufferedReader(
46: new InputStreamReader(in));
47: ResourceDescription descriptions[] = serializer
48: .readResourceDescriptions(reader);
49: if (descriptions.length < 1)
50: throw new AdminProtocolException("Unknown resource");
51: return factory.createRemoteResource(parent, identifier,
52: descriptions[0]);
53: } catch (SerializationException ex) {
54: throw new AdminProtocolException(
55: "Error in serialized resource :" + ex.getMessage());
56: }
57: }
58:
59: public static Resource readResource(InputStream in)
60: throws IOException, AdminProtocolException {
61: try {
62: Reader reader = new BufferedReader(
63: new InputStreamReader(in));
64: Resource resources[] = serializer.readResources(reader);
65: if (resources.length < 1)
66: throw new AdminProtocolException("No resource found.");
67: return resources[0];
68: } catch (SerializationException ex) {
69: throw new AdminProtocolException(
70: "Error in serialized resource :" + ex.getMessage());
71: }
72: }
73:
74: public static ResourceDescription readResourceDescription(
75: InputStream in) throws IOException, AdminProtocolException {
76: try {
77: Reader reader = new BufferedReader(
78: new InputStreamReader(in));
79: ResourceDescription descriptions[] = serializer
80: .readResourceDescriptions(reader);
81: if (descriptions.length < 1) {
82: throw new AdminProtocolException("No resource found.");
83: }
84: return descriptions[0];
85: } catch (SerializationException ex) {
86: throw new AdminProtocolException(
87: "Error in serialized resource :" + ex.getMessage());
88: }
89: }
90:
91: AdminReader(AdminContext admin) {
92: this .admin = admin;
93: this .factory = new RemoteResourceFactory(admin);
94: }
95:
96: }
|