Changeset 525

Show
Ignore:
Timestamp:
08/09/08 22:38:43 (3 months ago)
Author:
jharrop
Message:

Example traversal of table

Location:
trunk/docx4j
Files:
1 added
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/docx4j/src/main/java/org/docx4j/samples/OpenMainDocumentAndTraverse.java

    r520 r525  
    4646 
    4747                //String inputfilepath = "/home/dev/workspace/docx4j/sample-docs/jpeg.docx"; 
    48                 String inputfilepath = System.getProperty("user.dir") + "/sample-docs/fonts-modesOfApplication.docx"; 
     48                String inputfilepath = System.getProperty("user.dir") + "/sample-docs/Table.docx"; 
    4949                 
    5050                boolean save = false; 
    51                 String outputfilepath = "/home/dev/tmp/test-out.docx";           
     51                String outputfilepath = System.getProperty("user.dir") + "/test-out.docx";               
    5252                 
    5353                 
     
    6868                List <Object> bodyChildren = body.getEGBlockLevelElts(); 
    6969                 
    70                 walkJAXBElements(bodyChildren); 
    71                          
    72 //              // Change something 
    73 //              org.docx4j.wml.P p = (org.docx4j.wml.P)((JAXBElement)bodyChildren.get(2)).getValue(); 
    74                 org.docx4j.wml.P p = (org.docx4j.wml.P)bodyChildren.get(2); 
    75                  
    76                 //walkList(p.getParagraphContent()); 
    77                  
    78                 org.docx4j.wml.PPr pPr = p.getPPr(); 
    79                  
    80                 if (pPr!=null && pPr.getPStyle()!=null) { 
    81                         System.out.println( "Style: " + pPr.getPStyle().getVal() ); 
    82                 }                
    83                  
    84                 org.docx4j.wml.ObjectFactory factory = new org.docx4j.wml.ObjectFactory(); 
    85                 org.docx4j.wml.R  run = factory.createR(); 
    86                 org.docx4j.wml.Text  t = factory.createText(); 
    87                                  
    88                  
    89                 t.setValue("SOMETHING NEW, with added JAXB convenience!"); 
    90                  
    91                 run.getRunContent().add(t);              
    92                  
    93                 org.docx4j.wml.RPr  runProps = factory.createRPr(); 
    94                  
    95                 run.setRPr( runProps);  
    96                  
    97                 org.docx4j.wml.BooleanDefaultTrue val = factory.createBooleanDefaultTrue(); 
    98                 val.setVal(Boolean.valueOf(true)); 
    99                 runProps.setB( val ); 
    100                  
    101                 // or relying on the default value, could just do: 
    102                 // runProps.setB( factory.createBooleanDefaultTrue() ); 
    103                  
    104                 p.getParagraphContent().add(run); 
    105                  
    106 //              System.out.println( "/n/n What does that look like? /n/n"); 
    107 //              walkList(p.getParagraphContent()); 
     70                walkJAXBElements(bodyChildren);                  
    10871                                 
    10972                // Save it 
     
    12184                        if ( o instanceof javax.xml.bind.JAXBElement) { 
    12285                         
    123                                         System.out.println( o.getClass().getName() ); 
    124                                         System.out.println( ((JAXBElement)o).getName() ); 
    125                                         System.out.println( ((JAXBElement)o).getDeclaredType().getName() + "\n\n"); 
     86                                System.out.println( o.getClass().getName() ); 
     87                                System.out.println( ((JAXBElement)o).getName() ); 
     88                                System.out.println( ((JAXBElement)o).getDeclaredType().getName() + "\n\n"); 
    12689                                         
     90                                if ( ((JAXBElement)o).getDeclaredType().getName().equals("org.docx4j.wml.Tbl") ) { 
     91                                        org.docx4j.wml.Tbl tbl = (org.docx4j.wml.Tbl)((JAXBElement)o).getValue(); 
     92                                        describeTable(tbl); 
     93                                } 
    12794                        } else if (o instanceof org.docx4j.wml.P) { 
    12895                                System.out.println( "Paragraph object: "); 
     
    185152                } 
    186153        } 
     154 
     155        static void describeTable( org.docx4j.wml.Tbl tbl ) { 
     156                 
     157                // What does a table look like? 
     158                boolean suppressDeclaration = false; 
     159                boolean prettyprint = true; 
     160                System.out.println( org.docx4j.XmlUtils.marshaltoString(tbl, suppressDeclaration, prettyprint) ); 
     161                 
     162                // Could get the TblPr if we wanted them 
     163                 org.docx4j.wml.TblPr tblPr = tbl.getTblPr(); 
     164                  
     165                 // Could get the TblGrid if we wanted it 
     166                 org.docx4j.wml.TblGrid tblGrid = tbl.getTblGrid(); 
     167                  
     168                 // But here, let's look at the table contents 
     169                 for (Object o : tbl.getEGContentRowContent() ) { 
     170                          
     171                         if (o instanceof org.docx4j.wml.Tr) { 
     172                                  
     173                                 org.docx4j.wml.Tr tr = (org.docx4j.wml.Tr)o; 
     174                                  
     175                                 for (Object o2 : tr.getEGContentCellContent() ) { 
     176                                          
     177                                                System.out.println("  " + o2.getClass().getName() ); 
     178                                                if ( o2 instanceof javax.xml.bind.JAXBElement) { 
     179                                                         
     180                                                        if ( ((JAXBElement)o2).getDeclaredType().getName().equals("org.docx4j.wml.Tc") ) { 
     181                                                                org.docx4j.wml.Tc tc = (org.docx4j.wml.Tc)((JAXBElement)o2).getValue(); 
     182                                                                 
     183                                                                // Look at the paragraphs in the tc 
     184                                                                walkJAXBElements( tc.getEGBlockLevelElts() ); 
     185                                                                 
     186                                                        } else { 
     187                                                                // What is it, if it isn't a Tc? 
     188                                                                System.out.println("      " +  ((JAXBElement)o).getName() ); 
     189                                                                System.out.println("      " +  ((JAXBElement)o).getDeclaredType().getName()); 
     190                                                        } 
     191                                                } else { 
     192                                                        System.out.println("  " + o.getClass().getName() );                                                      
     193                                                } 
     194                                          
     195                                 } 
     196                                  
     197                                  
     198                         } else { 
     199                                System.out.println("  " + o.getClass().getName() ); 
     200                         } 
     201                          
     202                 } 
     203                  
     204                  
     205                 
     206        } 
    187207         
    188208        static void describeDrawing( org.docx4j.wml.Drawing d ) {