blob: 18d787504a02854e7c1c014c25f89ec22a15db7f (
plain)
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
|
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You 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 org.apache.poi.xwpf.usermodel;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.Map;
import javax.xml.namespace.QName;
import org.apache.poi.POIXMLDocumentPart;
import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.openxml4j.opc.PackageRelationship;
import org.apache.xmlbeans.XmlOptions;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDocProtect;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSettings;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTZoom;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STDocProtect;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STOnOff;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.SettingsDocument;
public class XWPFSettings extends POIXMLDocumentPart {
private CTSettings ctSettings;
public XWPFSettings(PackagePart part, PackageRelationship rel) throws IOException {
super(part, rel);
}
public XWPFSettings() {
super();
ctSettings = CTSettings.Factory.newInstance();
}
@Override
protected void onDocumentRead() throws IOException
{
super.onDocumentRead();
readFrom(getPackagePart().getInputStream());
}
/**
* Set zoom.<br/>
* In the zoom tag inside settings.xml file <br/>
* it sets the value of zoom
* <br/>
* sample snippet from settings.xml
* <pre>
* <w:zoom w:percent="50" />
* <pre>
* @return percentage as an integer of zoom level
*/
public long getZoomPercent() {
CTZoom zoom;
if (!ctSettings.isSetZoom()) {
zoom = ctSettings.addNewZoom();
} else {
zoom = ctSettings.getZoom();
}
return zoom.getPercent().longValue();
}
/**
* Set zoom.<br/>
* In the zoom tag inside settings.xml file <br/>
* it sets the value of zoom
* <br/>
* sample snippet from settings.xml
* <pre>
* <w:zoom w:percent="50" />
* <pre>
* @return percentage as an integer of zoom level
*/
public void setZoomPercent(long zoomPercent) {
if (! ctSettings.isSetZoom()) {
ctSettings.addNewZoom();
}
CTZoom zoom = ctSettings.getZoom();
zoom.setPercent(BigInteger.valueOf(zoomPercent));
}
/**
* Verifies the documentProtection tag inside settings.xml file <br/>
* if the protection is enforced (w:enforcement="1") <br/>
* and if the kind of protection equals to passed (STDocProtect.Enum editValue) <br/>
*
* <br/>
* sample snippet from settings.xml
* <pre>
* <w:settings ... >
* <w:documentProtection w:edit="readOnly" w:enforcement="1"/>
* </pre>
*
* @return true if documentProtection is enforced with option readOnly
*/
public boolean isEnforcedWith(STDocProtect.Enum editValue) {
CTDocProtect ctDocProtect = ctSettings.getDocumentProtection();
if (ctDocProtect == null) {
return false;
}
return ctDocProtect.getEnforcement().equals(STOnOff.X_1) && ctDocProtect.getEdit().equals(editValue);
}
/**
* Enforces the protection with the option specified by passed editValue.<br/>
* <br/>
* In the documentProtection tag inside settings.xml file <br/>
* it sets the value of enforcement to "1" (w:enforcement="1") <br/>
* and the value of edit to the passed editValue (w:edit="[passed editValue]")<br/>
* <br/>
* sample snippet from settings.xml
* <pre>
* <w:settings ... >
* <w:documentProtection w:edit="[passed editValue]" w:enforcement="1"/>
* </pre>
*/
public void setEnforcementEditValue(org.openxmlformats.schemas.wordprocessingml.x2006.main.STDocProtect.Enum editValue) {
safeGetDocumentProtection().setEnforcement(STOnOff.X_1);
safeGetDocumentProtection().setEdit(editValue);
}
/**
* Removes protection enforcement.<br/>
* In the documentProtection tag inside settings.xml file <br/>
* it sets the value of enforcement to "0" (w:enforcement="0") <br/>
*/
public void removeEnforcement() {
safeGetDocumentProtection().setEnforcement(STOnOff.X_0);
}
@Override
protected void commit() throws IOException {
XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS);
xmlOptions.setSaveSyntheticDocumentElement(new QName(CTSettings.type.getName().getNamespaceURI(), "settings"));
Map<String, String> map = new HashMap<String, String>();
map.put("http://schemas.openxmlformats.org/wordprocessingml/2006/main", "w");
xmlOptions.setSaveSuggestedPrefixes(map);
PackagePart part = getPackagePart();
OutputStream out = part.getOutputStream();
ctSettings.save(out, xmlOptions);
out.close();
}
private CTDocProtect safeGetDocumentProtection() {
CTDocProtect documentProtection = ctSettings.getDocumentProtection();
if (documentProtection == null) {
documentProtection = CTDocProtect.Factory.newInstance();
ctSettings.setDocumentProtection(documentProtection);
}
return ctSettings.getDocumentProtection();
}
private void readFrom(InputStream inputStream) {
try {
ctSettings = SettingsDocument.Factory.parse(inputStream).getSettings();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
|