Changeset 1640 for trunk/docx4j/src


Ignore:
Timestamp:
08/12/11 03:41:15 (10 months ago)
Author:
jharrop
Message:

Remove main method, which enabled old convention documents to be converted to suit the current convention.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/docx4j/src/main/java/org/docx4j/model/datastorage/OpenDoPEHandler.java

    r1639 r1640  
    12491249                        return part.getData().xpathGetNodes(xpath, prefixMappings); 
    12501250                } 
    1251                  
    1252          
    1253  
    1254         public static void main(String[] args) throws Exception { 
    1255                  
    1256 //              String inputfilepath = System.getProperty("user.dir") + "/sample-docs/databinding/invoice_old.docx"; 
    1257 //              String save_converted = System.getProperty("user.dir") + "/sample-docs/databinding/invoice.docx"; 
    1258  
    1259                 String inputfilepath = System.getProperty("user.dir") + "/sample-docs/databinding/CountryRegions_old.xml"; 
    1260                 String save_converted = System.getProperty("user.dir") + "/sample-docs/databinding/CountryRegions.xml"; 
    1261  
    1262                  
    1263                  
    1264                 WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(inputfilepath));           
    1265                  
    1266                 // Create an XPathsPart, and a props part 
    1267                 XPathsPart xPathsPart = new XPathsPart(new PartName("/customXml/xpaths.xml")); 
    1268                 wordMLPackage.getMainDocumentPart().addTargetPart(xPathsPart); 
    1269                  
    1270                 xpathsFactory = new org.opendope.xpaths.ObjectFactory(); 
    1271                 org.opendope.xpaths.Xpaths xpaths = xpathsFactory.createXpaths(); 
    1272                 xPathsPart.setJaxbElement(xpaths); 
    1273                  
    1274                 CustomXmlDataStoragePropertiesPart xPathsPropsPart = new CustomXmlDataStoragePropertiesPart( 
    1275                                 new PartName("/customXml/xpathsProps.xml"));  
    1276                  
    1277                 /* <ds:datastoreItem ds:itemID="{D08E37B0-BBEF-496D-874B-C21E2168A259}" xmlns:ds="http://schemas.openxmlformats.org/officeDocument/2006/customXml"> 
    1278                  *      <ds:schemaRefs> 
    1279                  *              <ds:schemaRef ds:uri="http://www.w3.org/2001/XMLSchema"/> 
    1280                  *              <ds:schemaRef ds:uri="http://opendope.org/xpaths"/>< 
    1281                         /ds:schemaRefs> 
    1282                          * </ds:datastoreItem> */ 
    1283                  
    1284                 org.docx4j.customXmlProperties.ObjectFactory dsf = new org.docx4j.customXmlProperties.ObjectFactory(); 
    1285                 org.docx4j.customXmlProperties.DatastoreItem dsi = dsf.createDatastoreItem(); 
    1286                 dsi.setItemID("{" + UUID.randomUUID().toString().toUpperCase() + "}"); 
    1287                 SchemaRefs schemaRefs = dsf.createSchemaRefs(); 
    1288                 dsi.setSchemaRefs(schemaRefs); 
    1289                 SchemaRef schemaRef = dsf.createSchemaRefsSchemaRef(); 
    1290                 schemaRef.setUri("http://www.w3.org/2001/XMLSchema"); 
    1291                 schemaRefs.getSchemaRef().add(schemaRef); 
    1292                 schemaRef = dsf.createSchemaRefsSchemaRef(); 
    1293                 schemaRef.setUri("http://opendope.org/xpaths"); 
    1294                 schemaRefs.getSchemaRef().add(schemaRef); 
    1295                  
    1296                 xPathsPropsPart.setJaxbElement(dsi); 
    1297                 xPathsPart.addTargetPart(xPathsPropsPart); 
    1298                  
    1299                 // Create a ConditionsPart 
    1300                 ConditionsPart conditionsPart = new ConditionsPart(new PartName("/customXml/conditions.xml"));           
    1301                 wordMLPackage.getMainDocumentPart().addTargetPart(conditionsPart); 
    1302                  
    1303                 conditionsFactory = new org.opendope.conditions.ObjectFactory(); 
    1304                 org.opendope.conditions.Conditions conditions = conditionsFactory.createConditions(); 
    1305                 conditionsPart.setJaxbElement(conditions); 
    1306                  
    1307                 CustomXmlDataStoragePropertiesPart conditionsPropsPart = new CustomXmlDataStoragePropertiesPart( 
    1308                                 new PartName("/customXml/conditionsProps.xml"));  
    1309                  
    1310                 /* 
    1311                 <ds:datastoreItem ds:itemID="{E8637D7C-41A8-4079-A250-062638BD1ADF}" xmlns:ds="http://schemas.openxmlformats.org/officeDocument/2006/c 
    1312                         ustomXml"><ds:schemaRefs><ds:schemaRef ds:uri="http://www.w3.org/2001/XMLSchema"/> 
    1313                         <ds:schemaRef ds:uri="http://opendope.org/conditions 
    1314                         "/></ds:schemaRefs></ds:datastoreItem> 
    1315 */ 
    1316  
    1317                 dsi = dsf.createDatastoreItem(); 
    1318                 dsi.setItemID("{" + UUID.randomUUID().toString().toUpperCase() + "}"); 
    1319                 schemaRefs = dsf.createSchemaRefs(); 
    1320                 dsi.setSchemaRefs(schemaRefs); 
    1321                 schemaRef = dsf.createSchemaRefsSchemaRef(); 
    1322                 schemaRef.setUri("http://www.w3.org/2001/XMLSchema"); 
    1323                 schemaRefs.getSchemaRef().add(schemaRef); 
    1324                 schemaRef = dsf.createSchemaRefsSchemaRef(); 
    1325                 schemaRef.setUri("http://opendope.org/conditions"); 
    1326                 schemaRefs.getSchemaRef().add(schemaRef); 
    1327                  
    1328                 conditionsPropsPart.setJaxbElement(dsi); 
    1329                 conditionsPart.addTargetPart(conditionsPropsPart); 
    1330                  
    1331                 // Go through all the sdt's 
    1332                 MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();             
    1333                 String xpathSdt = "//w:sdt"; 
    1334                 List<Object> list = null; 
    1335                 try { 
    1336                         list = documentPart.getJAXBNodesViaXPath(xpathSdt, false); 
    1337                 } catch (JAXBException e) { 
    1338                         // TODO Auto-generated catch block 
    1339                         e.printStackTrace(); 
    1340                 } 
    1341                  
    1342                 int i = 0; 
    1343                 for(Object raw : list) { 
    1344                         i++; 
    1345                          
    1346                         log.info(raw.getClass().getName() ); 
    1347                          
    1348                         Object o = XmlUtils.unwrap(raw); 
    1349  
    1350                         Method m = o.getClass().getDeclaredMethod("getSdtPr"); 
    1351                          
    1352                         SdtPr sdtPr = (SdtPr)m.invoke(o); 
    1353                          
    1354                         Tag tag = sdtPr.getTag(); 
    1355                          
    1356                         if (sdtPr.getDataBinding()!=null) { 
    1357  
    1358                                 // Just want to add this xpath to xpaths part. 
    1359                                 xpaths.getXpath().add( 
    1360                                                  
    1361                                                 tmpCreateXPath("x" + i,  
    1362                                                                 sdtPr.getDataBinding().getStoreItemID(), 
    1363                                                                 sdtPr.getDataBinding().getXpath(), 
    1364                                                                 sdtPr.getDataBinding().getPrefixMappings() ) 
    1365  
    1366                                 ); 
    1367                                  
    1368                                 // Write tag 
    1369                                 if (tag==null) { 
    1370                                         tag = Context.getWmlObjectFactory().createTag(); 
    1371                                         sdtPr.setTag(tag); 
    1372                                 } 
    1373                                 HashMap<String, String> map; 
    1374                                 if (tag.getVal()!=null) { 
    1375                                         map = QueryString.parseQueryString(tag.getVal(), true); 
    1376                                 } else { 
    1377                                         map = new HashMap<String, String>(); 
    1378                                 } 
    1379                                  
    1380                                 map.remove("bindingrole"); 
    1381                                 map.put(BINDING_ROLE_XPATH, "x"+i);                                      
    1382                                 tag.setVal(QueryString.create(map));                                     
    1383                                  
    1384                         } else { 
    1385                                                                          
    1386                                 if (tag==null) continue; 
    1387                                 log.info(tag.getVal()); 
    1388                                  
    1389                                 HashMap<String, String> map = QueryString.parseQueryString(tag.getVal(), true); 
    1390                                  
    1391                                 String bindingrole = map.get("bindingrole"); 
    1392                                 if (bindingrole==null) continue;  // do nothing 
    1393  
    1394                                 // get the value 
    1395                                 String storeItemId = map.get("w:storeItemID").toLowerCase(); 
    1396                                 String xpath = map.get("w:xpath"); 
    1397                                 String prefixMappings = map.get("w:prefixMappings"); 
    1398  
    1399                                 map.remove("w:storeItemID"); 
    1400                                 map.remove("w:xpath"); 
    1401                                 map.remove("w:prefixMappings"); 
    1402                                  
    1403                                 if (bindingrole.equals("conditional")) { 
    1404  
    1405                                         log.info("Processing Conditional: " + tag.getVal()); 
    1406                                          
    1407                                         // Create xpath 
    1408                                         xpaths.getXpath().add( 
    1409                                                          
    1410                                                         tmpCreateXPath("x" + i,  
    1411                                                                         storeItemId, 
    1412                                                                         xpath, 
    1413                                                                         prefixMappings ) 
    1414                                         ); 
    1415                                          
    1416                                         // Create condition 
    1417                                         Condition c = conditionsFactory.createCondition(); 
    1418                                         c.setId("c"+i); 
    1419                                         conditions.getCondition().add(c); 
    1420                                         org.opendope.conditions.Xpathref cxp = conditionsFactory.createXpathref(); 
    1421                                         c.setXpathref(cxp); 
    1422                                         cxp.setId("x"+i); 
    1423                                          
    1424                                         // Write tag 
    1425                                         map.remove("bindingrole"); 
    1426                                         map.put(BINDING_ROLE_CONDITIONAL, "c"+i);                                        
    1427                                         tag.setVal(QueryString.create(map));                                     
    1428                                          
    1429                                 } else if (bindingrole.equals("repeat")) { 
    1430  
    1431                                         log.info("Processing Repeat: " + tag.getVal()); 
    1432                                                  
    1433                                         // Create xpath 
    1434                                         xpaths.getXpath().add( 
    1435                                                          
    1436                                                         tmpCreateXPath("x" + i,  
    1437                                                                         storeItemId, 
    1438                                                                         xpath, 
    1439                                                                         prefixMappings ) 
    1440                                         ); 
    1441  
    1442                                         // Write tag 
    1443                                         map.remove("bindingrole"); 
    1444                                         map.put(BINDING_ROLE_REPEAT, "x"+i);                                     
    1445                                         tag.setVal(QueryString.create(map));     
    1446                                 } 
    1447                                          
    1448                         }                        
    1449                 } 
    1450                  
    1451  
    1452                 SaveToZipFile saver = new SaveToZipFile(wordMLPackage); 
    1453                 saver.save(save_converted); 
    1454                 System.out.println("Saved: " + save_converted); 
    1455                                  
    1456         } 
    1457          
    1458                  
    1459         private static org.opendope.xpaths.Xpaths.Xpath tmpCreateXPath(String id, String storeItemId, String xpx, String prefixMappings) { 
    1460                  
    1461                 org.opendope.xpaths.Xpaths.Xpath xpath = xpathsFactory.createXpathsXpath(); 
    1462                  
    1463                 xpath.setId(id); 
    1464                 org.opendope.xpaths.Xpaths.Xpath.DataBinding db = xpathsFactory.createXpathsXpathDataBinding(); 
    1465                 xpath.setDataBinding(db); 
    1466                 db.setPrefixMappings( prefixMappings); 
    1467                 db.setStoreItemID( storeItemId.toUpperCase() );  // Word writes these in UPPER CASE 
    1468                 db.setXpath( xpx );                              
    1469                  
    1470                 return xpath; 
    1471         } 
    1472  
    1473         private static org.opendope.xpaths.ObjectFactory xpathsFactory; 
    1474                  
    1475         private static org.opendope.conditions.ObjectFactory conditionsFactory; 
    1476          
    14771251         
    14781252} 
Note: See TracChangeset for help on using the changeset viewer.