| Java Doc By Examples | |
| Prev Class | Next Class | Frames | No Frames |
| Summary: Nested | Field | Method | Constr | Detail: Nested | Field | Method | Constr |
java.lang.Objectorg.jdom.DocumentDocType and other document-level information.
Field Summary | |
protected String |
|
Constructor Summary | |
| |
Method Summary | |
Document |
|
Document |
|
Document |
|
Document |
|
Object |
|
List<E> |
|
Element |
|
boolean | |
String |
|
List<E> |
|
Content |
|
List<E> |
|
int |
|
Iterator<E> |
|
Iterator<E> |
|
DocType |
|
Document | |
Parent |
|
Object |
|
Element |
|
boolean |
|
int |
|
int | |
List<E> |
|
Content |
|
boolean |
|
List<E> |
|
void |
|
Document |
|
Document |
|
Document |
|
Document |
|
Document |
|
void |
|
Document |
|
String |
|
Methods inherited from class java.lang.Object | |
clone, equals, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait | |
public Document()
Creates a new empty document. A document must have a root element, so this document will not be well-formed and accessor methods will throw an IllegalStateException if this document is accessed before a root element is added. This method is most useful for build tools.
1: 2: Document doc = new Document(); 3: Element easy = new Element("easy", nsShort); 4: doc.setRootElement(easy); 5: Element b = new Element("b", nsShort);
1: outputter = new XMLOutputter(); 2: currentDocument = new Document(); 3: Element root = new Element("current"); 4: currentDocument.setRootElement(root); 5: dateElement = new Element("date");
1: throws IOException { 2: Document errorDoc = new Document(); 3: Element error = new Element("error"); 4: error.setText(errorString);
public Document(List<E> content)
This will create a newDocument, with the supplied list of content, and adeclaration only if the content contains a DocType instance. A null list is treated the same as the no-arg constructor.DocType
- Parameters:
content-Listof starter content
- Throws:
IllegalAddException- if the List contains more than one Element or objects of illegal types.
1: public void testParsing() throws Exception { 2: Document doc = new Document(createGenerator()); 3: GeneratorConfig config = new GeneratorConfig(); 4: StringWriter writer = new StringWriter(); 5: XMLOutputter output = new XMLOutputter();
1: { 2: doc = new Document((Element)el.clone()); 3: ECLSearchResult result = 4: new ECLSearchResult(repository_name,xmlout.outputString(doc)); 5: ((ECLSearchHandler)ECLSearchHandler.getInstance()).
1: public Object exportContent(final Content content) { 2: return new Document(domExport(content)); 3: } 4: 5: public void exportContent(final Content content, OutputStream outStream) throws RepositoryException, IOException {
public Document(Element rootElement)
This will create a newDocument, with the suppliedas the root element, and noElementdeclaration.DocType
- Parameters:
rootElement-Elementfor document root
- Throws:
IllegalAddException- if the given rootElement already has a parent.
1: public void testParsing() throws Exception { 2: Document doc = new Document(createGenerator()); 3: GeneratorConfig config = new GeneratorConfig(); 4: StringWriter writer = new StringWriter(); 5: XMLOutputter output = new XMLOutputter();
1: { 2: doc = new Document((Element)el.clone()); 3: ECLSearchResult result = 4: new ECLSearchResult(repository_name,xmlout.outputString(doc)); 5: ((ECLSearchHandler)ECLSearchHandler.getInstance()).
1: public Object exportContent(final Content content) { 2: return new Document(domExport(content)); 3: } 4: 5: public void exportContent(final Content content, OutputStream outStream) throws RepositoryException, IOException {
public Document(Element rootElement, DocType docType)
This will create a newDocument, with the suppliedas the root element and the suppliedElementdeclaration.DocType
- Parameters:
rootElement-Elementfor document root.docType-DocTypedeclaration.
- Throws:
IllegalAddException- if the given DocType object is already attached to a document or the given rootElement already has a parent
1: DocType dt = new DocType("library", "library.dtd"); 2: Document doc = new Document(root, dt); 3: 4: Iterator publisherIter = library.getPublishers().iterator(); 5: while (publisherIter.hasNext()) {
1: this.rootOut = new Element("hibernate-mapping"); 2: this.docOut = new Document(rootOut, dType); 3: this.elclass = new Element("class"); 4: 5: this.intPKColumns = intPKColumns;
1: XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat()); 2: Document methodSetDocument = new Document(createRoot(className, timeStamp)); 3: outputter.output(methodSetDocument, ostream); 4: }
public Document(Element rootElement, DocType docType, String baseURI)
This will create a newDocument, with the suppliedas the root element, the suppliedElementdeclaration, and the specified base URI.DocType
- Parameters:
rootElement-Elementfor document root.docType-DocTypedeclaration.baseURI- the URI from which this doucment was loaded.
- Throws:
IllegalAddException- if the given docType object is already attached to a document or the given rootElement already has a parent
public Document addContent(int index, Collection<E> c)
Inserts the content in a collection into the content list at the given index. In event of an exception the original content will be unchanged and the objects in the supplied collection will be unaltered.
- Parameters:
index- location for adding the collectionc- collection to insert
- Returns:
- the parent on which the method was called
- Throws:
IndexOutOfBoundsException- if index is negative or beyond the current number of childrenIllegalAddException- if any item in the collection already has a parent or is of an illegal type.
public Document addContent(int index, Content child)
Inserts the child into the content list at the given index.
- Parameters:
index- location for adding the collectionchild- child to insert
- Returns:
- the parent on which the method was called
- Throws:
IndexOutOfBoundsException- if index is negative or beyond the current number of childrenIllegalAddException- if the given child already has a parent.
public Document addContent(Collection<E> c)
Appends all children in the given collection to the end of the content list. In event of an exception during add the original content will be unchanged and the objects in the supplied collection will be unaltered.
- Parameters:
c- collection to append
- Returns:
- the document on which the method was called
- Throws:
IllegalAddException- if any item in the collection already has a parent or is of an illegal type.
1: import org.apache.log4j.Logger; 2: import org.jdom.Document; 3: import org.jdom.Element; 4: ... 5: try { 6: org.jdom.Document doc = MCRXMLHelper.parseURI(filename, true); 7: org.jdom.Element rootelm = doc.getRootElement(); 8: ... 9: try { 10: Document doc = new Document(new Element("mcrpermissions")); 11: List permissions = AI.getPermissions(); 12: ... 13: Element rule = AI.getRule(permission); 14: mcrpermission.addContent(rule);
1: import org.apache.log4j.Logger; 2: import org.jdom.Document; 3: import org.jdom.Element; 4: ... 5: String id = request.getParameter("id"); 6: Document query = (Document) (getCache(QUERIES_KEY).get(id)); 7: 8: ... 9: xml.addContent(new Element("condition").setAttribute("format", "text").setText(cond.toString())); 10: xml.addContent(new Element("condition").setAttribute("format", "xml").addContent(cond.toXML())); 11: 12: ... 13: Element conditions = new Element("conditions"); 14: query.addContent(conditions);
public Document addContent(Content child)
Appends the child to the end of the content list.
- Parameters:
child- child to append to end of content list
- Returns:
- the document on which the method was called
- Throws:
IllegalAddException- if the given child already has a parent.
1: import org.apache.log4j.Logger; 2: import org.jdom.Document; 3: import org.jdom.Element; 4: ... 5: try { 6: org.jdom.Document doc = MCRXMLHelper.parseURI(filename, true); 7: org.jdom.Element rootelm = doc.getRootElement(); 8: ... 9: try { 10: Document doc = new Document(new Element("mcrpermissions")); 11: List permissions = AI.getPermissions(); 12: ... 13: Element rule = AI.getRule(permission); 14: mcrpermission.addContent(rule);
1: import org.apache.log4j.Logger; 2: import org.jdom.Document; 3: import org.jdom.Element; 4: ... 5: String id = request.getParameter("id"); 6: Document query = (Document) (getCache(QUERIES_KEY).get(id)); 7: 8: ... 9: xml.addContent(new Element("condition").setAttribute("format", "text").setText(cond.toString())); 10: xml.addContent(new Element("condition").setAttribute("format", "xml").addContent(cond.toXML())); 11: 12: ... 13: Element conditions = new Element("conditions"); 14: query.addContent(conditions);
public Object clone()
This will return a deep clone of thisDocument.
- Returns:
Objectclone of thisDocument
1: 2: import org.jdom.Document; 3: 4: ... 5: MCREditorSubmission sub = (MCREditorSubmission) (job.getRequest().getAttribute("MCREditorSubmission")); 6: org.jdom.Document indoc = sub.getXML(); 7: 8: ... 9: 10: org.jdom.Document outdoc = prepareMetadata((org.jdom.Document) indoc.clone(), ID, job, lang); 11: outxml = MCRUtils.getByteArray(outdoc);
1: import org.apache.log4j.Logger; 2: import org.jdom.Document; 3: import org.jdom.Element; 4: ... 5: 6: org.jdom.Document jdom = null; 7: Query qParam = new Query(); 8: ... 9: } catch (Exception ex) { 10: generateErrorPage(request, response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Error while forwarding XML document to LayoutServlet!", ex, 11: false); 12: ... 13: private final String getBrowseElementID(org.jdom.Document jdom, String ref, boolean next) throws MCRException { 14: org.jdom.Document tempDoc = (org.jdom.Document) jdom.clone();
public List<E> cloneContent()
Returns a list containing detached clones of this parent's content list.
- Specified by:
- cloneContent in interface Parent
- Returns:
- list of cloned child content
public Element detachRootElement()
Detach the rootfrom this document.Element
- Returns:
- removed root
Element
1: import org.embl.ebi.escience.scuflworkers.XMLHandler; 2: import org.jdom.Document; 3: import org.jdom.Element; 4: ... 5: else { 6: Document doc = XScuflView.getDocument(wp.getInternalModel()); 7: ... 8: spec.addContent(doc.detachRootElement()); 9: }
1: import org.embl.ebi.escience.scuflui.shared.ModelMap.ModelChangeListener; 2: import org.jdom.Document; 3: import org.jdom.Element; 4: ... 5: SAXBuilder builder = new SAXBuilder(false); 6: Document document; 7: try { 8: ... 9: 10: layout.addContent(document.detachRootElement()); 11: 12: ... 13: 14: Element perspectiveLayout = new SAXBuilder().build(perspective.getLayoutInputStream()).detachRootElement();
public final boolean equals(Object ob)
This tests for equality of thisDocumentto the suppliedObject.
- Parameters:
ob-Objectto compare to
- Returns:
booleanwhether theDocumentis equal to the suppliedObject
1: import org.jdom.DocType; 2: import org.jdom.Document; 3: import org.jdom.Element; 4: ... 5: 6: public static Document parseURI(String uri) throws MCRException { 7: return getParser().parseURI(uri); 8: ... 9: 10: public static Document parseURI(String uri, boolean valid) throws MCRException { 11: return getParser().parseURI(uri, valid); 12: ... 13: String v2 = t2.getValue(); 14: return v1.equals(v2);
public final String getBaseURI()
Returns the URI from which this document was loaded, or null if this is not known.
- Returns:
- the base URI of this document
public List<E> getContent()
This will return all content for theDocument. The returned list is "live" in document order and changes to it affect the document's actual content. Sequential traversal through the List is best done with a Iterator since the underlying implement of List.size() may require walking the entire list.
- Specified by:
- getContent in interface Parent
- Returns:
List- all Document content
- Throws:
IllegalStateException- if the root element hasn't been set
public Content getContent(int index)
Returns the child at the given index.
- Specified by:
- getContent in interface Parent
- Parameters:
index- location of desired child
- Returns:
- child at the given index
- Throws:
IndexOutOfBoundsException- if index is negative or beyond the current number of childrenIllegalStateException- if parent is a Document and the root element is not set
public List<E> getContent(Filter filter)
Return a filtered view of thisDocument's content. Sequential traversal through the List is best done with a Iterator since the underlying implement of List.size() may require walking the entire list.
- Specified by:
- getContent in interface Parent
- Parameters:
filter-Filterto apply
- Returns:
List- filtered Document content
- Throws:
IllegalStateException- if the root element hasn't been set
public int getContentSize()
Returns the number of children in this parent's content list. Children may be anyContenttype.
- Specified by:
- getContentSize in interface Parent
- Returns:
- number of children
public Iterator<E> getDescendants()
Returns an iterator that walks over all descendants in document order.
- Specified by:
- getDescendants in interface Parent
- Returns:
- an iterator to walk descendants
public Iterator<E> getDescendants(Filter filter)
Returns an iterator that walks over all descendants in document order applying the Filter to return only elements that match the filter rule. With filters you can match only Elements, only Comments, Elements or Comments, only Elements with a given name and/or prefix, and so on.
- Specified by:
- getDescendants in interface Parent
- Parameters:
filter- filter to select which descendants to see
- Returns:
- an iterator to walk descendants within a filter
1: import org.apache.commons.logging.LogFactory; 2: import org.jdom.Document; 3: import org.jdom.Element; 4: ... 5: 6: public Document readConfigurationStreamToData(String name, InputStream in) 7: throws ConfigurationFormatException { 8: ... 9: 10: Document document = null; 11: ConfigEntityResolver configEntityResolver = 12: ... 13: protected boolean containsDtd(Document document) { 14: return (document.getDocType() != null);
public Document getDocument()
- Specified by:
- getDocument in interface Parent
- See Also:
Parent.getDocument()
public Parent getParent()
Return this parent's parent, or null if this parent is currently not attached to another parent. This is the same method as in Content but also added to Parent to allow more easy up-the-tree walking.
- Returns:
- this parent's parent or null if none
public Object getProperty(String id)
Returns the object associated with this document under the given "id" string, or null if there is no binding or if the binding explicitly stored a null value.
- Parameters:
id- the id of the stored object to return
- Returns:
- the object associated with the given id
public Element getRootElement()
This will return the rootElementfor thisDocument
- Returns:
Element- the document's root element
- Throws:
IllegalStateException- if the root element hasn't been set
1: 2: import org.jdom.Document; 3: import org.jdom.Element; 4: ... 5: SAXBuilder builder = new SAXBuilder(); 6: Document books = builder.build("books.xml"); 7: ... 8: Document onebook = builder.build("onebook.xml"); 9: ... 10: Element bookToAdd = onebook.getRootElement().getChild("book");
1: import org.jdom.Attribute; 2: import org.jdom.Document; 3: import org.jdom.Element; 4: ... 5: 6: Document jdom = new Document(new Element("mcraccessrules")); 7: if (operation.equals("select")){ 8: ... 9: Element el = getRule(ruleid); 10: jdom.getRootElement().addContent(el); 11: }else if(operation.equals("selectall")){ 12: ... 13: for (int i=0; i<rules.size(); i++ ){ 14: jdom.getRootElement().addContent((Element) rules.get(i));
1: 2: import org.jdom.Document; 3: import org.jdom.Element; 4: ... 5: 6: private Document document; 7: 8: ... 9: if (inputElement.getName().equals("book")) { 10: document.getRootElement().addContent(inputElement); 11: } else {
1: import org.jdom.Attribute; 2: import org.jdom.Document; 3: import org.jdom.Element; 4: ... 5: 6: Document jdom = new Document(new Element("mycoreaccesscheck")); 7: ... 8: jdom.getRootElement().addContent(new Element("accesscheck")); 9: ... 10: jdom.getRootElement().getChild("accesscheck").setAttribute(new Attribute("return", String.valueOf(result)));
1: 2: import org.jdom.Document; 3: import org.jdom.Element; 4: ... 5: 6: public static Document getMetaDataDocument(Classification cl) { 7: return MetaDataElementFactory.getDocument(cl); 8: ... 9: 10: public static Document getEditorDocument(Classification cl) { 11: return ItemElementFactory.getDocument(cl, STANDARD_LABEL); 12: ... 13: Document cd = new Document(new Element("mycoreclass")); 14: cd.getRootElement().setAttribute("noNamespaceSchemaLocation", "MCRClassification.xsd", Namespace.getNamespace("xsi", MCRDefaults.XSI_URL));
public boolean hasRootElement()
This will returntrueif this document has a root element,falseotherwise.
- Returns:
trueif this document has a root element,falseotherwise.
public final int hashCode()
This returns the hash code for thisDocument.
- Returns:
inthash code
public int indexOf(Content child)
Returns the index of the supplied child in the content list, or -1 if not a child of this parent.
- Parameters:
child- child to search for
- Returns:
- index of child, or -1 if not found
public List<E> removeContent()
Removes all child content from this parent.
- Specified by:
- removeContent in interface Parent
- Returns:
- list of the old children detached from this parent
public Content removeContent(int index)
Removes and returns the child at the given index, or returns null if there's no such child.
- Specified by:
- removeContent in interface Parent
- Parameters:
index- index of child to remove
- Returns:
- detached child at given index or null if no
- Throws:
IndexOutOfBoundsException- if index is negative or beyond the current number of children
public boolean removeContent(Content child)
Removes a single child node from the content list.
- Specified by:
- removeContent in interface Parent
- Parameters:
child- child to remove
- Returns:
- whether the removal occurred
public List<E> removeContent(Filter filter)
Remove all child content from this parent matching the supplied filter.
- Specified by:
- removeContent in interface Parent
- Parameters:
filter- filter to select which content to remove
- Returns:
- list of the old children detached from this parent
public final void setBaseURI(String uri)
Sets the effective URI from which this document was loaded, and against which relative URLs in this document will be resolved.
- Parameters:
uri- the base URI of this document
public Document setContent(int index, Collection<E> collection)
Replace the child at the given index whith the supplied collection. In event of an exception the original content will be unchanged and the content in the supplied collection will be unaltered.
- Parameters:
index- - index of child to replace.collection- - collection of content to add.
- Returns:
- object on which the method was invoked
- Throws:
IllegalAddException- if the collection contains objects of illegal types.IndexOutOfBoundsException- if index is negative or greater than the current number of children.
public Document setContent(Collection<E> newContent)
This sets the content of theDocument. The supplied List should contain only objects of typeElement,Comment, andProcessingInstruction. When all objects in the supplied List are legal and before the new content is added, all objects in the old content will have their parentage set to null (no parent) and the old content list will be cleared. This has the effect that any active list (previously obtained with a call togetContent) will also change to reflect the new content. In addition, all objects in the supplied List will have their parentage set to this document, but the List itself will not be "live" and further removals and additions will have no effect on this document content. If the user wants to continue working with a "live" list, then a call to setContent should be followed by a call togetContentto obtain a "live" version of the content. Passing a null or empty List clears the existing content. In event of an exception the original content will be unchanged and the objects in the supplied content will be unaltered.
- Parameters:
newContent-Listof content to set
- Returns:
- this document modified
- Throws:
IllegalAddException- if the List contains objects of illegal types or with existing parentage.
public Document setContent(Content child)
Set this document's content to be the supplied child. If the supplied child is legal content for a Document and before it is added, all content in the current content list will be cleared and all current children will have their parentage set to null. This has the effect that any active list (previously obtained with a call to one of thegetContentmethods will also change to reflect the new content. In addition, all content in the supplied collection will have their parentage set to this Document. If the user wants to continue working with a "live" list of this Document's child, then a call to setContent should be followed by a call to one of thegetContentmethods to obtain a "live" version of the children. Passing a null child clears the existing content. In event of an exception the original content will be unchanged and the supplied child will be unaltered.
- Parameters:
child- new content to replace existing content
- Returns:
- the parent on which the method was called
- Throws:
IllegalAddException- if the supplied child is already attached or not legal content for this parent
public Document setDocType(DocType docType)
This will set thedeclaration for thisDocTypeDocument. Note that a DocType can only be attached to one Document. Attempting to set the DocType to a DocType object that already belongs to a Document will result in an IllegalAddException being thrown.
- Parameters:
docType-DocTypedeclaration.
- Returns:
- object on which the method was invoked
- Throws:
IllegalAddException- if the given docType is already attached to a Document.
1: import org.jdom.Attribute; 2: import org.jdom.Document; 3: import org.jdom.DocType; 4: ... 5: Element root = getXMLRepresentation(obj.getObject()); 6: Document doc = new Document(root); 7: 8: ... 9: DocType docType = new DocType(root.getName(), publicID, systemID); 10: doc.setDocType(docType); 11: }
public void setProperty(String id, Object value)
Assigns an arbitrary object to be associated with this document under the given "id" string. Null values are permitted. Strings beginning with "http://www.jdom.org/ are reserved for JDOM use.
- Parameters:
id- the id of the stored objectvalue- the object to store
1: import org.jaxen.jdom.JDOMXPath; 2: import org.jdom.Document; 3: import org.jdom.Element; 4: ... 5: 6: public static void create(final String mcrType, final org.jdom.Document datamodel) 7: throws MCRConfigurationException, MCRPersistenceException { 8: ... 9: final String itemPrefix = cutString(conf.getString(sb + "_prefix"), 2); 10: datamodel.setProperty("itemPrefix", itemPrefix); 11: 12: ... 13: 14: private static void buildChildComponents(final Document metamodel, final DKDatastoreDefICM dsDefICM,
public Document setRootElement(Element rootElement)
This sets the rootfor theElementDocument. If the document already has a root element, it is replaced.
- Parameters:
rootElement-Elementto be new root.
- Returns:
Document- modified Document.
- Throws:
IllegalAddException- if the given rootElement already has a parent.
1: 2: import org.jdom.Document; 3: import org.jdom.Element; 4: ... 5: 6: Document doc = new Document(); 7: Element easy = new Element("easy", nsShort); 8: ... 9: doc.setRootElement(easy); 10: Element b = new Element("b", nsShort);
1: 2: import org.jdom.Document; 3: import org.jdom.Element; 4: ... 5: { 6: private Document document; 7: private String portletApplication; 8: ... 9: root = new Element("Context"); 10: document.setRootElement(root); 11: }
1: 2: import org.jdom.Document; 3: import org.jdom.Element; 4: ... 5: 6: private Document currentDocument; 7: 8: ... 9: outputter = new XMLOutputter(); 10: currentDocument = new Document(); 11: Element root = new Element("current"); 12: ... 13: currentDocument.setRootElement(root);
1: 2: import org.jdom.Document; 3: import org.jdom.Element; 4: ... 5: { 6: Document document = new Document(); 7: Element root = new org.jdom.Element("converted-values"); 8: ... 9: document.setRootElement(root);
1: 2: import org.jdom.Document; 3: import org.jdom.Element; 4: ... 5: PrivateKey privateKey) throws SamlException { 6: Document doc = Util.createJdomDoc(samlResponse); 7: if (doc != null) { 8: ... 9: publicKey); 10: doc.setRootElement((Element) signedElement.detach()); 11: XMLOutputter xmlOutputter = new XMLOutputter(); 12: ... 13: } else { 14: throw new SamlException("Error signing SAML Response: Null document");
public String toString()
This returns aStringrepresentation of theDocument, suitable for debugging. If the XML representation of theDocumentis desired,XMLOutputter.outputString(Document)should be used.
- Returns:
String- information about theDocument
1: 2: import org.jdom.Document; 3: import org.jdom.Element; 4: ... 5: returnXML.append("\r\n</converted-values>"); 6: return returnXML.toString(); 7: } 8: ... 9: { 10: Document document = new Document(); 11: Element root = new org.jdom.Element("converted-values"); 12: ... 13: 14: String xml = document.toString();