id = getId(endIdMethod, o);
if (unwantedId.compareTo((BigInteger)id)==0) {
// Found it
try {
CTMarkupRange mr = (CTMarkupRange)o;
List<Object> theList = null;
if (mr.getParent() instanceof List) {
theList = (List)mr.getParent(); // eg blockRange.getContents()
} else {
theList = ((ContentAccessor)(mr.getParent())).getContent();
}
Object deleteMe = null;
for (Object ox : theList) {
if (XmlUtils.unwrap(ox).equals(mr)) {
deleteMe = ox;
break;
}
}
if (deleteMe!=null) {
theList.remove(deleteMe);
}
} catch (ClassCastException e) {
log.error(e.getMessage(), e);
}
rt.ends.remove(o);
break;
}
} catch (ClassCastException cce) {
// String
id = getIdString(endIdMethod, o);
if (unwantedId.toString().equals(id) ) {
// Found it
try {
CTMarkupRange mr = (CTMarkupRange)o;
List<Object> theList = null;
if (mr.getParent() instanceof List) {
theList = (List)mr.getParent(); // eg blockRange.getContents()
} else {
theList = ((ContentAccessor)(mr.getParent())).getContent();
}
Object deleteMe = null;
for (Object ox : theList) {
if (XmlUtils.unwrap(ox).equals(mr)) {
deleteMe = ox;
}
}
if (deleteMe!=null) {
theList.remove(deleteMe);
}
} catch (ClassCastException e) {
log.error(e.getMessage(), e);
}
rt.ends.remove(o);
break;
}
}
}
}
}
// The below also renumbers bookmarks, so
// that they are unique across documents.
// Don't need to worry about refs to bookmarks,
// since these are by name, not ID. eg
// <w:instrText xml:space="preserve"> REF MyBookmark </w:instrText>
// except that we want those to be unique
// across documents so prepend doc#_
// for each opening point tag
int counter = 0; // for bookmark renumbering
for (Object o : rt.starts) {
counter++;
// long newId = global + counter; // depending on what global is, these may collide!
long newId = getBookmarkId().getAndIncrement();
if (startIdMethod == null)
startIdMethod = findGetIdMethod(o);
Object id = null;
boolean matched = false;
try {
// BigInteger (eg comment, bookmark)
id = getId(startIdMethod, o);
if (startElement.equals("CTBookmark")) {
Method setIdMethod = findSetIdMethod(o);
if (id instanceof BigInteger) {
setIdMethod.invoke(o, BigInteger.valueOf(newId));
}
// else if (id instanceof String) {
// setIdMethod.invoke(o, "" + newId);
// }
String oldName = ((CTBookmark)o).getName();
String newName = oldName + "_" + instanceNumber ; // can't start with a number
((CTBookmark)o).setName(newName);
for (Object ref : rt.refs) {
Text fieldInstr = (Text)ref;
String fieldVal = fieldInstr.getValue();
if (fieldVal.contains("REF ")
&& fieldVal.contains(" " + oldName + " ") ) {
fieldInstr.setValue(fieldVal.replace(oldName, newName));
}
}
}
// find the closing point tag
BigInteger tmpId;
for (Object end : rt.ends) {
if (endIdMethod == null)
endIdMethod = findGetIdMethod(end);
tmpId = getId(endIdMethod, end);
if (tmpId!=null
&& tmpId.equals(id)) {
// found it
matched = true;
if (endElement.equals("CTMarkupRange")) {
Method setIdMethod = findSetIdMethod(end);
if (id instanceof BigInteger) {
setIdMethod.invoke(end, BigInteger.valueOf(newId));
}
// else if (id instanceof String) {
// setIdMethod.invoke(end, "" + newId);
// }
}
break;
}
}
} catch (ClassCastException cce) {
// String (eg permStart)
id = getIdString(startIdMethod, o);
// if (startElement.equals("CTBookmark")) {
// Method setIdMethod = findSetIdMethod(o);
// if (id instanceof BigInteger) {
// setIdMethod.invoke(o, BigInteger.valueOf(newId));
// }
//// else if (id instanceof String) {
//// setIdMethod.invoke(o, "" + newId);
//// }
// }
// find the closing point tag
String tmpId;
for (Object end : rt.ends) {
if (endIdMethod == null)
endIdMethod = findGetIdMethod(end);
tmpId = getIdString(endIdMethod, end);
if (tmpId!=null
&& tmpId.equals(id)) {
// found it
matched = true;
// if (endElement.equals("CTMarkupRange")) {
// Method setIdMethod = findSetIdMethod(end);
// if (id instanceof BigInteger) {
// setIdMethod.invoke(end, BigInteger.valueOf(newId));
// }
//// else if (id instanceof String) {
//// setIdMethod.invoke(end, "" + newId);
//// }
// }
break;
}
}
}
if (!matched) {
// Object p = paragraphs.get( paragraphs.size() -1 );
// if (p instanceof P) {
// ((P)p).getParagraphContent().add(createObject(endElement,
// id));
// } else {
// System.out.println("add a close tag in " +
// p.getClass().getName() );
// }
/*
* CommentRangeEnd can be block level; Bookmark End can precede
* or follow a w:tbl closing tag.
*
* So for now, insert these at block level. I haven't checked
* the other range tags.
*
* I'm presuming the open tags can be block level as well.
*/
if (endElement.equals("CTMarkupRange")) {
CTMarkupRange mr = Context.getWmlObjectFactory().createCTMarkupRange();
// mr.setId((BigInteger)id);
mr.setId(BigInteger.valueOf(newId));
JAXBElement<CTMarkupRange> bmEnd = Context.getWmlObjectFactory().createBodyBookmarkEnd(mr);
paragraphs.add(bmEnd);
} else
if (endElement.equals("CTPerm")) {
CTPerm mr = Context.getWmlObjectFactory().createCTPerm();
mr.setId((String)id);
JAXBElement<CTPerm> rEnd = Context.getWmlObjectFactory().createBodyPermEnd(mr);
paragraphs.add(rEnd);
} else {
paragraphs.add(createObject(endElement, id));
}
if (refElement != null) {
// In practice this is always CommentReference,
// so rely on that
// if (p instanceof P) {
// R.CommentReference cr =
// Context.getWmlObjectFactory().createRCommentReference();
// cr.setId(id);
// ((P)p).getParagraphContent().add(cr);
// // that should be put in a w:r
// // <w:r><w:rPr><w:rStyle
// w:val="CommentReference"/></w:rPr><w:commentReference
// w:id="0"/></w:r>
// } else {
// System.out.println(" add a close tag in " +
// p.getClass().getName() );
// }
// <w:r><w:rPr><w:rStyle
// w:val="CommentReference"/></w:rPr><w:commentReference
// w:id="0"/></w:r>
P p = Context.getWmlObjectFactory().createP();
R r = Context.getWmlObjectFactory().createR();
p.getParagraphContent().add(r);
R.CommentReference cr = Context.getWmlObjectFactory()
.createRCommentReference();
cr.setId( (BigInteger)id);
r.getRunContent().add(cr);
paragraphs.add(p);
}
}
}
for (Object o : rt.ends) {
counter++;
long newId = getBookmarkId().getAndIncrement();
// only renumber here for ends without starts
if (endIdMethod == null)
endIdMethod = findGetIdMethod(o);
Object id = null;
boolean matched = false;
try {
// BigInteger
id = getId(endIdMethod, o);
// find the opening point tag
BigInteger tmpId;
for (Object start : rt.starts) {
if (startIdMethod == null)
startIdMethod = findGetIdMethod(start);
tmpId = getId(startIdMethod, start);
if (tmpId!=null
&& tmpId.equals(id)) {
// found it
matched = true;
break;
}
}
} catch (ClassCastException cce) {
// String
id = getIdString(endIdMethod, o);
// find the opening point tag
String tmpId;
for (Object start : rt.starts) {
if (startIdMethod == null)
startIdMethod = findGetIdMethod(start);
tmpId = getIdString(startIdMethod, start);
if (tmpId!=null
&& tmpId.equals(id)) {
// found it
matched = true;
break;
}
}
}
if (!matched) {
if (endElement.equals("CTMarkupRange")) {
// missing start, so renumber
Method setIdMethod = findSetIdMethod(o);
if (id instanceof BigInteger) {
setIdMethod.invoke(o, BigInteger.valueOf(newId));
}
}
/* I'm presuming the open tags can be block level as well. */
if (endElement.equals("CTPerm")) {
RangePermissionStart mr = Context.getWmlObjectFactory().createRangePermissionStart();
mr.setId((String)id);
JAXBElement<RangePermissionStart> rs =
Context.getWmlObjectFactory().createBodyPermStart(mr);
paragraphs.add(rs);
} else if (startElement.equals("CTBookmark")) {
log.debug("Add w:bookmarkStart");