0001: /*
0002: * The contents of this file are subject to the
0003: * Mozilla Public License Version 1.1 (the "License");
0004: * you may not use this file except in compliance with the License.
0005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
0006: *
0007: * Software distributed under the License is distributed on an "AS IS"
0008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
0009: * See the License for the specific language governing rights and
0010: * limitations under the License.
0011: *
0012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
0013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
0014: *
0015: * All Rights Reserved.
0016: *
0017: * Contributor(s):
0018: */
0019: package org.openharmonise.rm.publishing;
0020:
0021: import java.io.*;
0022: import java.util.*;
0023:
0024: import org.openharmonise.commons.cache.CacheException;
0025: import org.openharmonise.commons.dsi.AbstractDataStoreInterface;
0026: import org.openharmonise.commons.xml.*;
0027: import org.openharmonise.commons.xml.parser.*;
0028: import org.openharmonise.rm.DataAccessException;
0029: import org.openharmonise.rm.config.*;
0030: import org.openharmonise.rm.factory.*;
0031: import org.openharmonise.rm.metadata.*;
0032: import org.openharmonise.rm.resources.*;
0033: import org.openharmonise.rm.resources.metadata.values.Value;
0034: import org.openharmonise.rm.resources.publishing.WebPage;
0035: import org.openharmonise.rm.resources.users.User;
0036: import org.openharmonise.rm.sessions.*;
0037: import org.openharmonise.rm.view.servlet.utils.HttpRequestManager;
0038: import org.w3c.dom.*;
0039:
0040: /**
0041: * This class provides an XML representation of a context with the process of
0042: * publishing content within the Harmonise framework. Extending <code>XMLDocument</code>, it is
0043: * generally a XML representation of the HTTP query string which the <code>Harmonise</code>
0044: * servlet receives.
0045: *
0046: * @author Michael Bell
0047: * @version $Revision: 1.2 $
0048: *
0049: */
0050: public class State extends XMLDocument {
0051:
0052: private Map m_requestHeaders;
0053: private String m_requestRemoteAddress = null;
0054:
0055: //config constant prop names
0056: protected static final String PNAME_DEFAULT_PAGE = "DEFAULT_PAGE";
0057:
0058: //XML constants
0059: public static final String TAG_STATE = "State";
0060: public static final String ATTRIB_STATE_ID = "stateId";
0061: public static final String TAG_REFERER = "Referer";
0062:
0063: protected User m_User = null;
0064: protected Profile m_UserProf = null;
0065: protected String m_stringcontent = "";
0066: protected File m_filecontent = null;
0067: protected PublishFilter m_publishFilter = null;
0068: protected AbstractDataStoreInterface m_dsi = null;
0069: protected boolean m_bIsPopulated = false;
0070: private static HashSet ignoreTags = new HashSet(89);
0071:
0072: static {
0073: ignoreTags.add("show_xml");
0074: ignoreTags.add("ignore_submit");
0075: ignoreTags.add("ignore_submit.x");
0076: ignoreTags.add("ignore_submit.y");
0077: ignoreTags.add("ignore_filename");
0078: ignoreTags.add("ignore_filepath");
0079: }
0080:
0081: /**
0082: * Constructs State object from a W3C Document.
0083: *
0084: * @param document - The document to use
0085: */
0086: public State(AbstractDataStoreInterface dbintrf)
0087: throws StateException {
0088: super ();
0089: m_dsi = dbintrf;
0090: setBlankUserDetails();
0091: }
0092:
0093: /**
0094: * Constructs State object from a W3C Document.
0095: *
0096: * @param document - The document to use
0097: */
0098: public State(AbstractDataStoreInterface dbintrf, User usr)
0099: throws StateException {
0100: super ();
0101: m_dsi = dbintrf;
0102: m_User = usr;
0103: }
0104:
0105: /**
0106: * Constructs State object from W3C Document.
0107: *
0108: * @param document - The document to use
0109: */
0110: public State(org.w3c.dom.Document document,
0111: AbstractDataStoreInterface dbintrf) throws StateException {
0112: super (document);
0113: m_dsi = dbintrf;
0114: setBlankUserDetails();
0115: }
0116:
0117: /**
0118: * Finds element in state which matches the given element
0119: *
0120: * @param el Element to match in state
0121: * @param state XML representation of state
0122: * @exception Exception
0123: * @return Matching Element found in state
0124: */
0125: public Element findElement(Element el) {
0126: Element foundEl = null;
0127: String sTagName = el.getTagName();
0128:
0129: NodeList nodes = getElementsByTagName(sTagName);
0130:
0131: String sFindName = el.getAttribute(AbstractObject.ATTRIB_NAME);
0132: String sFindId = el.getAttribute(AbstractObject.ATTRIB_ID);
0133: String sFindStateId = el.getAttribute(ATTRIB_STATE_ID);
0134:
0135: for (int i = 0; i < nodes.getLength(); i++) {
0136: Element next = (Element) nodes.item(i);
0137: String sNextName = next
0138: .getAttribute(AbstractObject.ATTRIB_NAME);
0139: String sNextId = next
0140: .getAttribute(AbstractObject.ATTRIB_ID);
0141: String sNextStateId = next.getAttribute(ATTRIB_STATE_ID);
0142:
0143: boolean bFoundName = false;
0144: boolean bFoundId = false;
0145: boolean bFoundStateId = false;
0146:
0147: if (!sFindName.equalsIgnoreCase("")
0148: && !sNextName.equalsIgnoreCase("")) {
0149: if (sFindName.equals(sNextName)) {
0150: bFoundName = true;
0151: }
0152: } else {
0153: bFoundName = true;
0154: }
0155:
0156: if (!sFindId.equalsIgnoreCase("")
0157: && !sNextId.equalsIgnoreCase("")) {
0158: if (sFindId.equals(sNextId)) {
0159: bFoundId = true;
0160: }
0161: } else {
0162: bFoundId = true;
0163: }
0164:
0165: if (!sFindStateId.equalsIgnoreCase("")
0166: && !sNextStateId.equalsIgnoreCase("")) {
0167: if (sFindStateId.equals(sNextStateId)) {
0168: bFoundStateId = true;
0169: }
0170: } else {
0171: bFoundStateId = true;
0172: }
0173:
0174: if (bFoundId && bFoundName && bFoundStateId) {
0175: foundEl = next;
0176: }
0177: }
0178:
0179: return foundEl;
0180: }
0181:
0182: /**
0183: * Finds elements in state which matches the given element.
0184: *
0185: * @param el Element to match in state
0186: * @param state XML representation of state
0187: * @exception Exception
0188: * @return Matching Element found in state
0189: */
0190: public List findElements(Element el) {
0191: Element foundEl = null;
0192: String sTagName = el.getTagName();
0193: List vRetn = new ArrayList();
0194:
0195: NodeList nodes = getElementsByTagName(sTagName);
0196:
0197: String sFindName = el.getAttribute(AbstractObject.ATTRIB_NAME);
0198: String sFindId = el.getAttribute(AbstractObject.ATTRIB_ID);
0199: String sFindStateId = el.getAttribute(ATTRIB_STATE_ID);
0200:
0201: for (int i = 0; i < nodes.getLength(); i++) {
0202: Element next = (Element) nodes.item(i);
0203: String sNextName = next
0204: .getAttribute(AbstractObject.ATTRIB_NAME);
0205: String sNextId = next
0206: .getAttribute(AbstractObject.ATTRIB_ID);
0207: String sNextStateId = next.getAttribute(ATTRIB_STATE_ID);
0208:
0209: boolean bFoundName = false;
0210: boolean bFoundId = false;
0211: boolean bFoundStateId = false;
0212:
0213: if (!sFindName.equalsIgnoreCase("")
0214: && !sNextName.equalsIgnoreCase("")) {
0215: if (sFindName.equals(sNextName)) {
0216: bFoundName = true;
0217: }
0218: } else {
0219: bFoundName = true;
0220: }
0221:
0222: if (!sFindId.equalsIgnoreCase("")
0223: && !sNextId.equalsIgnoreCase("")) {
0224: if (sFindId.equals(sNextId)) {
0225: bFoundId = true;
0226: }
0227: } else {
0228: bFoundId = true;
0229: }
0230:
0231: if (!sFindStateId.equalsIgnoreCase("")
0232: && !sNextStateId.equalsIgnoreCase("")) {
0233: if (sFindStateId.equals(sNextStateId)) {
0234: bFoundStateId = true;
0235: }
0236: } else {
0237: bFoundStateId = true;
0238: }
0239:
0240: if (bFoundId && bFoundName && bFoundStateId) {
0241: vRetn.add(next);
0242: }
0243: }
0244:
0245: return vRetn;
0246: }
0247:
0248: /**
0249: * Resolves a relative path to an absolute one. Only copes
0250: * with relative tokens at the beginning of a path.
0251: *
0252: * @param sClassName The tag name for the class that the path is in the context of
0253: * @param sPath The path
0254: * @return An absolute version of the path
0255: * @throws Exception
0256: */
0257: public String resolveRelativePath(String sClassName, String sPath)
0258: throws StateException {
0259: String sReturn = null;
0260:
0261: if (sPath.startsWith(".")) {
0262: StringTokenizer tokenizer = new StringTokenizer(sPath, "/");
0263: StringBuffer sNewPath = new StringBuffer();
0264: AbstractChildObject obj = (AbstractChildObject) getHarmoniseObject(sClassName);
0265:
0266: // if there is not an object of the required type, see if any
0267: // children are in the state
0268: if (obj == null) {
0269: Element el = createElement(sClassName);
0270: AbstractObject ohObj = null;
0271: try {
0272: ohObj = HarmoniseObjectFactory
0273: .instantiateHarmoniseObject(m_dsi, el, this );
0274: } catch (HarmoniseFactoryException e) {
0275: throw new StateException(
0276: "Error getting object from factory", e);
0277: }
0278:
0279: if (ohObj instanceof AbstractParentObject) {
0280: AbstractParentObject parent = (AbstractParentObject) ohObj;
0281:
0282: List childClassNames = parent.getChildClassNames();
0283:
0284: Iterator iter = childClassNames.iterator();
0285:
0286: boolean bFound = false;
0287:
0288: while (iter.hasNext() == true && bFound == false) {
0289: sClassName = (String) iter.next();
0290:
0291: if (sClassName.indexOf('.') >= 0) {
0292: sClassName = sClassName
0293: .substring(sClassName
0294: .lastIndexOf('.') + 1);
0295: }
0296:
0297: obj = (AbstractChildObject) getHarmoniseObject(sClassName);
0298:
0299: bFound = (obj != null);
0300: }
0301:
0302: } else {
0303: // must be a child object
0304: sClassName = ((AbstractChildObject) ohObj)
0305: .getParentObjectClassName();
0306:
0307: if (sClassName.indexOf('.') >= 0) {
0308: sClassName = sClassName.substring(sClassName
0309: .lastIndexOf('.') + 1);
0310: }
0311:
0312: obj = (AbstractChildObject) getHarmoniseObject(sClassName);
0313: }
0314: }
0315:
0316: if (obj != null) {
0317: String sNext = null;
0318:
0319: while (tokenizer.hasMoreTokens()) {
0320: sNext = tokenizer.nextToken();
0321:
0322: try {
0323: if (sNext.equals("..")) {
0324: if (sNewPath.length() > 0) {
0325: sNewPath.append("/");
0326: }
0327:
0328: obj = (AbstractChildObject) obj
0329: .getRealParent();
0330: sNewPath = new StringBuffer(obj.getPath());
0331: sNewPath.append("/");
0332: sNewPath.append(obj.getName());
0333: } else if (sNext.equals(".")) {
0334: if (sNewPath.length() > 0) {
0335: sNewPath.append("/");
0336: }
0337:
0338: sNewPath.append(obj.getPath());
0339: sNewPath.append("/");
0340: sNewPath.append(obj.getName());
0341: } else {
0342: if (sNewPath.length() > 0) {
0343: sNewPath.append("/");
0344: }
0345:
0346: sNewPath.append(sNext);
0347:
0348: while (tokenizer.hasMoreTokens()) {
0349: sNewPath.append("/");
0350: sNext = tokenizer.nextToken();
0351: sNewPath.append(sNext);
0352: }
0353: }
0354: } catch (DataAccessException e) {
0355: throw new StateException(
0356: "Error occured accessing object data",
0357: e);
0358: }
0359: }
0360:
0361: sReturn = sNewPath.toString();
0362: }
0363: } else {
0364: sReturn = sPath;
0365: }
0366:
0367: return sReturn;
0368: }
0369:
0370: /**
0371: * Adds the file as extra contents to this representation of the state.
0372: *
0373: * @param fileContent
0374: */
0375: public void addFileContents(File fileContent) {
0376: this .m_filecontent = fileContent;
0377: }
0378:
0379: /**
0380: * Adds <code>sContent</code> as extra content to this state.
0381: *
0382: * @param stringContent
0383: */
0384: public void addStringContents(String sContent) {
0385: this .m_stringcontent = sContent;
0386: }
0387:
0388: /**
0389: * Returns the <code>File</code> contents of this state.
0390: *
0391: * @return
0392: */
0393: public File getFile() {
0394: return m_filecontent;
0395: }
0396:
0397: /**
0398: * Returns the <code>String</code> contents of this state.
0399: *
0400: * @return
0401: */
0402: public String getStringContent() {
0403: return m_stringcontent;
0404: }
0405:
0406: /**
0407: * Finds logged in User from Session in the state and returns their Profile.
0408: *
0409: * @param state XML state representation
0410: * @exception Exception
0411: * @return Profile of current logged in user
0412: */
0413: public Profile getLoggedInUserProfile() {
0414:
0415: return m_UserProf;
0416: }
0417:
0418: /**
0419: * Finds and returns the logged-in User found from Session in the state.
0420: *
0421: * @param state XML representation of the state
0422: * @exception Exception
0423: * @return User found in the state
0424: */
0425: public User getLoggedInUser() {
0426:
0427: return m_User;
0428: }
0429:
0430: /**
0431: * Adds a user to this state representation.
0432: *
0433: * @param usr
0434: */
0435: public void setLoggedInUser(User usr) {
0436: m_User = usr;
0437: }
0438:
0439: /**
0440: * Returns the current Webpage id found in the state.
0441: *
0442: * @param state XML representation of the state
0443: * @exception Exception
0444: * @return Current Webpage id
0445: */
0446: public int getCurrentPageId() throws Exception {
0447: Element pageEl = XMLUtils.getFirstNamedChild(
0448: getDocumentElement(), WebPage.TAG_PAGE);
0449:
0450: int nId = -1;
0451:
0452: if (pageEl != null) {
0453: nId = Integer.parseInt(pageEl
0454: .getAttribute(AbstractObject.ATTRIB_ID));
0455: }
0456:
0457: return nId;
0458: }
0459:
0460: /**
0461: * Returns the page id from state.
0462: *
0463: * @return
0464: */
0465: public int getPageId() {
0466: NodeList xnlPage = getElementsByTagName(WebPage.TAG_PAGE);
0467: int nId = -1;
0468:
0469: if (xnlPage.getLength() > 0) {
0470: nId = Integer.parseInt(((Element) xnlPage.item(0))
0471: .getAttribute(AbstractObject.ATTRIB_ID));
0472: }
0473:
0474: return nId;
0475: }
0476:
0477: /**
0478: * Returns the session id from state.
0479: *
0480: * @return
0481: */
0482: public String getSessionId() {
0483: NodeList xnlSession = getElementsByTagName(Session.TAG_SESSION);
0484: String sSessionId = null;
0485:
0486: if (xnlSession.getLength() > 0) {
0487: sSessionId = ((Element) xnlSession.item(0))
0488: .getAttribute(AbstractObject.ATTRIB_ID);
0489: }
0490:
0491: return sSessionId;
0492: }
0493:
0494: /**
0495: * Returns the session associated with this state.
0496: *
0497: * @return
0498: * @throws DataAccessException
0499: */
0500: public Session getSession() throws DataAccessException {
0501: Session session = null;
0502: try {
0503: String sSessionId = getSessionId();
0504: if (sSessionId != null) {
0505: session = SessionCache.getInstance(m_dsi).getSession(
0506: sSessionId);
0507: }
0508: } catch (CacheException e) {
0509: throw new DataAccessException(
0510: "Error getting session from cache", e);
0511: }
0512:
0513: return session;
0514: }
0515:
0516: /**
0517: * Returns a String that is this state converted to a list of HTTP params.
0518: *
0519: * @return The parameters
0520: */
0521: public String encodeAsHttpParams() {
0522: NodeList nodes = getDocumentElement().getChildNodes();
0523: StringBuffer sUrl = new StringBuffer();
0524:
0525: for (int i = 0; i < nodes.getLength(); i++) {
0526: if (nodes.item(i).getNodeType() != Node.ELEMENT_NODE) {
0527: continue;
0528: }
0529:
0530: if (sUrl.length() != 0) {
0531: sUrl.append("&");
0532: }
0533:
0534: Element elNext = (Element) nodes.item(i);
0535: addHttpParams(sUrl, new StringBuffer(), elNext);
0536: }
0537:
0538: return sUrl.toString();
0539: }
0540:
0541: /**
0542: * Adds HTTP parameters to a URL, recurses to grow the URL.
0543: *
0544: * @param sUrl The URL that is being built
0545: * @param sCurrentFragment The current context
0546: * @param el The Element to start at
0547: */
0548: protected void addHttpParams(StringBuffer sUrl,
0549: StringBuffer sCurrentFragment, Element el) {
0550:
0551: sCurrentFragment.append(el.getTagName());
0552: String sId = el.getAttribute(AbstractObject.ATTRIB_ID);
0553:
0554: StringBuffer sValue = new StringBuffer();
0555: Vector elements = new Vector();
0556: Vector names = new Vector();
0557:
0558: NodeList nodes = el.getChildNodes();
0559: for (int i = 0; i < nodes.getLength(); i++) {
0560: if (nodes.item(i).getNodeType() == Node.TEXT_NODE) {
0561: String sText = nodes.item(i).getNodeValue();
0562: if (sText.trim().length() > 0) {
0563: sValue.append(sText);
0564: }
0565: } else if (nodes.item(i).getNodeType() == Node.ELEMENT_NODE) {
0566: Element elNext = (Element) nodes.item(i);
0567: String sName = elNext.getTagName();
0568: if (sName.equals(Profile.TAG_PROFILE)
0569: || sName
0570: .equals(AbstractPropertyInstance.TAG_PROPERTYINSTANCE)) {
0571: sName = elNext
0572: .getAttribute(AbstractObject.ATTRIB_NAME);
0573: }
0574: if (names.contains(sName)) {
0575: if (elNext.getAttribute(AbstractObject.ATTRIB_ID) == null) {
0576: elNext.setAttribute(AbstractObject.ATTRIB_ID,
0577: Integer.toString(i));
0578: }
0579: } else {
0580: names.add(sName);
0581: }
0582: elements.add(elNext);
0583: }
0584: }
0585:
0586: if (sValue.length() > 0) {
0587: sUrl.append(sCurrentFragment);
0588: sUrl.append("=");
0589: sUrl.append(sValue);
0590: } else if (elements.size() == 0) {
0591: sUrl.append(sCurrentFragment);
0592: sUrl.append("/");
0593: sUrl.append("@id=");
0594: sUrl.append(sId);
0595: } else {
0596: if (sId != null) {
0597: sCurrentFragment.append("[@id=");
0598: sCurrentFragment.append(sId);
0599: sCurrentFragment.append("]/");
0600: }
0601: for (int j = 0; j < elements.size(); j++) {
0602: addHttpParams(sUrl, sCurrentFragment,
0603: (Element) elements.elementAt(j));
0604: }
0605: }
0606: }
0607:
0608: /**
0609: * Sets the <code>PublishFilter</code> for this state.
0610: *
0611: * @param filter
0612: *
0613: * @see PublishFilter
0614: */
0615: public void setPublishFilter(PublishFilter filter) {
0616: m_publishFilter = filter;
0617: }
0618:
0619: /**
0620: * Returns the <code>PublishFilter</code> for this state.
0621: *
0622: * @return
0623: *
0624: * @see PublishFilter
0625: */
0626: public PublishFilter getPublishFilter() {
0627: return m_publishFilter;
0628: }
0629:
0630: /**
0631: * Returns the headers included in the HTTP request.
0632: *
0633: * @return
0634: */
0635: public Map getHeaders() {
0636: return m_requestHeaders;
0637: }
0638:
0639: /**
0640: * Returns the remote address associated with the HTTP request.
0641: *
0642: * @return
0643: */
0644: public String getRemoteAddress() {
0645: return m_requestRemoteAddress;
0646: }
0647:
0648: /**
0649: * Populates this object from the given <code>HttpRequestManager</code>.
0650: *
0651: * @param req_man
0652: * @throws StateException
0653: */
0654: public void populate(HttpRequestManager req_man)
0655: throws StateException {
0656: if (m_bIsPopulated == true) {
0657: throw new StateException("Already populated!!");
0658: }
0659:
0660: //hold on to request headers
0661: m_requestHeaders = req_man.getHeaders();
0662:
0663: //hold on to remote address
0664: m_requestRemoteAddress = req_man.getRemoteAddress();
0665:
0666: try {
0667: if (req_man.hasUploadedFiles() == true) {
0668: if (req_man.isContentBinary() == true) {
0669: addFileContents(req_man.getUploadedFile());
0670: } else {
0671:
0672: addStringContents(req_man.getContentAsString(true));
0673: }
0674: }
0675:
0676: if (req_man.containsXMLFeed() == true) {
0677: addStringContents(req_man.getContentAsString(true));
0678: }
0679: } catch (IOException e) {
0680: throw new StateException(
0681: "Error accessing file or string content from request",
0682: e);
0683: }
0684:
0685: Element root = createElement(TAG_STATE);
0686: appendChild(root);
0687:
0688: Enumeration attribs = req_man.getParameterNames();
0689: ArrayList tokanizedName = new ArrayList(89);
0690:
0691: String sParamName = "";
0692: String sTokElem = "";
0693:
0694: Vector vAttNames = new Vector();
0695: Vector vAttVals = new Vector();
0696:
0697: String sTempAttName = "";
0698: String sTempAttVal = "";
0699: String sTempNodeName = "";
0700:
0701: boolean isAttributeValue = false;
0702:
0703: Element appendNode = null;
0704: Element currentNode = null;
0705: Element tempPropVal = null;
0706: Element tempPropAV = null;
0707: Element tempPropDesc = null;
0708: Text textNode = null;
0709:
0710: while (attribs.hasMoreElements()) {
0711: appendNode = root;
0712: isAttributeValue = false;
0713:
0714: sParamName = ((String) attribs.nextElement()).trim();
0715:
0716: if (ignoreTags.contains(sParamName)
0717: || (sParamName.indexOf("ignore") >= 0)) {
0718: continue;
0719: }
0720:
0721: StringTokenizer sTok = new StringTokenizer(sParamName, "/");
0722:
0723: while (sTok.hasMoreTokens()) {
0724: vAttNames.removeAllElements();
0725: vAttVals.removeAllElements();
0726:
0727: sTempNodeName = "";
0728: sTempAttName = "";
0729: sTempAttVal = "";
0730:
0731: sTokElem = sTok.nextToken().trim();
0732: sTempNodeName = sTokElem;
0733:
0734: if (sTokElem.indexOf("[") > -1) {
0735: try {
0736: sTempNodeName = sTokElem.substring(0, sTokElem
0737: .indexOf("["));
0738: boolean bEqualsSign = true;
0739: if (sTokElem.indexOf("=") == -1) {
0740: bEqualsSign = false;
0741: sTempAttName = sTokElem.substring(sTokElem
0742: .indexOf("@") + 1, sTokElem
0743: .indexOf("_eq_"));
0744: } else {
0745: sTempAttName = sTokElem.substring(sTokElem
0746: .indexOf("@") + 1, sTokElem
0747: .indexOf("="));
0748: }
0749: vAttNames.add(sTempAttName);
0750:
0751: if (sTokElem.indexOf("]") == -1) {
0752: if (bEqualsSign) {
0753: sTempAttVal = sTokElem
0754: .substring(sTokElem
0755: .indexOf("=") + 1)
0756: + "/";
0757: } else {
0758: sTempAttVal = sTokElem
0759: .substring(sTokElem
0760: .indexOf("_eq_") + 4)
0761: + "/";
0762: }
0763: sTokElem = sTok.nextToken().trim();
0764:
0765: while (sTok.hasMoreTokens()
0766: && (sTokElem.indexOf("]") == -1)) {
0767: sTempAttVal = sTempAttVal + sTokElem
0768: + "/";
0769: sTokElem = sTok.nextToken().trim();
0770: }
0771:
0772: sTempAttVal = sTempAttVal
0773: + sTokElem.substring(0, sTokElem
0774: .indexOf("]"));
0775: } else {
0776: if (bEqualsSign) {
0777: sTempAttVal = sTokElem.substring(
0778: sTokElem.indexOf("=") + 1,
0779: sTokElem.indexOf("]"));
0780: } else {
0781: sTempAttVal = sTokElem.substring(
0782: sTokElem.indexOf("_eq_") + 4,
0783: sTokElem.indexOf("]"));
0784: }
0785: }
0786:
0787: vAttVals.add(sTempAttVal);
0788: } catch (Exception e) {
0789: throw new StateException("token=" + sTokElem);
0790: }
0791:
0792: currentNode = findNode(appendNode, sTempNodeName,
0793: vAttNames, vAttVals);
0794:
0795: if (currentNode == null) {
0796: currentNode = createNode(appendNode,
0797: sTempNodeName, vAttNames, vAttVals);
0798: }
0799: } else if (sTokElem.indexOf("@") > -1) {
0800: isAttributeValue = true;
0801: } else {
0802:
0803: currentNode = findNode(appendNode, sTokElem,
0804: vAttNames, vAttVals);
0805:
0806: if (currentNode == null) {
0807: currentNode = createNode(appendNode, sTokElem,
0808: vAttNames, vAttVals);
0809: }
0810: }
0811:
0812: appendNode = currentNode;
0813: }
0814:
0815: if (isAttributeValue) {
0816: if (req_man.getParameterValues(sParamName).length > 1) {
0817: String sTagName = currentNode.getNodeName();
0818: Element tempElem = null;
0819:
0820: for (int i = 1; i < req_man
0821: .getParameterValues(sParamName).length; i++) {
0822: tempElem = createElement(sTagName);
0823: tempElem
0824: .setAttribute(
0825: sTokElem.substring(1),
0826: req_man
0827: .getParameterValues(sParamName)[i]);
0828: currentNode.getParentNode().appendChild(
0829: tempElem);
0830: }
0831:
0832: ((Element) currentNode).setAttribute(sTokElem
0833: .substring(1), req_man
0834: .getParameterValues(sParamName)[0]);
0835: } else {
0836: ((Element) currentNode).setAttribute(sTokElem
0837: .substring(1), req_man
0838: .getParameterValues(sParamName)[0]);
0839: }
0840:
0841: } else {
0842: if (((Element) currentNode)
0843: .getTagName()
0844: .equalsIgnoreCase(
0845: AbstractPropertyInstance.TAG_PROPERTYINSTANCE)) {
0846: if (req_man.getParameterValues(sParamName).length > 1) {
0847: for (int i = 0; i < req_man
0848: .getParameterValues(sParamName).length; i++) {
0849: if (req_man.getParameterValues(sParamName)[i]
0850: .equals("XXXX") == false) {
0851:
0852: tempPropAV = createElement(AbstractPropertyInstance.TAG_AVAILABLEOPTIONS);
0853: tempPropAV
0854: .setAttribute("selected", "1");
0855: tempPropVal = createElement(Value.TAG_VALUE);
0856: textNode = createTextNode(req_man
0857: .getParameterValues(sParamName)[i]);
0858:
0859: tempPropVal.appendChild(textNode);
0860: tempPropAV.appendChild(tempPropVal);
0861: currentNode.appendChild(tempPropAV);
0862: }
0863: }
0864: } else {
0865: if (req_man.getParameterValues(sParamName)[0]
0866: .equals("XXXX") == false) {
0867:
0868: tempPropVal = createElement(Profile.TAG_PROPERTY_VALUE);
0869: textNode = createTextNode(req_man
0870: .getParameterValues(sParamName)[0]);
0871: tempPropVal.appendChild(textNode);
0872: currentNode.appendChild(tempPropVal);
0873: }
0874: }
0875: } else {
0876:
0877: String sFragment = req_man
0878: .getParameterValues(sParamName)[0];
0879:
0880: if ((sFragment != null) && (sFragment.length() > 0)) {
0881: XMLFragmentParser parser = new XMLFragmentParser(
0882: sFragment);
0883: try {
0884: parser.parse(currentNode);
0885: } catch (ParseException e) {
0886: throw new StateException(
0887: "Error occured parsing fragment", e);
0888: }
0889: } else {
0890: textNode = createTextNode(sFragment);
0891: currentNode.appendChild(textNode);
0892: }
0893:
0894: }
0895: }
0896: }
0897:
0898: String sReferer = (String) req_man.getHeaders().get("Referer");
0899: String sRefererId = "";
0900:
0901: if (sReferer != null) {
0902: int nStart = sReferer.indexOf("Page/@id=") + 9;
0903: int nEnd = sReferer.indexOf('&', nStart);
0904:
0905: if (nEnd < 0) {
0906: sRefererId = sReferer.substring(nStart);
0907: } else {
0908: sRefererId = sReferer.substring(nStart, nEnd);
0909: }
0910: } else {
0911: try {
0912: sRefererId = ConfigSettings.getProperty(
0913: PNAME_DEFAULT_PAGE, "1");
0914: } catch (ConfigException e) {
0915: throw new StateException(
0916: "Error occured getting default page number", e);
0917: }
0918: }
0919:
0920: Element elReferer = createElement(TAG_REFERER);
0921: Element elPage = createElement(WebPage.TAG_PAGE);
0922:
0923: elPage.setAttribute(AbstractObject.ATTRIB_ID, sRefererId);
0924: elReferer.appendChild(elPage);
0925: getDocumentElement().appendChild(elReferer);
0926:
0927: m_bIsPopulated = true;
0928: }
0929:
0930: /*--------------------------------------------------------------------------
0931:
0932: Private methods
0933: -------------------------------------------------------------------------*/
0934:
0935: /**
0936: * initialises the default user details.
0937: */
0938: private void setBlankUserDetails() throws StateException {
0939: m_User = new User(m_dsi);
0940:
0941: m_UserProf = new Profile(m_dsi);
0942:
0943: try {
0944: m_User = (User) HarmoniseObjectFactory
0945: .instantiatePublishableObject(m_dsi, User.class
0946: .getName(), "/root/public/guest");
0947: } catch (HarmoniseFactoryException e) {
0948: throw new StateException(
0949: "Error getting default user from Database", e);
0950: }
0951: }
0952:
0953: /**
0954: * Returns an <code>AbstractObject</code> which matches the given element name and
0955: * is represented in the state.
0956: *
0957: * @param sClassname
0958: * @return
0959: * @throws Exception
0960: */
0961: private AbstractObject getHarmoniseObject(String sTagName)
0962: throws StateException {
0963: AbstractObject obj = null;
0964: Element stateElement = findElement(createElement(sTagName));
0965:
0966: if (stateElement != null) {
0967: try {
0968: obj = HarmoniseObjectFactory
0969: .instantiateHarmoniseObject(m_dsi,
0970: stateElement, this );
0971: } catch (HarmoniseFactoryException e) {
0972: throw new StateException(
0973: "Error occured getting object from factory", e);
0974: }
0975: }
0976:
0977: return obj;
0978: }
0979:
0980: /**
0981: * Finds node under <code>appendNode</code> which matches <code>sNodeName</code>. Returns
0982: * <code>null</code> if there's no match.
0983: *
0984: * @param appendNode
0985: * @param sNodeName
0986: * @param vAttNames
0987: * @param vAttVals
0988: * @return
0989: */
0990: private Element findNode(Element appendNode, String sNodeName,
0991: Vector vAttNames, Vector vAttVals) {
0992: Element foundNode = null;
0993: boolean bAttsFound = true;
0994:
0995: NodeList nlNodes = appendNode.getChildNodes();
0996:
0997: if (vAttNames.size() > 0) {
0998: for (int i = 0; i < nlNodes.getLength(); i++) {
0999: if ((nlNodes.item(i).getNodeType() == Node.ELEMENT_NODE)
1000: && nlNodes.item(i).getNodeName().equals(
1001: sNodeName)) {
1002: bAttsFound = true;
1003:
1004: Element tempElem = (Element) nlNodes.item(i);
1005:
1006: for (int j = 0; j < vAttNames.size(); j++) {
1007: if (!tempElem.getAttribute(
1008: (String) vAttNames.elementAt(j))
1009: .equals((String) vAttVals.elementAt(j))) {
1010: bAttsFound = false;
1011: }
1012: }
1013:
1014: if (bAttsFound) {
1015: foundNode = (Element) nlNodes.item(i);
1016: }
1017: }
1018: }
1019: } else {
1020: if (nlNodes.getLength() > 0) {
1021: for (int j = 0; j < nlNodes.getLength(); j++) {
1022: if ((nlNodes.item(j).getNodeType() == Node.ELEMENT_NODE)
1023: && nlNodes.item(j).getNodeName().equals(
1024: sNodeName)) {
1025: foundNode = (Element) nlNodes.item(j);
1026: }
1027: }
1028: }
1029: }
1030:
1031: return foundNode;
1032: }
1033:
1034: /**
1035: * Creates an <code>Element</code> with the name <code>sNodeName</code> and atttribute
1036: * values taken from the vectors <code>vAttNames</code> and <code>vAttVals</code>.
1037: *
1038: * @param appendNode
1039: * @param sNodeName
1040: * @param vAttNames
1041: * @param vAttVals
1042: * @return
1043: */
1044: private Element createNode(Element appendNode, String sNodeName,
1045: Vector vAttNames, Vector vAttVals) {
1046: Element newNode = createElement(sNodeName);
1047:
1048: if (vAttNames.size() > 0) {
1049: for (int i = 0; i < vAttNames.size(); i++) {
1050: newNode.setAttribute((String) vAttNames.elementAt(i),
1051: (String) vAttVals.elementAt(i));
1052:
1053: }
1054: }
1055:
1056: appendNode.appendChild(newNode);
1057:
1058: return newNode;
1059: }
1060:
1061: /**
1062: * Sets the session id associated to this object.
1063: *
1064: * @param sessionId the session id
1065: */
1066: public void setSessionId(String sessionId) {
1067: NodeList xnlSession = getElementsByTagName(Session.TAG_SESSION);
1068:
1069: Element sessEl = null;
1070:
1071: if (xnlSession.getLength() > 0) {
1072: sessEl = ((Element) xnlSession.item(0));
1073: } else {
1074: sessEl = createElement(Session.TAG_SESSION);
1075: Element rootEl = getDocumentElement();
1076:
1077: if (rootEl == null) {
1078: rootEl = createElement(TAG_STATE);
1079: appendChild(rootEl);
1080: }
1081:
1082: rootEl.appendChild(sessEl);
1083: }
1084:
1085: sessEl.setAttribute(AbstractObject.ATTRIB_ID, sessionId);
1086:
1087: }
1088: }
|