public static DocumentAndSize [] cut(final Document doc,final PageSize size) {
		NodeList nl = doc.getElementsByTagNameNS("http://www.allcolor.org/xmlns/yahp",
				"pb");
		
		if (nl.getLength() == 0) {
			// see if someone forget namespace decl ?
			nl = doc.getElementsByTagName("yahp:pb");
		}

		// if no pagebreak found, return the given document.
		if (nl.getLength() == 0) {
			return new DocumentAndSize [] { new DocumentAndSize(doc,size) };
		} // end if

		// get the start-end offset of all page breaks.
		PbDocument pbdocs[] = getPbDocs(nl);

		// create as much document as they are pages.
		DocumentAndSize array[] = new DocumentAndSize[pbdocs.length];
		for (int i = 0; i < pbdocs.length; i++) {
			PbDocument pbdoc = pbdocs[i];

			// get start and end offset
			Element pbstart = (Element) pbdoc.getPbStart();
			Element pbend = (Element) pbdoc.getPbEnd();
			
			PageSize tmpSize = getPageSize(pbstart == null ? null : pbstart.getAttribute("size"),size);

			// create a new doc and set the URI
			ADocument ndoc = new CDom2HTMLDocument();
			ndoc.setDocumentURI(doc.getDocumentURI());

			CNamespace xmlnsdef = new CNamespace((pbend != null)
					? pbend.getPrefix()
					: pbstart.getPrefix(),
					"http://www.allcolor.org/xmlns/yahp");
			ndoc.getNamespaceList().add(xmlnsdef);

			if (pbend == null) {
				// from pbstart to the end of the document.
				// create the container node
				Element parentPb = (Element) ndoc.adoptNode(pbstart.getParentNode()
																	   .cloneNode(false));

				// copy all next siblings
				Node Sibling = pbstart.getNextSibling();

				while (Sibling != null) {
					Node node = ndoc.adoptNode(Sibling.cloneNode(true));
					// add to container node
					parentPb.appendChild(node);
					Sibling = Sibling.getNextSibling();
				} // end while

				// copy parent node of the start page break
				Node parent   = pbstart.getParentNode();
				Node nextNode = null;
				Node ppr	  = null;

				while (parent != null) {
					if (parent.getNodeType() == Node.ELEMENT_NODE) {
						// copy the node
						Node node = ndoc.adoptNode(parent.cloneNode(
									false));

						if (nextNode != null) {
							// append the node to the container
							node.appendChild(nextNode);

							if (!("body".equals(ppr.getNodeName()))) {
								// append next sibling of the previous container
								Sibling = ppr.getNextSibling();

								while (Sibling != null) {
									Node n = ndoc.adoptNode(Sibling.cloneNode(
												true));
									node.appendChild(n);
									Sibling = Sibling.getNextSibling();
								} // end while
							} // end if

							nextNode = node;
						} // end if
						else {
							nextNode = parentPb;
						} // end else

						ppr = parent;
					} // end if

					parent = parent.getParentNode();
				} // end while

				ndoc.appendChild(nextNode);
			} // end if
			else {
				Element parentPb   = (Element) ndoc.adoptNode(pbend.getParentNode()
																	   .cloneNode(false));
				Node    Sibling    = pbend.getPreviousSibling();
				boolean hasbeendes = false;

				while (!hasbeendes && (Sibling != null)) {
					if (isDescendant(pbstart, Sibling)) {
						hasbeendes = true;

						break;
					} // end if

					Node node = ndoc.adoptNode(Sibling.cloneNode(true));

					if (parentPb.getChildNodes().getLength() == 0) {
						parentPb.appendChild(node);
					} // end if
					else {
						parentPb.insertBefore(node,
							parentPb.getFirstChild());
					} // end else

					Sibling = Sibling.getPreviousSibling();
				} // end while
				
				if (hasbeendes && pbstart != Sibling) {
					Node c  = ndoc.adoptNode(Sibling.cloneNode(
								true));
					((Element)c).setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:yahp", "http://www.allcolor.org/xmlns/yahp");
					Node pb = ((Element) c).getElementsByTagNameNS("http://www.allcolor.org/xmlns/yahp",
							"pb").item(0);
					Node p  = pb;
					Node pp = pb.getParentNode()
									.getPreviousSibling();

					while (p != null) {
						Node tmp = p.getPreviousSibling();
						p.getParentNode()
							 .removeChild(p);
						p = tmp;
					} // end while

					while (pp != null) {
						Node tmp = pp.getPreviousSibling();

						if (tmp == null) {
							Node ppz = pp.getParentNode();

							if (ppz.getPreviousSibling() != null) {
								tmp = ppz.getPreviousSibling();
							} // end if
							else {
								Node prev = null;

								while (prev == null) {
									ppz = ppz.getParentNode();

									if (ppz == null) {
										break;
									} // end if

									prev = ppz.getPreviousSibling();
								} // end while

								if (prev != null) {
									tmp = prev;
								} // end if
							} // end else
						} // end if

						pp.getParentNode()
							  .removeChild(pp);
						pp = tmp;
					} // end while

					if (parentPb.getChildNodes()
								.getLength() == 0) {
						parentPb.appendChild(c);
					} // end if
					else {
						parentPb.insertBefore(c,
								parentPb.getFirstChild());
					} // end else
				} // end if				
				

				Node parent		  = pbend.getParentNode();
				Node previousNode = null;
				Node ppr		  = null;

				while (parent != null) {
					if (parent.getNodeType() == Node.ELEMENT_NODE) {
						if (previousNode != null) {
							Node node = ndoc.adoptNode(parent.cloneNode(
									false));
							node.appendChild(previousNode);

							if (!("body".equals(ppr.getNodeName()))) {
								Sibling = ppr.getPreviousSibling();

								while (!hasbeendes &&
										(Sibling != null)) {
									if (isDescendant(pbstart, Sibling)) {
										// here special handling need to be
										// taken.
										if (pbstart != Sibling) {
											Node c  = ndoc.adoptNode(Sibling.cloneNode(
														true));
											Node pb = ((Element) c).getElementsByTagNameNS("http://www.allcolor.org/xmlns/yahp",
													"pb").item(0);
											Node p  = pb;
											Node pp = pb.getParentNode()
															.getPreviousSibling();

											while (p != null) {
												Node tmp = p.getPreviousSibling();
												p.getParentNode()
													 .removeChild(p);
												p = tmp;
											} // end while

											while (pp != null) {
												Node tmp = pp.getPreviousSibling();

												if (tmp == null) {
													Node ppz = pp.getParentNode();

													if (ppz.getPreviousSibling() != null) {
														tmp = ppz.getPreviousSibling();
													} // end if
													else {
														Node prev = null;

														while (prev == null) {
															ppz = ppz.getParentNode();

															if (ppz == null) {
																break;
															} // end if

															prev = ppz.getPreviousSibling();
														} // end while

														if (prev != null) {
															tmp = prev;
														} // end if
													} // end else
												} // end if

												pp.getParentNode()
													  .removeChild(pp);
												pp = tmp;
											} // end while

											if (node.getChildNodes()
														.getLength() == 0) {
												node.appendChild(c);
											} // end if
											else {
												node.insertBefore(c,
													node.getFirstChild());
											} // end else
										} // end if

										hasbeendes = true;

										break;
									} // end if

									Node n = ndoc.adoptNode(Sibling.cloneNode(
												true));

									if (node.getChildNodes().getLength() == 0) {
										node.appendChild(n);
									} // end if
									else {
										node.insertBefore(n,
											node.getFirstChild());
									} // end else

									Sibling = Sibling.getPreviousSibling();
								} // end while
							} // end if

							previousNode = node;
						} // end if
						else {
							previousNode = parentPb;
						} // end else

						ppr = parent;
					} // end if

					parent = parent.getParentNode();
				} // end while

				ndoc.appendChild(previousNode);
			} // end else

			// copy header
			copyHeader(doc, ndoc);
			array[i] = new DocumentAndSize(ndoc,tmpSize);
		} // end for

		return array;
	}
public static DocumentAndSize [ ] cut ( final Document doc , final PageSize size ) { NodeList nl = doc . getElementsByTagNameNS ( "http://www.allcolor.org/xmlns/yahp" , "pb" ) ; if ( nl . getLength ( ) == 0 ) { nl = doc . getElementsByTagName ( "yahp:pb" ) ; } if ( nl . getLength ( ) == 0 ) { return new DocumentAndSize [ ] { new DocumentAndSize ( doc , size ) } ; } PbDocument pbdocs [ ] = getPbDocs ( nl ) ; DocumentAndSize array [ ] = new DocumentAndSize [ pbdocs . length ] ; for ( int i = 0 ; i < pbdocs . length ; i ++ ) { PbDocument pbdoc = pbdocs [ i ] ; Element pbstart = ( Element ) pbdoc . getPbStart ( ) ; Element pbend = ( Element ) pbdoc . getPbEnd ( ) ; PageSize tmpSize = getPageSize ( pbstart == null ? null : pbstart . getAttribute ( "size" ) , size ) ; ADocument ndoc = new CDom2HTMLDocument ( ) ; ndoc . setDocumentURI ( doc . getDocumentURI ( ) ) ; CNamespace xmlnsdef = new CNamespace ( ( pbend != null ) ? pbend . getPrefix ( ) : pbstart . getPrefix ( ) , "http://www.allcolor.org/xmlns/yahp" ) ; ndoc . getNamespaceList ( ) . add ( xmlnsdef ) ; if ( pbend == null ) { Element parentPb = ( Element ) ndoc . adoptNode ( pbstart . getParentNode ( ) . cloneNode ( false ) ) ; Node Sibling = pbstart . getNextSibling ( ) ; while ( Sibling != null ) { Node node = ndoc . adoptNode ( Sibling . cloneNode ( true ) ) ; parentPb . appendChild ( node ) ; Sibling = Sibling . getNextSibling ( ) ; } Node parent = pbstart . getParentNode ( ) ; Node nextNode = null ; Node ppr = null ; while ( parent != null ) { if ( parent . getNodeType ( ) == Node . ELEMENT_NODE ) { Node node = ndoc . adoptNode ( parent . cloneNode ( false ) ) ; if ( nextNode != null ) { node . appendChild ( nextNode ) ; if ( ! ( "body" . equals ( ppr . getNodeName ( ) ) ) ) { Sibling = ppr . getNextSibling ( ) ; while ( Sibling != null ) { Node n = ndoc . adoptNode ( Sibling . cloneNode ( true ) ) ; node . appendChild ( n ) ; Sibling = Sibling . getNextSibling ( ) ; } } nextNode = node ; } else { nextNode = parentPb ; } ppr = parent ; } parent = parent . getParentNode ( ) ; } ndoc . appendChild ( nextNode ) ; } else { Element parentPb = ( Element ) ndoc . adoptNode ( pbend . getParentNode ( ) . cloneNode ( false ) ) ; Node Sibling = pbend . getPreviousSibling ( ) ; boolean hasbeendes = false ; while ( ! hasbeendes && ( Sibling != null ) ) { if ( isDescendant ( pbstart , Sibling ) ) { hasbeendes = true ; break ; } Node node = ndoc . adoptNode ( Sibling . cloneNode ( true ) ) ; if ( parentPb . getChildNodes ( ) . getLength ( ) == 0 ) { parentPb . appendChild ( node ) ; } else { parentPb . insertBefore ( node , parentPb . getFirstChild ( ) ) ; } Sibling = Sibling . getPreviousSibling ( ) ; } if ( hasbeendes && pbstart != Sibling ) { Node c = ndoc . adoptNode ( Sibling . cloneNode ( true ) ) ; ( ( Element ) c ) . setAttributeNS ( "http://www.w3.org/2000/xmlns/" , "xmlns:yahp" , "http://www.allcolor.org/xmlns/yahp" ) ; Node pb = ( ( Element ) c ) . getElementsByTagNameNS ( "http://www.allcolor.org/xmlns/yahp" , "pb" ) . item ( 0 ) ; Node p = pb ; Node pp = pb . getParentNode ( ) . getPreviousSibling ( ) ; while ( p != null ) { Node tmp = p . getPreviousSibling ( ) ; p . getParentNode ( ) . removeChild ( p ) ; p = tmp ; } while ( pp != null ) { Node tmp = pp . getPreviousSibling ( ) ; if ( tmp == null ) { Node ppz = pp . getParentNode ( ) ; if ( ppz . getPreviousSibling ( ) != null ) { tmp = ppz . getPreviousSibling ( ) ; } else { Node prev = null ; while ( prev == null ) { ppz = ppz . getParentNode ( ) ; if ( ppz == null ) { break ; } prev = ppz . getPreviousSibling ( ) ; } if ( prev != null ) { tmp = prev ; } } } pp . getParentNode ( ) . removeChild ( pp ) ; pp = tmp ; } if ( parentPb . getChildNodes ( ) . getLength ( ) == 0 ) { parentPb . appendChild ( c ) ; } else { parentPb . insertBefore ( c , parentPb . getFirstChild ( ) ) ; } } Node parent = pbend . getParentNode ( ) ; Node previousNode = null ; Node ppr = null ; while ( parent != null ) { if ( parent . getNodeType ( ) == Node . ELEMENT_NODE ) { if ( previousNode != null ) { Node node = ndoc . adoptNode ( parent . cloneNode ( false ) ) ; node . appendChild ( previousNode ) ; if ( ! ( "body" . equals ( ppr . getNodeName ( ) ) ) ) { Sibling = ppr . getPreviousSibling ( ) ; while ( ! hasbeendes && ( Sibling != null ) ) { if ( isDescendant ( pbstart , Sibling ) ) { if ( pbstart != Sibling ) { Node c = ndoc . adoptNode ( Sibling . cloneNode ( true ) ) ; Node pb = ( ( Element ) c ) . getElementsByTagNameNS ( "http://www.allcolor.org/xmlns/yahp" , "pb" ) . item ( 0 ) ; Node p = pb ; Node pp = pb . getParentNode ( ) . getPreviousSibling ( ) ; while ( p != null ) { Node tmp = p . getPreviousSibling ( ) ; p . getParentNode ( ) . removeChild ( p ) ; p = tmp ; } while ( pp != null ) { Node tmp = pp . getPreviousSibling ( ) ; if ( tmp == null ) { Node ppz = pp . getParentNode ( ) ; if ( ppz . getPreviousSibling ( ) != null ) { tmp = ppz . getPreviousSibling ( ) ; } else { Node prev = null ; while ( prev == null ) { ppz = ppz . getParentNode ( ) ; if ( ppz == null ) { break ; } prev = ppz . getPreviousSibling ( ) ; } if ( prev != null ) { tmp = prev ; } } } pp . getParentNode ( ) . removeChild ( pp ) ; pp = tmp ; } if ( node . getChildNodes ( ) . getLength ( ) == 0 ) { node . appendChild ( c ) ; } else { node . insertBefore ( c , node . getFirstChild ( ) ) ; } } hasbeendes = true ; break ; } Node n = ndoc . adoptNode ( Sibling . cloneNode ( true ) ) ; if ( node . getChildNodes ( ) . getLength ( ) == 0 ) { node . appendChild ( n ) ; } else { node . insertBefore ( n , node . getFirstChild ( ) ) ; } Sibling = Sibling . getPreviousSibling ( ) ; } } previousNode = node ; } else { previousNode = parentPb ; } ppr = parent ; } parent = parent . getParentNode ( ) ; } ndoc . appendChild ( previousNode ) ; } copyHeader ( doc , ndoc ) ; array [ i ] = new DocumentAndSize ( ndoc , tmpSize ) ; } return array ; }
Cut the given document

@param doc the document to cut

@return an array of document