Lines 49-60
Link Here
|
49 |
import org.apache.poi.util.IOUtils; |
49 |
import org.apache.poi.util.IOUtils; |
50 |
import org.apache.poi.util.IdentifierManager; |
50 |
import org.apache.poi.util.IdentifierManager; |
51 |
import org.apache.poi.util.Internal; |
51 |
import org.apache.poi.util.Internal; |
|
|
52 |
import org.apache.poi.util.POILogFactory; |
53 |
import org.apache.poi.util.POILogger; |
52 |
import org.apache.poi.util.PackageHelper; |
54 |
import org.apache.poi.util.PackageHelper; |
53 |
import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy; |
55 |
import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy; |
54 |
import org.apache.xmlbeans.XmlCursor; |
56 |
import org.apache.xmlbeans.XmlCursor; |
55 |
import org.apache.xmlbeans.XmlException; |
57 |
import org.apache.xmlbeans.XmlException; |
56 |
import org.apache.xmlbeans.XmlObject; |
58 |
import org.apache.xmlbeans.XmlObject; |
57 |
import org.apache.xmlbeans.XmlOptions; |
59 |
import org.apache.xmlbeans.XmlOptions; |
|
|
60 |
import org.openxmlformats.schemas.officeDocument.x2006.customProperties.CTProperty; |
58 |
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTComment; |
61 |
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTComment; |
59 |
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDocument1; |
62 |
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDocument1; |
60 |
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFtnEdn; |
63 |
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFtnEdn; |
Lines 85-90
Link Here
|
85 |
*/ |
88 |
*/ |
86 |
public class XWPFDocument extends POIXMLDocument implements Document, IBody { |
89 |
public class XWPFDocument extends POIXMLDocument implements Document, IBody { |
87 |
|
90 |
|
|
|
91 |
/** For our own logging use */ |
92 |
private final static POILogger LOGGER = POILogFactory.getLogger(XWPFDocument.class); |
93 |
private final static String EMPTYSPACE = " "; |
94 |
|
88 |
private CTDocument1 ctDocument; |
95 |
private CTDocument1 ctDocument; |
89 |
private XWPFSettings settings; |
96 |
private XWPFSettings settings; |
90 |
/** |
97 |
/** |
Lines 1307-1312
Link Here
|
1307 |
return tableRow.getTableCell(cell); |
1314 |
return tableRow.getTableCell(cell); |
1308 |
} |
1315 |
} |
1309 |
|
1316 |
|
|
|
1317 |
|
1318 |
|
1319 |
/** |
1320 |
* Propagate the document custom properties. If the document (Word) |
1321 |
* has custom properties defined, those will be matched across the text |
1322 |
* and if there are differences those will be updated. |
1323 |
* |
1324 |
* @exception IOException if anything can't be written. |
1325 |
*/ |
1326 |
public void propagateXwpfCustomProperties() { |
1327 |
|
1328 |
LOGGER.log(POILogger.INFO,"--> method propagateDocxProperties started!"); |
1329 |
|
1330 |
|
1331 |
POIXMLProperties summaryInfo = null; |
1332 |
|
1333 |
Boolean go4NextWt = false; |
1334 |
|
1335 |
Boolean processed = false; |
1336 |
|
1337 |
Boolean catchNextWt = false; |
1338 |
|
1339 |
|
1340 |
ArrayList<String> toBeprocessedItems = new ArrayList<String>(); |
1341 |
HashMap<String, String> hashmp = new HashMap<String, String>(); |
1342 |
Integer i = 0, j = 0; |
1343 |
Integer selectedItem = 0; |
1344 |
|
1345 |
summaryInfo = this.getProperties(); |
1346 |
|
1347 |
List<CTProperty> propList = summaryInfo.getCustomProperties() |
1348 |
.getUnderlyingProperties().getPropertyList(); |
1349 |
|
1350 |
Iterator<CTProperty> propsIterator = propList.iterator(); |
1351 |
|
1352 |
while (propsIterator.hasNext()) { |
1353 |
CTProperty prop = propsIterator.next(); |
1354 |
LOGGER.log(POILogger.INFO, "--> Grouping property name {0} with value= {1}", new Object[]{prop.getName(), prop.getLpwstr()}); |
1355 |
toBeprocessedItems.add(prop.getName()); |
1356 |
hashmp.put(prop.getName(), prop.getLpwstr()); |
1357 |
} |
1358 |
|
1359 |
List<XWPFParagraph> paragraphList = this.getParagraphs(); |
1360 |
Iterator<XWPFParagraph> paragraphIterator = paragraphList.iterator(); |
1361 |
|
1362 |
// Iterate over each paragraph: |
1363 |
|
1364 |
processed = false; |
1365 |
paragraphIterator = paragraphList.iterator(); |
1366 |
|
1367 |
while (paragraphIterator.hasNext() && !processed) { |
1368 |
|
1369 |
XWPFParagraph element = paragraphIterator.next(); |
1370 |
|
1371 |
LOGGER.log(POILogger.INFO, "--> Processing paragraph: {0}", element.getParagraphText()); |
1372 |
CTP ctp = element.getCTP(); |
1373 |
|
1374 |
XmlObject o; |
1375 |
|
1376 |
XmlCursor c = ctp.newCursor(); |
1377 |
|
1378 |
for (c.toFirstContentToken(); c.hasNextToken(); c.toNextToken()) { |
1379 |
|
1380 |
o = c.getObject(); |
1381 |
|
1382 |
/* |
1383 |
* Here we look for Customized properties that are |
1384 |
* defined/declared in the XML Text areas. E.g. <w:instrText |
1385 |
* xml:space="preserve">DOCPROPERTY dummyProperty \* |
1386 |
* MERGEFORMAT</w:instrText> |
1387 |
*/ |
1388 |
if (c.isText() && c.getTextValue().contains("DOCPROPERTY")) { |
1389 |
|
1390 |
for (i = 0; i < toBeprocessedItems.size(); i++) { |
1391 |
if (c.getTextValue() != null |
1392 |
&& c.getTextValue().contains( |
1393 |
EMPTYSPACE + toBeprocessedItems.get(i) |
1394 |
+ EMPTYSPACE)) { |
1395 |
|
1396 |
selectedItem = i; |
1397 |
|
1398 |
LOGGER.log(POILogger.INFO,"-->FOUND TEXT PROPERTY DEFINITION VALUE --> " |
1399 |
+ " with i = {0} and text : {1}", new Object[]{i, c.getTextValue()}); |
1400 |
} |
1401 |
} |
1402 |
|
1403 |
String tempText = c.getTextValue(); |
1404 |
int len1 = tempText.length(); |
1405 |
int len2 = tempText.lastIndexOf("MERGEFORMAT") |
1406 |
+ "MERGEFORMAT".length() + 1; |
1407 |
if (len1 == len2) { |
1408 |
catchNextWt = true; |
1409 |
tempText = tempText.substring(0, len2); |
1410 |
} else { |
1411 |
catchNextWt = false; |
1412 |
} |
1413 |
|
1414 |
} |
1415 |
if (o != null) { |
1416 |
String tagName1 = o.getDomNode().getNodeName(); |
1417 |
|
1418 |
if (catchNextWt) { |
1419 |
if ("w:t".equals(tagName1)) { |
1420 |
catchNextWt = false; |
1421 |
c.getTextValue(); |
1422 |
c.setTextValue(hashmp.get(toBeprocessedItems |
1423 |
.get(selectedItem))); |
1424 |
|
1425 |
LOGGER.log(POILogger.INFO, "--> CHANGED value--> {0}", c.getTextValue()); |
1426 |
} |
1427 |
} |
1428 |
|
1429 |
} |
1430 |
|
1431 |
o = c.getObject(); |
1432 |
|
1433 |
/* |
1434 |
* Here we look for Customized properties that are |
1435 |
* defined/declared in the XML Tag Value areas. E.g. |
1436 |
* <w:fldSimple w:instr=" DOCPROPERTY dummyProperty \* |
1437 |
* MERGEFORMAT "> |
1438 |
*/ |
1439 |
if (o != null && o.getDomNode() != null) { |
1440 |
|
1441 |
String tagName = o.getDomNode().getNodeName(); |
1442 |
String tagValue = o.getDomNode().getNodeValue(); |
1443 |
|
1444 |
for (j = 0; j < toBeprocessedItems.size(); j++) { |
1445 |
if (tagValue != null |
1446 |
&& tagValue.contains("DOCPROPERTY") |
1447 |
&& tagValue.contains(EMPTYSPACE |
1448 |
+ toBeprocessedItems.get(j) |
1449 |
+ EMPTYSPACE)) { |
1450 |
go4NextWt = true; |
1451 |
selectedItem = j; |
1452 |
|
1453 |
LOGGER.log(POILogger.INFO,"--> FOUND TAG PROPERTY DEFINITION VALUE --> " + " {0}", tagValue); |
1454 |
|
1455 |
} |
1456 |
} |
1457 |
|
1458 |
if (tagName.contains("w:t") |
1459 |
&& go4NextWt.booleanValue() == true) { |
1460 |
|
1461 |
c.setTextValue(hashmp.get(toBeprocessedItems |
1462 |
.get(selectedItem))); |
1463 |
LOGGER.log(POILogger.INFO, "--> CHANGED value--> {0}", c.getTextValue()); |
1464 |
|
1465 |
go4NextWt = false; |
1466 |
} |
1467 |
|
1468 |
processed = true; |
1469 |
} |
1470 |
} |
1471 |
|
1472 |
} |
1473 |
|
1474 |
} |
1475 |
|
1310 |
public XWPFDocument getXWPFDocument() { |
1476 |
public XWPFDocument getXWPFDocument() { |
1311 |
return this; |
1477 |
return this; |
1312 |
} |
1478 |
} |
1313 |
+ true |
1479 |
+ true |