+
+ public static void sendStory(Story story, Object sender)
+ throws NoSuchFieldException, NoSuchMethodException,
+ ClassNotFoundException, IOException {
+
+ if (!story.getMeta().isImageDocument()) {
+ sendNextObject(sender, story);
+ return;
+ }
+
+ story = story.clone();
+
+ List<Chapter> chaps = story.getChapters();
+ story.setChapters(new ArrayList<Chapter>());
+ sendNextObject(sender, story);
+
+ for (Chapter chap : chaps) {
+ List<Paragraph> paras = chap.getParagraphs();
+ chap.setParagraphs(new ArrayList<Paragraph>());
+ sendNextObject(sender, chap);
+
+ for (Paragraph para : paras) {
+ sendNextObject(sender, para);
+ }
+ }
+ }
+
+ public static Story recStory(Object source) throws NoSuchFieldException,
+ NoSuchMethodException, ClassNotFoundException, IOException {
+
+ Story story = null;
+
+ Object obj = getNextObject(source);
+ if (obj instanceof Story) {
+ story = (Story) obj;
+
+ Chapter current = null;
+ for (obj = getNextObject(source); obj != null; obj = getNextObject(source)) {
+ if (obj instanceof Chapter) {
+ current = (Chapter) obj;
+ story.getChapters().add(current);
+ } else if (obj instanceof Paragraph) {
+ current.getParagraphs().add((Paragraph) obj);
+ }
+ }
+ }
+
+ return story;
+ }
+
+ private static Object getNextObject(Object clientOrServer)
+ throws NoSuchFieldException, NoSuchMethodException,
+ ClassNotFoundException, IOException {
+ if (clientOrServer instanceof ConnectActionClientObject) {
+ ConnectActionClientObject client = (ConnectActionClientObject) clientOrServer;
+ return client.send(null);
+ } else if (clientOrServer instanceof ConnectActionServerObject) {
+ ConnectActionServerObject server = (ConnectActionServerObject) clientOrServer;
+ Object obj = server.rec();
+ server.send(null);
+ return obj;
+ } else {
+ throw new ClassNotFoundException();
+ }
+ }
+
+ private static void sendNextObject(Object clientOrServer, Object obj)
+ throws NoSuchFieldException, NoSuchMethodException,
+ ClassNotFoundException, IOException {
+ if (clientOrServer instanceof ConnectActionClientObject) {
+ ConnectActionClientObject client = (ConnectActionClientObject) clientOrServer;
+ client.send(obj);
+ } else if (clientOrServer instanceof ConnectActionServerObject) {
+ ConnectActionServerObject server = (ConnectActionServerObject) clientOrServer;
+ server.send(obj);
+ server.rec();
+ } else {
+ throw new ClassNotFoundException();
+ }
+ }