| /* |
| * Copyright 2005 The Apache Software Foundation. |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| package org.apache.jackrabbit.webdav.transaction; |
| |
| import org.apache.log4j.Logger; |
| import org.apache.jackrabbit.webdav.xml.XmlSerializable; |
| import org.apache.jackrabbit.webdav.xml.DomUtil; |
| import org.w3c.dom.Element; |
| import org.w3c.dom.Document; |
| |
| /** |
| * <code>TransactionInfo</code> class encapsultes the information present |
| * in the {@link #XML_TRANSACTIONINFO} element that forms the request body of |
| * the UNLOCk request for a transaction lock. |
| * |
| * @see TransactionConstants#XML_TRANSACTIONINFO |
| * @see TransactionConstants#XML_TRANSACTION |
| */ |
| public class TransactionInfo implements TransactionConstants, XmlSerializable { |
| |
| private static Logger log = Logger.getLogger(TransactionInfo.class); |
| |
| private final boolean isCommit; |
| |
| /** |
| * Creates a <code>TransactionInfo</code> object |
| * |
| * @param isCommit |
| */ |
| public TransactionInfo(boolean isCommit) { |
| this.isCommit = isCommit; |
| } |
| |
| /** |
| * Creates a <code>TransactionInfo</code> object from the given 'transactionInfo' |
| * element. The 'transactionInfo' must have the following form: |
| * <pre> |
| * |
| * <!ELEMENT transactioninfo (transactionstatus) > |
| * <!ELEMENT transactionstatus ( commit | rollback ) > |
| * <!ELEMENT commit EMPTY > |
| * <!ELEMENT rollback EMPTY > |
| * </pre> |
| * @param transactionInfo as present in the UNLOCK request body. |
| * @throws IllegalArgumentException if the given transactionInfo element |
| * is not valid. |
| */ |
| public TransactionInfo(Element transactionInfo) { |
| if (transactionInfo == null || !XML_TRANSACTIONINFO.equals(transactionInfo.getLocalName())) { |
| throw new IllegalArgumentException("transactionInfo element expected."); |
| } |
| Element txStatus = DomUtil.getChildElement(transactionInfo, XML_TRANSACTIONSTATUS, NAMESPACE); |
| if (txStatus != null) { |
| // retrieve status: commit or rollback |
| isCommit = DomUtil.hasChildElement(txStatus, XML_COMMIT, NAMESPACE); |
| } else { |
| throw new IllegalArgumentException("transactionInfo must contain a single 'transactionstatus' element."); |
| } |
| } |
| |
| /** |
| * Returns true, if this info requires a 'commit' action, false otherwise |
| * (i.e. 'rollback' is requested). |
| * |
| * @return true if a 'commit' element was present. false otherwise. |
| * @see #XML_COMMIT |
| * @see #XML_ROLLBACK |
| */ |
| public boolean isCommit() { |
| return isCommit; |
| } |
| |
| //------------------------------------------< XmlSerializable interface >--- |
| /** |
| * @see org.apache.jackrabbit.webdav.xml.XmlSerializable#toXml(Document) |
| * @param document |
| */ |
| public Element toXml(Document document) { |
| Element elem = DomUtil.createElement(document, XML_TRANSACTIONINFO, NAMESPACE); |
| Element st = DomUtil.addChildElement(elem, XML_TRANSACTIONSTATUS, NAMESPACE); |
| String lName = (isCommit) ? XML_COMMIT : XML_ROLLBACK; |
| DomUtil.addChildElement(st, lName, NAMESPACE); |
| return elem; |
| } |
| |
| } |