feiyu02
2024-09-25 0516cba27e632f20efac2752787f38f0c87baafa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
 *  Copyright 2014, Plutext Pty Ltd.
 *   
 *  This file is part of docx4j.
 
    docx4j is licensed under the Apache License, Version 2.0 (the "License"); 
    you may not use this file except in compliance with the License. 
 
    You may obtain a copy of the License at 
 
        http://www.apache.org/licenses/LICENSE-2.0 
 
    Unless required by applicable law or agreed to in writing, software 
    distributed under the License is distributed on an "AS IS" BASIS, 
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
    See the License for the specific language governing permissions and 
    limitations under the License.
 
 */
 
package cn.flightfeather.supervision.docx4j.simpleDemo;
 
 
import net.engio.mbassy.bus.MBassador;
import net.engio.mbassy.bus.config.BusConfiguration;
import net.engio.mbassy.bus.config.Feature;
import net.engio.mbassy.listener.Handler;
import org.docx4j.Docx4J;
import org.docx4j.events.Docx4jEvent;
import org.docx4j.events.PackageIdentifierTransient;
import org.docx4j.events.StartEvent;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.samples.AbstractSample;
 
import java.io.File;
 
 
/**
 * Simple example of monitoring docx4j events.
 * 
 * Uses the following library https://github.com/bennidi/mbassador 
 * to provide the event plumbing; see that page for more details about it.
 * 
 * Get the jar from http://search.maven.org/#artifactdetails%7Cnet.engio%7Cmbassador%7C1.1.10%7Cbundle
 * 
 * 
 * @author jharrop
 * @since 3.1.0
 */
public class EventMonitoringDemo extends AbstractSample {
    
    public static void main(String[] args) throws Exception {
 
        try {
            getInputFilePath(args);
        } catch (IllegalArgumentException e) {
            inputfilepath = System.getProperty("user.dir") + "/sample-docs/word/sample-docx.docx";
        }
        System.out.println(inputfilepath);       
        
        // Creation of message bus
        MBassador<Docx4jEvent> bus = new MBassador<Docx4jEvent>(
                new BusConfiguration()
                 .addFeature(Feature.SyncPubSub.Default()) // configure the synchronous message publication
                 .addFeature(Feature.AsynchronousHandlerInvocation.Default()) // configure asynchronous invocation of handlers
                 .addFeature(Feature.AsynchronousMessageDispatch.Default()) // configure asyncronous message publication (fire&forget)
                 );
        Docx4jEvent.setEventNotifier(bus);
        
        //  and registration of listeners
        ListeningBean listener = new ListeningBean();
        bus.subscribe(listener);        
        
        
        // Approach 1: Load the docx from a file 
        // In this case the events get a name for the events related to this package from the filename
        //WordprocessingMLPackage wordMLPackage = Docx4J.load(new java.io.File(inputfilepath));
 
        // Approach 2: Load the docx from a file 
        // In this case the events get a name for the events related to this package from the filename
        PackageIdentifierTransient pkgIdentifier = new PackageIdentifierTransient("templateXYZ");
        WordprocessingMLPackage wordMLPackage = Docx4J.load(pkgIdentifier, new File(inputfilepath));
        
        // Approach 3: Load the docx from an input stream
        // In this case, without more, we can't identify which pkg the events belong to
        // so a unique identifier is automatically assigned
//        FileInputStream fis = new FileInputStream(new java.io.File(inputfilepath));
//        WordprocessingMLPackage wordMLPackage = Docx4J.load(fis);
 
        // Approach 4:  Load the docx from an input stream
        // assigning your identifier
//        FileInputStream fis = new FileInputStream(new java.io.File(inputfilepath));
//        PackageIdentifierTransient pkgIdentifier = new PackageIdentifierTransient("myID");
//        WordprocessingMLPackage wordMLPackage = Docx4J.load(pkgIdentifier, fis);
        
        // Save it
        String outputfilepath = System.getProperty("user.dir") + "/OUT_OpenAndSaveRoundTripTest.docx";
        Docx4J.save(wordMLPackage, new File(outputfilepath), Docx4J.FLAG_NONE); //(FLAG_NONE == default == zipped docx)
        
        System.out.println("Saved: " + outputfilepath);
    }
        
    /**
     * Implement your net.engio.mbassy.listener.Handler 
     *
     */
    static class ListeningBean {
        
         // every message of type Docx4jEvent  will be delivered
        // to this handler; NPEs etc in this handler will be silently ignored.
        @Handler
        public void handleMessage(Docx4jEvent message) {
            
            String state = (message instanceof StartEvent) ? "starting" : "finished";
            
            
            
            if (message.getPkgIdentifier()==null) {
 
                System.out.println("\n\n\n\n ****  " + message.getProcessStep() + " " + state + " ***** \n\n");
                
            } else {
 
                System.out.println("\n\n\n\n **** " + message.getPkgIdentifier().name() + ": " 
                        + message.getProcessStep() + " " + state + " ***** \n\n");
                
            }
                
        }
        
    }    
 
}