Java Code Examples for org.docx4j.XmlUtils#unwrap()

The following examples show how to use org.docx4j.XmlUtils#unwrap() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WmlElementUtils.java    From docx4j-template with Apache License 2.0 6 votes vote down vote up
public static <T> List<T> getChildrenElements(Object source, Class<T> targetClass) {  
    List<T> result = new ArrayList<T>();  
    //获取真实的对象
	Object target = XmlUtils.unwrap(source);
	//if (target.getClass().equals(targetClass)) {
    if (targetClass.isAssignableFrom(target.getClass())) { 
        result.add((T)target);
    } else if (target instanceof ContentAccessor) {  
        List<?> children = ((ContentAccessor) target).getContent();  
        //if (children.getClass().equals(targetClass)) {
        if (targetClass.isAssignableFrom(children.getClass())) { 
            result.add((T)children);
        }
    }
    return result;  
}
 
Example 2
Source File: DocumentWalker.java    From docx-stamper with MIT License 6 votes vote down vote up
public void walk() {
    for (Object contentElement : contentAccessor.getContent()) {
        Object unwrappedObject = XmlUtils.unwrap(contentElement);
        if (unwrappedObject instanceof P) {
            P p = (P) unwrappedObject;
            walkParagraph(p);
        } else if (unwrappedObject instanceof Tbl) {
            Tbl table = (Tbl) unwrappedObject;
            walkTable(table);
        } else if (unwrappedObject instanceof Tr) {
            Tr row = (Tr) unwrappedObject;
            walkTableRow(row);
        } else if (unwrappedObject instanceof Tc) {
            Tc cell = (Tc) unwrappedObject;
            walkTableCell(cell);
        } else if (unwrappedObject instanceof CommentRangeStart) {
            CommentRangeStart commentRangeStart = (CommentRangeStart) unwrappedObject;
            onCommentRangeStart(commentRangeStart);
        } else if (unwrappedObject instanceof CommentRangeEnd) {
            CommentRangeEnd commentRangeEnd = (CommentRangeEnd) unwrappedObject;
            onCommentRangeEnd(commentRangeEnd);
        }
    }
}
 
Example 3
Source File: WmlElementUtils.java    From docx4j-template with Apache License 2.0 6 votes vote down vote up
/**
 * @Description:删除指定位置的表格,删除后表格数量减一
 */
public static boolean removeTableByIndex(WordprocessingMLPackage wordMLPackage, int index) throws Exception {
    boolean flag = false;
    if (index < 0) {
        return flag;
    }
    List<Object> objList = wordMLPackage.getMainDocumentPart().getContent();
    if (objList == null) {
        return flag;
    }
    int k = -1;
    for (int i = 0, len = objList.size(); i < len; i++) {
        Object obj = XmlUtils.unwrap(objList.get(i));
        if (obj instanceof Tbl) {
            k++;
            if (k == index) {
                wordMLPackage.getMainDocumentPart().getContent().remove(i);
                flag = true;
                break;
            }
        }
    }
    return flag;
}
 
Example 4
Source File: WmlElementUtils.java    From docx4j-template with Apache License 2.0 6 votes vote down vote up
/**
 * @Description: 删除指定行 删除后行数减一
 */
public static boolean removeTrByIndex(Tbl tbl, int index) {
    boolean flag = false;
    if (index < 0) {
        return flag;
    }
    List<Object> objList = tbl.getContent();
    if (objList == null) {
        return flag;
    }
    int k = -1;
    for (int i = 0, len = objList.size(); i < len; i++) {
        Object obj = XmlUtils.unwrap(objList.get(i));
        if (obj instanceof Tr) {
            k++;
            if (k == index) {
                tbl.getContent().remove(i);
                flag = true;
                break;
            }
        }
    }
    return flag;
}
 
Example 5
Source File: Docx4j_工具类_S3_Test.java    From docx4j-template with Apache License 2.0 6 votes vote down vote up
/**
 * @Description: 删除指定行 删除后行数减一
 */
public boolean removeTrByIndex(Tbl tbl, int index) {
    boolean flag = false;
    if (index < 0) {
        return flag;
    }
    List<Object> objList = tbl.getContent();
    if (objList == null) {
        return flag;
    }
    int k = -1;
    for (int i = 0, len = objList.size(); i < len; i++) {
        Object obj = XmlUtils.unwrap(objList.get(i));
        if (obj instanceof Tr) {
            k++;
            if (k == index) {
                tbl.getContent().remove(i);
                flag = true;
                break;
            }
        }
    }
    return flag;
}
 
Example 6
Source File: Docx4j_读取内容控件_S4_Test.java    From docx4j-template with Apache License 2.0 6 votes vote down vote up
public void walkJAXBElements(Object parent) {  
    List children = getChildren(parent);  
    if (children != null) {  
        for (Object o : children) {  
            if (o instanceof javax.xml.bind.JAXBElement  
                    && (((JAXBElement) o).getName().getLocalPart()  
                            .equals("sdt"))) {  
                ((Child) ((JAXBElement) o).getValue()).setParent(XmlUtils  
                        .unwrap(parent));  
            } else {  
                o = XmlUtils.unwrap(o);  
                if (o instanceof Child) {  
                    ((Child) o).setParent(XmlUtils.unwrap(parent));  
                }  
            }  
            this.apply(o);  
            if (this.shouldTraverse(o)) {  
                walkJAXBElements(o);  
            }  
        }  
    }  
}
 
Example 7
Source File: Docx4j_读取内容控件_S3_Test.java    From docx4j-template with Apache License 2.0 6 votes vote down vote up
public void walkJAXBElements(Object parent) {  
    List children = getChildren(parent);  
    if (children != null) {  
        for (Object o : children) {  
            if (o instanceof javax.xml.bind.JAXBElement  
                    && (((JAXBElement) o).getName().getLocalPart()  
                            .equals("sdt"))) {  
                ((Child) ((JAXBElement) o).getValue()).setParent(XmlUtils  
                        .unwrap(parent));  
            } else {  
                o = XmlUtils.unwrap(o);  
                if (o instanceof Child) {  
                    ((Child) o).setParent(XmlUtils.unwrap(parent));  
                }  
            }  
            this.apply(o);  
            if (this.shouldTraverse(o)) {  
                walkJAXBElements(o);  
            }  
        }  
    }  
}
 
Example 8
Source File: CommentUtil.java    From docx-stamper with MIT License 6 votes vote down vote up
private static boolean deleteCommentReference(ContentAccessor parent,
		BigInteger commentId) {
	for (int i = 0; i < parent.getContent().size(); i++) {
		Object contentObject = XmlUtils.unwrap(parent.getContent().get(i));
		if (contentObject instanceof ContentAccessor) {
			if (deleteCommentReference((ContentAccessor) contentObject, commentId)) {
				return true;
			}
		}
		if (contentObject instanceof R) {
			for (Object runContentObject : ((R) contentObject).getContent()) {
				Object unwrapped = XmlUtils.unwrap(runContentObject);
				if (unwrapped instanceof R.CommentReference) {
					BigInteger foundCommentId = ((R.CommentReference) unwrapped)
							.getId();
					if (foundCommentId.equals(commentId)) {
						parent.getContent().remove(i);
						return true;
					}
				}
			}
		}
	}
	return false;
}
 
Example 9
Source File: DocumentWalker.java    From docx-stamper with MIT License 5 votes vote down vote up
private void walkTableCell(Tc cell) {
    onTableCell(cell);
    for (Object cellContentElement : cell.getContent()) {
        if (XmlUtils.unwrap(cellContentElement) instanceof P) {
            P p = (P) cellContentElement;
            walkParagraph(p);
        } else if (XmlUtils.unwrap(cellContentElement) instanceof Tbl) {
            Tbl nestedTable = (Tbl) ((JAXBElement) cellContentElement).getValue();
            walkTable(nestedTable);
        }
    }
}
 
Example 10
Source File: CoordinatesWalker.java    From docx-stamper with MIT License 5 votes vote down vote up
private void walkTableRow(TableRowCoordinates rowCoordinates) {
    onTableRow(rowCoordinates);
    int cellIndex = 0;
    for (Object rowContentElement : rowCoordinates.getRow().getContent()) {
        if (XmlUtils.unwrap(rowContentElement) instanceof Tc) {
            Tc cell = rowContentElement instanceof Tc ? (Tc) rowContentElement : (Tc) ((JAXBElement) rowContentElement).getValue();
            TableCellCoordinates cellCoordinates = new TableCellCoordinates(cell, cellIndex, rowCoordinates);
            walkTableCell(cellCoordinates);
        }
        cellIndex++;
    }
}
 
Example 11
Source File: CoordinatesWalker.java    From docx-stamper with MIT License 5 votes vote down vote up
private void walkTable(TableCoordinates tableCoordinates) {
    onTable(tableCoordinates);
    int rowIndex = 0;
    for (Object contentElement : tableCoordinates.getTable().getContent()) {
        if (XmlUtils.unwrap(contentElement) instanceof Tr) {
            Tr row = (Tr) contentElement;
            TableRowCoordinates rowCoordinates = new TableRowCoordinates(row, rowIndex, tableCoordinates);
            walkTableRow(rowCoordinates);
        }
        rowIndex++;
    }
}
 
Example 12
Source File: CoordinatesWalker.java    From docx-stamper with MIT License 5 votes vote down vote up
private void walkParagraph(ParagraphCoordinates paragraphCoordinates){
	int rowIndex = 0;
	for (Object contentElement : paragraphCoordinates.getParagraph().getContent()){
		 if (XmlUtils.unwrap(contentElement) instanceof R) {
			 R run = (R) contentElement;
			 RunCoordinates runCoordinates = new RunCoordinates(run, rowIndex);
			 onRun(runCoordinates, paragraphCoordinates);
		 }
	}
	// we run the paragraph afterwards so that the comments inside work before the whole paragraph comments
	onParagraph(paragraphCoordinates);

}
 
Example 13
Source File: CoordinatesWalker.java    From docx-stamper with MIT License 5 votes vote down vote up
private void walkContent(List<Object> contentElements) {
    for (int i = 0; i < contentElements.size(); i++) {
        Object contentElement = contentElements.get(i);
        Object unwrappedObject = XmlUtils.unwrap(contentElement);
        if (unwrappedObject instanceof P) {
            P p = (P) unwrappedObject;
            ParagraphCoordinates coordinates = new ParagraphCoordinates(p, i);
            walkParagraph(coordinates);
        } else if (unwrappedObject instanceof Tbl) {
            Tbl table = (Tbl) unwrappedObject;
            TableCoordinates tableCoordinates = new TableCoordinates(table, i);
            walkTable(tableCoordinates);
        }
    }
}
 
Example 14
Source File: DocumentWalker.java    From docx-stamper with MIT License 5 votes vote down vote up
private void walkTable(Tbl table) {
    onTable(table);
    for (Object contentElement : table.getContent()) {
        if (XmlUtils.unwrap(contentElement) instanceof Tr) {
            Tr row = (Tr) contentElement;
            walkTableRow(row);
        }
    }
}
 
Example 15
Source File: Docx4j_读取内容控件_S3_Test.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
public void printSdtPrContent(SdtPr sdtPr) {  
    StringBuffer sb = new StringBuffer();  
    List<Object> rprList = sdtPr.getRPrOrAliasOrLock();  
    for (Object obj : rprList) {  
        if (obj instanceof JAXBElement) {  
            String eName = ((JAXBElement) obj).getName().getLocalPart();  
            // 布尔类型特殊处理  
            if ("temporary".equals(eName)) {  
                sb.append(" 替换后是否删除内容控件:").append("是");  
            }  
            obj = XmlUtils.unwrap(obj);  
            // System.out.println("------after="+ obj.getClass().getName());  
            if (obj instanceof CTLock) {  
                CTLock lock = (CTLock) obj;  
                if (lock != null) {  
                    sb.append(" 锁定方式:").append(lock.getVal());  
                }  
            } else if (obj instanceof Alias) {  
                Alias alias = (Alias) obj;  
                if (alias != null) {  
                    sb.append(" 标题:").append(alias.getVal());  
                }  
            }  
        } else if (obj instanceof Tag) {  
            Tag tag = (Tag) obj;  
            if (tag != null) {  
                sb.append(" tag标记:").append(tag.getVal());  
            }  
        } else if (obj instanceof Id) {  
            Id id = (Id) obj;  
            if (id != null) {  
                sb.append(" id:").append(id.getVal());  
            }  
        }  
    }  
    System.out.println(sb.toString());  
}
 
Example 16
Source File: Docx4j_删除所有批注_S3_Test.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
public void walkJAXBElements(Object parent) {  
    List children = getChildren(parent);  
    if (children != null) {  
        for (Object o : children) {  
            if (o instanceof javax.xml.bind.JAXBElement  
                    && (((JAXBElement) o).getName().getLocalPart()  
                            .equals("commentReference")  
                            || ((JAXBElement) o).getName()  
                                    .getLocalPart()  
                                    .equals("commentRangeStart") || ((JAXBElement) o)  
                            .getName().getLocalPart()  
                            .equals("commentRangeEnd"))) {  
                ((Child) ((JAXBElement) o).getValue())  
                        .setParent(XmlUtils.unwrap(parent));  
            } else {  
                o = XmlUtils.unwrap(o);  
                if (o instanceof Child) {  
                    ((Child) o).setParent(XmlUtils.unwrap(parent));  
                }  
            }  
            this.apply(o);  
            if (this.shouldTraverse(o)) {  
                walkJAXBElements(o);  
            }  
        }  
    }  
}
 
Example 17
Source File: TextMerger.java    From yarg with Apache License 2.0 5 votes vote down vote up
protected void removeUnnecessaryTexts() {
    for (Text text : textsToRemove) {
        Object parent = XmlUtils.unwrap(text.getParent());
        if (parent instanceof R) {
            ((R) parent).getContent().remove(text);
        }
    }
}
 
Example 18
Source File: TableCollector.java    From yarg with Apache License 2.0 5 votes vote down vote up
public void walkJAXBElements(Object parent) {
    List children = getChildren(parent);
    if (children != null) {

        for (Object o : children) {
            o = XmlUtils.unwrap(o);

            if (o instanceof Child && !(parent instanceof SdtBlock)) {
                ((Child) o).setParent(parent);
            }

            if (o instanceof Tbl) {
                currentTables.push(new TableManager(docxFormatter, (Tbl) o));
            }

            this.apply(o);

            if (this.shouldTraverse(o)) {
                walkJAXBElements(o);
            }

            if (o instanceof Tbl) {
                TableManager currentTable = currentTables.pop();
                currentTable.setSkipIt(false);
            }
        }
    }
}
 
Example 19
Source File: WmlElementUtils.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
public static <T> List<T> getTargetElements(Object source, Class<T> targetClass) {  
    List<T> result = new ArrayList<T>();  
    //获取真实的对象
	Object target = XmlUtils.unwrap(source);
    //if (target.getClass().equals(targetClass)) {
	if (targetClass.isAssignableFrom(target.getClass())) { 
        result.add((T) target);  
    } else if (target instanceof ContentAccessor) {  
        List<?> children = ((ContentAccessor) target).getContent();  
        for (Object child : children) {  
            result.addAll(getTargetElements(child, targetClass));  
        }
    }
    return result;  
}
 
Example 20
Source File: DocxBuilder.java    From TranskribusCore with GNU General Public License v3.0 4 votes vote down vote up
public void createFootnote(String fnComment, R r, MainDocumentPart mdp) throws Exception{
	
	// Setup FootnotesPart if necessary,
	// along with DocumentSettings
	FootnotesPart footnotesPart = mdp.getFootnotesPart();
	if (footnotesPart==null) { // that'll be the case in this example
		// initialise it
		footnotesPart = new FootnotesPart();
		mdp.addTargetPart(footnotesPart);
		
		CTFootnotes footnotes = (CTFootnotes)XmlUtils.unwrap(
				XmlUtils.unmarshalString(footnotePartXML));	
		footnotesPart.setJaxbElement(footnotes);
					
		// Usually the settings part contains footnote properties;
		// so add these if not present
		DocumentSettingsPart dsp =mdp.getDocumentSettingsPart();
		if (dsp==null) {
			// create it
			dsp = new DocumentSettingsPart();
			mdp.addTargetPart(dsp);
		} 
		CTSettings settings = dsp.getContents();
		if (settings ==null) {
			settings = wmlObjectFactory.createCTSettings(); 
			dsp.setJaxbElement(settings);				
		}
		
		CTFtnDocProps ftndocprops = settings.getFootnotePr();
		
		if (ftndocprops==null ) {
			String openXML = "<w:footnotePr xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\">"
		            + "<w:footnote w:id=\"-1\"/>" // these 2 numbers are special, and correspond with string footnotePartXML below
		            + "<w:footnote w:id=\"0\"/>"
		        +"</w:footnotePr>";
			settings.setFootnotePr(
					(CTFtnDocProps)XmlUtils.unmarshalString(openXML, Context.jc, CTFtnDocProps.class	 ));
		}
		
		
	}
	
	mdp.getPropertyResolver().activateStyle("FootnoteReference");
	//mdp.getPropertyResolver().activateStyle("FootnoteText");
	
	// OK, add a footnote
	addFootnote(footnoteCounter++, fnComment, footnotesPart, r);  
		// Note: your footnote ids must be distinct; they don't need to be ordered (though Word will do that when you open the docx)
	
}