0001: /*
0002: * JFolder, Copyright 2001-2006 Gary Steinmetz
0003: *
0004: * Distributable under LGPL license.
0005: * See terms of license at gnu.org.
0006: */
0007:
0008: package org.jfolder.common.tagging;
0009:
0010: //base classes
0011: import java.io.ByteArrayInputStream;
0012: import java.io.ByteArrayOutputStream;
0013: import java.io.IOException;
0014: import java.io.InputStream;
0015: import java.io.StringWriter;
0016: import java.util.ArrayList;
0017: import java.util.HashMap;
0018: import java.util.HashSet;
0019: import java.util.Iterator;
0020: import java.util.StringTokenizer;
0021: import javax.xml.parsers.ParserConfigurationException; //import javax.xml.soap.AttachmentPart;
0022: //import javax.xml.soap.MessageFactory;
0023: //import javax.xml.soap.Name;
0024: //import javax.xml.soap.SOAPBody;
0025: //import javax.xml.soap.SOAPBodyElement;
0026: //import javax.xml.soap.SOAPConnection;
0027: //import javax.xml.soap.SOAPConnectionFactory;
0028: //import javax.xml.soap.SOAPElement;
0029: //import javax.xml.soap.SOAPException;
0030: //import javax.xml.soap.SOAPFactory;
0031: //import javax.xml.soap.SOAPMessage;
0032: import org.w3c.dom.Attr;
0033: import org.w3c.dom.Document;
0034: import org.w3c.dom.Element;
0035: import org.w3c.dom.Node;
0036: import org.w3c.dom.NodeList;
0037: import org.w3c.dom.Text;
0038: import org.xml.sax.SAXException;
0039:
0040: //project specific classes
0041: import org.jfolder.common.UnexpectedSystemException;
0042: import org.jfolder.common.files.BaseVirtualFileSystemHolder;
0043: import org.jfolder.common.files.VirtualFileSystemDirectory;
0044: import org.jfolder.common.files.VirtualFileSystemFile;
0045: import org.jfolder.common.files.VirtualFileSystemHolder;
0046: import org.jfolder.common.files.VirtualFileSystemRoot;
0047: import org.jfolder.common.files.memory.MemoryFileSystemRootHelper;
0048: import org.jfolder.common.utils.misc.MiscHelper;
0049: import org.jfolder.common.utils.xml.XMLHelper;
0050:
0051: //other classes
0052:
0053: public class RootConceptTagHolder {
0054:
0055: //
0056: protected final static String DIR_PREFIX = "0_";
0057: protected final static String FILE_PREFIX = "1_";
0058: //
0059: private final static String DOCS = "docs";
0060: private final static String RCTH = "rcth";
0061: private final static String LOCAL_FILES = "local_files";
0062:
0063: //
0064: private boolean errorPresent = false;
0065: private Throwable errorSource = null;
0066: private ConceptTagPreferences ctp = null;
0067: //
0068: private ConceptTagConstraintHolder rootCtconh = null;
0069: private ConceptTagCharacteristicHolder rootCtcharh = null;
0070: //
0071: private HashMap archivedRcth = null;
0072: //
0073: private VirtualFileSystemRoot vsr = null;
0074:
0075: protected RootConceptTagHolder(ConceptTagPreferences inCtp) {
0076:
0077: VirtualFileSystemRoot localVsr = null;
0078: if (inCtp.isFileSystemPresent()) {
0079: //
0080: localVsr = MemoryFileSystemRootHelper.newInstance();
0081: VirtualFileSystemHolder docsVsh = localVsr
0082: .createDirectory(DOCS);
0083: VirtualFileSystemDirectory localFilesVsh = docsVsh
0084: .createDirectory(LOCAL_FILES);
0085: }
0086: init(inCtp, localVsr);
0087: }
0088:
0089: protected RootConceptTagHolder(ConceptTagPreferences inCtp,
0090: VirtualFileSystemRoot inVsr) {
0091: //
0092: init(inCtp, inVsr);
0093: }
0094:
0095: private void init(ConceptTagPreferences inCtp,
0096: VirtualFileSystemRoot inVsr) {
0097: //
0098: this .ctp = inCtp;
0099: this .ctp.setRootHolder(this );
0100: if (this .ctp.isFileSystemPresent()) {
0101: this .vsr = inVsr;
0102: this .archivedRcth = new HashMap();
0103: }
0104: }
0105:
0106: protected VirtualFileSystemDirectory getLocalFilesDirectory() {
0107: return this .vsr.getDirectory(DOCS).getDirectory(LOCAL_FILES);
0108: }
0109:
0110: //
0111: /*
0112: public void exportDocuments(SOAPFactory inSf, SOAPMessage inMessage,
0113: SOAPElement inElement) throws SOAPException {
0114: //
0115: if (this.ctp.isFileSystemPresent()) {
0116: exportDocuments(inSf, inMessage, inElement, this.vsr,
0117: VirtualFileSystemHolder.DIRECTORY_SEPARATOR);
0118: }
0119: }
0120: private void exportDocuments(
0121: SOAPFactory inSf, SOAPMessage inMessage, SOAPElement inElement,
0122: VirtualFileSystemHolder inCurrentDir, String inCurrentName)
0123: throws SOAPException {
0124: //
0125: //add files to root here
0126: VirtualFileSystemHolder docsVsh = inCurrentDir.createDirectory(DOCS);
0127: VirtualFileSystemHolder rcthVsh = docsVsh.createDirectory(RCTH);
0128: ArrayList rcthNamespaces = getRootHolderNamespaceList();
0129: for (int i = 0; i < rcthNamespaces.size(); i++) {
0130: String nextRcthNamespace = (String)rcthNamespaces.get(i);
0131: ArrayList nextRcthList = getRootHolderList(nextRcthNamespace);
0132: //
0133: VirtualFileSystemHolder nextRcthDir = rcthVsh.createDirectory(
0134: nextRcthNamespace);
0135: //
0136: for (int j = 0; j < nextRcthList.size(); j++) {
0137: RootConceptTagHolder nextRcth =
0138: (RootConceptTagHolder)nextRcthList.get(j);
0139: //
0140: byte nextContent[] = ConceptRollupTag.getRollupFromList(
0141: nextRcth, null);
0142: //
0143: nextRcthDir.createFile(j + ".zip", nextContent);
0144: }
0145: }
0146: //
0147: VirtualFileSystemDirectory localFilesVsh =
0148: docsVsh.createDirectory(LOCAL_FILES);
0149: BaseVirtualFileSystemHolder.copyDirectory(
0150: getLocalFilesDirectory(), localFilesVsh);
0151: //
0152: //
0153: ArrayList localDirs = inCurrentDir.getLocalDirectories();
0154: for (int i = 0; i < localDirs.size(); i++) {
0155: VirtualFileSystemDirectory nextVsh =
0156: (VirtualFileSystemDirectory)localDirs.get(i);
0157: Name nextVshName = inSf.createName(nextVsh.getName());
0158: SOAPElement nextVshEle = inElement.addChildElement(nextVshName);
0159: //
0160: String dirName = inCurrentName
0161: + VirtualFileSystemHolder.DIRECTORY_SEPARATOR
0162: + nextVsh.getName();
0163: //
0164: exportDocuments(inSf, inMessage, nextVshEle, nextVsh, dirName);
0165: }
0166: //
0167: ArrayList localFiles = inCurrentDir.getLocalFiles();
0168: for (int i = 0; i < localFiles.size(); i++) {
0169: VirtualFileSystemFile nextVsf =
0170: (VirtualFileSystemFile)localFiles.get(i);
0171: //
0172: String fileName = inCurrentName
0173: + VirtualFileSystemHolder.DIRECTORY_SEPARATOR
0174: + nextVsf.getName();
0175: //
0176: ByteArrayInputStream nextFileContent =
0177: new ByteArrayInputStream(nextVsf.getContent());
0178: //
0179: AttachmentPart nextFile = inMessage.createAttachmentPart();
0180: nextFile.setContent(nextFileContent, "application/octet-stream");
0181: nextFile.setContentId(fileName);
0182: inMessage.addAttachmentPart(nextFile);
0183: }
0184: }
0185: //
0186: public void importDocuments(SOAPFactory inSf, SOAPMessage inMessage,
0187: SOAPElement inElement) throws SOAPException, IOException {
0188: //
0189: if (this.ctp.isFileSystemPresent()) {
0190: importDocuments(inSf, inMessage, inElement, this.vsr);
0191:
0192: Iterator iter = inMessage.getAttachments();
0193: while (iter.hasNext()) {
0194: AttachmentPart nextPart = (AttachmentPart)iter.next();
0195: String fullName = nextPart.getContentId();
0196: //
0197: ArrayList fileList =
0198: BaseVirtualFileSystemHolder.parseDirectoryName(fullName);
0199: String fileName = (String)fileList.remove(fileList.size() - 1);
0200: //
0201: VirtualFileSystemHolder currDir =
0202: this.vsr.getOrCreateAbsoluteDirectory(
0203: BaseVirtualFileSystemHolder.parseDirectoryArray(
0204: fileList));
0205: //
0206: InputStream nextIs = (InputStream)nextPart.getContent();
0207: ByteArrayOutputStream baos = new ByteArrayOutputStream();
0208: //
0209: byte buffer[] = new byte[1024];
0210: int size = 0;
0211: while ((size = nextIs.read(buffer, 0, buffer.length)) != -1) {
0212: baos.write(buffer, 0, size);
0213: }
0214: //
0215: currDir.createFile(fileName, baos.toByteArray());
0216: }
0217: //
0218: //get files from root here
0219: VirtualFileSystemHolder docsVsh = this.vsr.getDirectory(DOCS);
0220: VirtualFileSystemHolder rcthVsh = docsVsh.getDirectory(RCTH);
0221: ArrayList rcthSubDirs = rcthVsh.getLocalDirectories();
0222: for (int i = 0; i < rcthSubDirs.size(); i++) {
0223: VirtualFileSystemDirectory nextDir =
0224: (VirtualFileSystemDirectory)rcthSubDirs.get(i);
0225: ArrayList nextFiles = nextDir.getLocalFiles();
0226: for (int j = 0; j < nextFiles.size(); j++) {
0227: VirtualFileSystemFile nextFile =
0228: (VirtualFileSystemFile)nextFiles.get(j);
0229: byte nextRcthContent[] = nextFile.getContent();
0230: RootConceptTagHolder nextRcth =
0231: ConceptRollupTag.getRootHolderFromList(nextRcthContent);
0232: //
0233: addRootHolder(nextRcth);
0234: }
0235: }
0236: //
0237: //
0238: VirtualFileSystemDirectory localFilesVsh =
0239: docsVsh.getDirectory(LOCAL_FILES);
0240: importFiles(localFilesVsh, new ArrayList());
0241: }
0242: }
0243: private void importDocuments(
0244: SOAPFactory inSf, SOAPMessage inMessage, SOAPElement inElement,
0245: VirtualFileSystemHolder inCurrentDir) throws SOAPException {
0246: //
0247: Iterator iter = inElement.getChildElements();
0248: while (iter.hasNext()) {
0249: Object o = iter.next();
0250: if (o instanceof SOAPElement) {
0251: SOAPElement nextElement = (SOAPElement)o;
0252: VirtualFileSystemHolder nextDir = inCurrentDir.createDirectory(
0253: nextElement.getLocalName());
0254: importDocuments(inSf, inMessage, nextElement, nextDir);
0255: }
0256: }
0257: }
0258: private void importFiles(
0259: VirtualFileSystemDirectory inCurrentDir, ArrayList inCurrentList) {
0260: //
0261: ArrayList localDirs = inCurrentDir.getLocalDirectories();
0262: for (int i = 0; i < localDirs.size(); i++) {
0263: VirtualFileSystemDirectory nextDir =
0264: (VirtualFileSystemDirectory)localDirs.get(i);
0265: //
0266: String nextDirName =
0267: nextDir.getName().substring(DIR_PREFIX.length());
0268: //
0269: inCurrentList.add(nextDirName);
0270: importFiles(inCurrentDir, inCurrentList);
0271: inCurrentList.remove(inCurrentList.size() - 1);
0272: //
0273: }
0274: //
0275: ArrayList localFiles = inCurrentDir.getLocalFiles();
0276: for (int i = 0; i < localFiles.size(); i++) {
0277: VirtualFileSystemFile nextFile =
0278: (VirtualFileSystemFile)localFiles.get(i);
0279: String nextFileName =
0280: nextFile.getName().substring(FILE_PREFIX.length());
0281: //
0282: VirtualFileSystemHolder nextLocalDirectory =
0283: getLocalFilesDirectory();
0284: for (int j = 0; j < inCurrentList.size(); j++) {
0285: nextLocalDirectory = nextLocalDirectory.getDirectory(
0286: DIR_PREFIX + inCurrentList.get(j));
0287: }
0288: nextLocalDirectory.createFile(
0289: FILE_PREFIX + nextFileName, nextFile.getContent());
0290: //addLocalFile(inCurrentList, nextFileName, nextFile.getContent());
0291: }
0292: }
0293: */
0294:
0295: //
0296: public ConceptTagPreferences getPreferences() {
0297: return this .ctp;
0298: }
0299:
0300: //
0301: protected boolean isFileSystemPresent() {
0302: return this .ctp.isFileSystemPresent();
0303: }
0304:
0305: private void validateFileSystemPresent() {
0306: if (!isFileSystemPresent()) {
0307: throw new UnexpectedSystemException(
0308: "File system not present");
0309: }
0310: }
0311:
0312: //
0313: protected boolean isRootHolderPresent(RootConceptTagHolder inRcth) {
0314: //
0315: boolean outValue = false;
0316:
0317: validateFileSystemPresent();
0318:
0319: String namespace = inRcth.getPreferences()
0320: .getPreferencesHandle();
0321:
0322: if (this .archivedRcth.containsKey(namespace)) {
0323: ArrayList hs = (ArrayList) this .archivedRcth.get(namespace);
0324: if (hs.contains(inRcth)) {
0325: outValue = true;
0326: }
0327: }
0328:
0329: return outValue;
0330: }
0331:
0332: protected void addRootHolder(RootConceptTagHolder inRcth) {
0333: //
0334: validateFileSystemPresent();
0335: String namespace = inRcth.getPreferences()
0336: .getPreferencesHandle();
0337: if (isRootHolderPresent(inRcth)) {
0338: throw new UnexpectedSystemException(
0339: "Root holder already registered");
0340: } else {
0341: ArrayList hs = null;
0342: if (this .archivedRcth.containsKey(namespace)) {
0343: hs = (ArrayList) this .archivedRcth.get(namespace);
0344: } else {
0345: hs = new ArrayList();
0346: this .archivedRcth.put(namespace, hs);
0347: }
0348: hs.add(inRcth);
0349: }
0350: }
0351:
0352: protected int getRootHolderIndex(RootConceptTagHolder inRcth) {
0353: //
0354: int outValue = 0;
0355:
0356: validateFileSystemPresent();
0357: String namespace = inRcth.getPreferences()
0358: .getPreferencesHandle();
0359: if (isRootHolderPresent(inRcth)) {
0360: //
0361: ArrayList hs = (ArrayList) this .archivedRcth.get(namespace);
0362: outValue = hs.indexOf(inRcth);
0363: } else {
0364: throw new UnexpectedSystemException(
0365: "Root holder not registered");
0366: }
0367:
0368: return outValue;
0369: }
0370:
0371: protected ArrayList getRootHolderNamespaceList() {
0372:
0373: ArrayList outValue = new ArrayList();
0374:
0375: validateFileSystemPresent();
0376: Iterator iter = this .archivedRcth.keySet().iterator();
0377: while (iter.hasNext()) {
0378: outValue.add(iter.next());
0379: }
0380:
0381: return outValue;
0382: }
0383:
0384: protected ArrayList getRootHolderList(String inNamespace) {
0385:
0386: ArrayList outValue = null;
0387:
0388: validateFileSystemPresent();
0389: if (this .archivedRcth.containsKey(inNamespace)) {
0390: ArrayList hs = (ArrayList) this .archivedRcth
0391: .get(inNamespace);
0392: outValue = (ArrayList) hs.clone();
0393: } else {
0394: outValue = new ArrayList();
0395: }
0396:
0397: return outValue;
0398: }
0399:
0400: //
0401:
0402: public ConceptTagCharacteristicHolder getRootCharacteristicHolder() {
0403: return this .rootCtcharh;
0404: }
0405:
0406: public ConceptTagCharacteristic getFirstCharacteristic() {
0407:
0408: return getFirstConstraint().getCharacteristic();
0409: }
0410:
0411: public ConceptTagConstraint getFirstConstraint() {
0412:
0413: ConceptTagConstraint outValue = null;
0414:
0415: //ConceptTagCharacteristicHolder localRootCtcharh =
0416: // getRootCharacteristicHolder();
0417: String rootConstraintSymbol = getPreferences()
0418: .getRootConstraint();
0419: //
0420: if (!rootConstraintSymbol.equals(getPreferences()
0421: .getUnconstrainedSymbol())) {
0422: //
0423: outValue = this .rootCtconh
0424: .getConstraint(rootConstraintSymbol);
0425: } else {
0426: outValue = this .rootCtconh.getUndefinedConstraint();
0427: }
0428:
0429: return outValue;
0430: }
0431:
0432: public ConceptTagCharacteristicHolder getFirstChildCharacteristicHolder() {
0433:
0434: ConceptTagCharacteristicHolder outValue = null;
0435:
0436: ConceptTagCharacteristic childCtchar = getFirstCharacteristic();
0437: outValue = childCtchar.getValueAsHolder(0, null);
0438:
0439: return outValue;
0440: }
0441:
0442: public ConceptTag getFirstChildConceptTag() {
0443:
0444: ConceptTag outValue = null;
0445:
0446: ConceptTagCharacteristicHolder ctcharh = getFirstChildCharacteristicHolder();
0447: outValue = ctcharh.getConceptTag();
0448:
0449: return outValue;
0450: }
0451:
0452: protected void setRootCharacteristicHolder(
0453: ConceptTagCharacteristicHolder inCtcharh) {
0454: this .rootCtcharh = inCtcharh;
0455: }
0456:
0457: public ConceptTagConstraintHolder getRootConstraintHolder() {
0458: return this .rootCtconh;
0459: }
0460:
0461: //protected void setConstraintHolder(ConceptTagConstraintHolder inCtchol) {
0462: // this.rootCtconh = inCtchol;
0463: //}
0464:
0465: //
0466: public boolean isErrorPresent() {
0467: return this .errorPresent;
0468: }
0469:
0470: public Throwable getErrorSource() {
0471: return this .errorSource;
0472: }
0473:
0474: protected void setErrorPresent(boolean inErrorPresent,
0475: Throwable inError) {
0476: //MiscHelper.println("<!-- REGISTERING ERROR -->");
0477: //(new Exception()).printStackTrace();
0478: //MiscHelper.println("<!----------------------->");
0479: this .errorPresent = inErrorPresent;
0480: this .errorSource = inError;
0481: }
0482:
0483: //
0484: public void validate() {
0485:
0486: InitializeConceptTagContext ictc = this .ctp
0487: .getInitializeContext();
0488: //this.rootCtconh.validate(ictc);
0489: //ictc.nonImmediateValidation();
0490: //ictc.reset();
0491: validate(ictc);
0492: }
0493:
0494: public void validate(InitializeConceptTagContext inIctc) {
0495:
0496: try {
0497: //InitializeConceptTagContext ictc =
0498: // this.ctp.getInitializeContext();
0499: //
0500: //
0501: //
0502: //
0503: ///////////////////////////////////////////////////////
0504:
0505: String rootClassName = this .ctp.getRootClassName();
0506: String rootCon = this .ctp.getRootConstraint();
0507: String unconSym = this .ctp.getUnconstrainedSymbol();
0508:
0509: ConceptTagConstraintHolder rootCtconh = ConceptTagConstraintHolder
0510: .newInstance(
0511: //ctp.getFlags(),
0512: this .ctp, this .rootCtcharh, null);
0513:
0514: //
0515: ConceptTagConstraint ctconMain = null;
0516: DynamicConceptTagConstraintContext ctconMainDctcc = null;
0517: //
0518: if (this .ctp.isUnconstrainedAllowed()
0519: && rootCon.equals(unconSym)) {
0520:
0521: ctconMain = rootCtconh.getUndefinedConstraint();
0522: ctconMain.setMinimumLength(1);
0523: ctconMain.setMaximumLength(1);
0524: if (!ctconMain.isDynamicContextPresent()) {
0525: DynamicConceptTagConstraintContext
0526: .newInstance(ctconMain);
0527: }
0528: ctconMainDctcc = ctconMain
0529: .getDynamicConstraintContext();
0530: } else {
0531: ctconMain = ConceptTagConstraint.newInstance(
0532: //rootCtconh.getFlags(),
0533: rootCon, rootCtconh);
0534: ctconMainDctcc = DynamicConceptTagConstraintContext
0535: .newInstance(ctconMain);
0536: }
0537: Class rootClass = Class.forName(rootClassName);
0538: ctconMainDctcc.getValidator().allowOnlyTheseClasses(
0539: rootClass, "Only root tags are allowed here");
0540: //ctconMainDctcc.setClassRestrictions(rootClass);
0541: ctconMainDctcc.addDefaultValue(rootClass);
0542:
0543: this .rootCtconh = rootCtconh;
0544: /////////////////////////////////////////////////////////////
0545: //
0546: //
0547: //
0548: //
0549: //
0550: //
0551: //
0552: //
0553: //
0554: //
0555: //
0556: //
0557: //
0558: //
0559: //
0560: //
0561: //
0562: //
0563: this .rootCtconh.validate(inIctc);
0564: inIctc.nonImmediateValidation();
0565: inIctc.reset();
0566: } catch (ClassNotFoundException cnfe) {
0567: MiscHelper.writeMessage(ConceptTagHelper.class, cnfe);
0568: this .setErrorPresent(true, cnfe);
0569: }
0570:
0571: }
0572:
0573: public String getXMLRepButReplaceRootElement(String inNamespace,
0574: String inNsPrefix, String inName, String inAttrsNs[],
0575: String inAttrsName[], String inAttrsPrefix[],
0576: String inAttrsValue[]) {
0577: //
0578: try {
0579:
0580: StringWriter outValue = new StringWriter();
0581:
0582: HashMap currNsMap = new HashMap();
0583: Document currDocument = getXMLRepButReplaceRootElement(
0584: inNamespace, inNsPrefix, inName, inAttrsNs,
0585: inAttrsName, inAttrsPrefix, inAttrsValue, currNsMap);
0586: XMLHelper.writeDocument(currDocument, outValue, currNsMap,
0587: false);
0588:
0589: return outValue.toString();
0590: }
0591: //catch (ParserConfigurationException pce) {
0592: // throw new UnexpectedSystemException(pce);
0593: //}
0594: catch (IOException ioe) {
0595: throw new UnexpectedSystemException(ioe);
0596: }
0597: }
0598:
0599: public Document getXMLRepButReplaceRootElement(String inNamespace,
0600: String inNsPrefix, String inName, String inAttrsNs[],
0601: String inAttrsName[], String inAttrsPrefix[],
0602: String inAttrsValue[], HashMap inNsMapping) {
0603:
0604: try {
0605:
0606: //StringWriter outValue = new StringWriter();
0607:
0608: Document outValue = XMLHelper.createBlankDocument();
0609: ;
0610:
0611: //
0612: Document origDocument = getXMLRepresentationAsDocument();
0613: Element origRootEle = origDocument.getDocumentElement();
0614:
0615: //
0616: //Document currDocument = XMLHelper.createBlankDocument();
0617: Element currRootEle = outValue.createElementNS(inNamespace,
0618: inName);
0619: outValue.appendChild(currRootEle);
0620:
0621: //
0622: for (int i = 0; i < inAttrsNs.length; i++) {
0623: String nextNs = inAttrsNs[i];
0624: String nextName = inAttrsName[i];
0625: String nextPrefix = inAttrsPrefix[i];
0626: String nextValue = inAttrsValue[i];
0627: //
0628: Attr nextAttr = outValue.createAttributeNS(nextNs,
0629: nextName);
0630: currRootEle.setAttributeNodeNS(nextAttr);
0631: nextAttr.setNodeValue(nextValue);
0632: //currRootEle.setAttributeNS(
0633: // nextNs, nextPrefix + ":" + nextName, nextValue);
0634: //nextRootAttr.setNodeValue(nextValue);
0635: //currRootEle.setAttributeNode(nextRootAttr);
0636: }
0637:
0638: //
0639: NodeList origNl = origRootEle.getChildNodes();
0640: for (int i = 0; i < origNl.getLength(); i++) {
0641: Node nextNode = outValue.importNode(origNl.item(i),
0642: true);
0643: currRootEle.appendChild(nextNode);
0644: }
0645:
0646: //
0647: HashMap origNsMap = this .ctp
0648: .getDefaultNamespaceAssignments();
0649: //HashMap currNsMap = (HashMap)origNsMap.clone();
0650: inNsMapping.putAll(origNsMap);
0651: if (inNamespace != null) {
0652: inNsMapping.put(inNamespace, inNsPrefix);
0653: }
0654: for (int i = 0; i < inAttrsNs.length; i++) {
0655: if (inAttrsNs[i] != null) {
0656: inNsMapping.put(inAttrsNs[i], inAttrsPrefix[i]);
0657: }
0658: }
0659:
0660: return outValue;
0661: } catch (ParserConfigurationException pce) {
0662: throw new UnexpectedSystemException(pce);
0663: }
0664: //catch (IOException ioe) {
0665: // throw new UnexpectedSystemException(ioe);
0666: //}
0667: }
0668:
0669: public String getXMLRepresentation() {
0670: return getXMLRepresentation(false);
0671: }
0672:
0673: public String getXMLRepresentation(boolean inIndent) {
0674:
0675: try {
0676: StringWriter outValue = new StringWriter();
0677:
0678: Document doc = getXMLRepresentationAsDocument();
0679:
0680: this .ctp.correctForSpecialCharactersAndElements(doc);
0681:
0682: XMLHelper.writeDocument(doc, outValue, this .ctp
0683: .getDefaultNamespaceAssignments(), inIndent);
0684:
0685: return outValue.toString();
0686: } catch (IOException ioe) {
0687: throw new UnexpectedSystemException(ioe);
0688: }
0689: }
0690:
0691: public Document getXMLRepresentationAsDocument() {
0692: try {
0693: Document outValue = XMLHelper.createBlankDocument();
0694:
0695: String rootConstraint = this .ctp.getRootConstraint();
0696:
0697: ConceptTagCharacteristic rootCtchar = null;
0698: if (!rootConstraint.equals(this .ctp
0699: .getUnconstrainedSymbol())) {
0700: rootCtchar = this .rootCtcharh
0701: .getCharacteristic(rootConstraint);
0702: } else {
0703: rootCtchar = this .rootCtcharh
0704: .getUndefinedCharacteristic();
0705: }
0706:
0707: getXMLRepresentation(rootCtchar, outValue, outValue);
0708:
0709: return outValue;
0710: } catch (ParserConfigurationException pce) {
0711: throw new UnexpectedSystemException(pce);
0712: }
0713: }
0714:
0715: private void getXMLRepresentation(
0716: ConceptTagCharacteristic inCtchar, Node inBase,
0717: Document inDoc) {
0718:
0719: String constraintName = inCtchar.getConstraintName();
0720:
0721: for (int i = 0; i < inCtchar.getValueLength(); i++) {
0722:
0723: if (inCtchar.isStatic(i, null)) {
0724: if (this .ctp.isStaticUsed()) {
0725:
0726: if (constraintName.equals(this .ctp
0727: .getUnconstrainedSymbol())) {
0728: Text textNode = inDoc.createTextNode(inCtchar
0729: .getValueAsString(i, null));
0730: inBase.appendChild(textNode);
0731: } else {
0732: Element childElement = null;
0733: if (this .ctp.getStaticNamespace() != null) {
0734: childElement = inDoc.createElementNS(
0735: this .ctp.getStaticNamespace(),
0736: this .ctp.getStaticName());
0737: } else {
0738: childElement = inDoc.createElement(this .ctp
0739: .getStaticName());
0740: }
0741: Text textNode = inDoc.createTextNode(inCtchar
0742: .getValueAsString(i, null));
0743: childElement.appendChild(textNode);
0744:
0745: appendNode(inDoc, inBase, childElement,
0746: constraintName);
0747: }
0748: } else {
0749: UnexpectedSystemException.unknownState();
0750: }
0751: } else if (inCtchar.isHolderWithConceptTag(i, null)) {
0752: Element childElement = null;
0753: ConceptTag childTag = inCtchar.getValueAsConceptTag(i,
0754: null);
0755: ConceptTagCharacteristicHolder childCtcharh = inCtchar
0756: .getValueAsHolder(i, null);
0757:
0758: ConceptTagSetHolder ctsh = this .ctp.getSetHolder();
0759: Class tagClass = childTag.getClass();
0760:
0761: String tagNs = null;
0762: String tagName = null;
0763: //
0764: if (childTag instanceof UnknownConceptTag) {
0765: UnknownConceptTag uct = (UnknownConceptTag) childTag;
0766: tagNs = uct.getUnknownConceptTagNamespace();
0767: tagName = uct.getUnknownConceptTagName();
0768: } else {
0769: tagNs = ctsh.getConceptTagNamespace(tagClass
0770: .getName());
0771: tagName = ctsh
0772: .getConceptTagName(tagClass.getName());
0773: }
0774: //
0775: if (tagName == null) {
0776: throw new UnexpectedSystemException(
0777: "Class '"
0778: + tagClass.getName()
0779: + "' is not registered and does not implement '"
0780: + UnknownConceptTag.class.getName()
0781: + "'");
0782: }
0783:
0784: childElement = inDoc.createElementNS(tagNs, tagName);
0785: appendAttributes(inDoc, childElement, childCtcharh);
0786: appendStyles(inDoc, childElement, childCtcharh);
0787: appendNode(inDoc, inBase, childElement, constraintName);
0788:
0789: int jCount = childCtcharh.getCharacteristicCount();
0790: for (int j = 0; j < jCount; j++) {
0791: ConceptTagCharacteristic nextCtchar = childCtcharh
0792: .getCharacteristic(j);
0793: getXMLRepresentation(nextCtchar, childElement,
0794: inDoc);
0795: }
0796: if (this .ctp.isUnconstrainedAllowed()) {
0797: ConceptTagCharacteristic nextCtchar = childCtcharh
0798: .getUndefinedCharacteristic();
0799: getXMLRepresentation(nextCtchar, childElement,
0800: inDoc);
0801: }
0802: } else if (inCtchar.isHolderWithoutConceptTag(i, null)) {
0803: Element childElement = null;
0804: if (this .ctp.isCompoundUsed()) {
0805: ConceptTagCharacteristicHolder childCtcharh = inCtchar
0806: .getValueAsHolder(i, null);
0807:
0808: if (this .ctp.getCompoundNamespace() != null) {
0809: childElement = inDoc.createElementNS(this .ctp
0810: .getCompoundNamespace(), this .ctp
0811: .getCompoundName());
0812: } else {
0813: childElement = inDoc.createElement(this .ctp
0814: .getCompoundName());
0815: }
0816:
0817: appendAttributes(inDoc, childElement, childCtcharh);
0818: appendStyles(inDoc, childElement, childCtcharh);
0819: appendNode(inDoc, inBase, childElement,
0820: constraintName);
0821:
0822: int jCount = childCtcharh.getCharacteristicCount();
0823: for (int j = 0; j < jCount; j++) {
0824: ConceptTagCharacteristic nextCtchar = childCtcharh
0825: .getCharacteristic(j);
0826: getXMLRepresentation(nextCtchar, childElement,
0827: inDoc);
0828: }
0829: } else {
0830: UnexpectedSystemException.unknownState();
0831: }
0832: } else {
0833: throw new UnexpectedSystemException("Unknown Ctchar ("
0834: + inCtchar + ") value at " + i);
0835: }
0836:
0837: }
0838: }
0839:
0840: private void appendAttributes(Document inDoc, Element inElement,
0841: ConceptTagCharacteristicHolder inCtcharh) {
0842:
0843: try {
0844: ConceptTagCharacteristicHolder attrs = inCtcharh
0845: .getAttributes();
0846: for (int i = 0; i < attrs.getCharacteristicCount(); i++) {
0847: ConceptTagCharacteristic nextCtchar = attrs
0848: .getCharacteristic(i);
0849: appendAttribute(inDoc, inElement, nextCtchar);
0850: }
0851: } catch (IOException ioe) {
0852: throw new UnexpectedSystemException(ioe);
0853: }
0854: }
0855:
0856: private void appendAttribute(Document inDoc, Element inElement,
0857: ConceptTagCharacteristic inCtchar) throws IOException {
0858:
0859: Attr nextAttr = inDoc.createAttribute(inCtchar
0860: .getConstraintName());
0861: nextAttr.setNodeValue(XMLHelper.fromStringToCData(inCtchar
0862: .getValueAsString(0, null)));
0863: inElement.setAttributeNode(nextAttr);
0864: }
0865:
0866: private void appendStyles(Document inDoc, Element inElement,
0867: ConceptTagCharacteristicHolder inCtcharh) {
0868:
0869: try {
0870: if (this .ctp.isStyleUsed()) {
0871: StringBuffer styleContent = new StringBuffer();
0872: ConceptTagCharacteristicHolder styles = inCtcharh
0873: .getStyles();
0874: int icount = styles.getCharacteristicCount();
0875: for (int i = 0; i < icount; i++) {
0876: ConceptTagCharacteristic nextCtchar = styles
0877: .getCharacteristic(i);
0878: appendStyle(styleContent, nextCtchar);
0879: }
0880: //
0881: if (styleContent.length() > 0) {
0882: Attr styleAttr = null;
0883: if (this .ctp.getStyleNamespace() != null) {
0884: styleAttr = inDoc.createAttributeNS(this .ctp
0885: .getStyleNamespace(), this .ctp
0886: .getStyleName());
0887: } else {
0888: styleAttr = inDoc.createAttribute(this .ctp
0889: .getStyleName());
0890: }
0891: styleAttr.setNodeValue(styleContent.toString());
0892: inElement.setAttributeNode(styleAttr);
0893: }
0894: }
0895: } catch (IOException ioe) {
0896: throw new UnexpectedSystemException(ioe);
0897: }
0898: }
0899:
0900: private void appendStyle(StringBuffer inBuffer,
0901: ConceptTagCharacteristic inCtchar) throws IOException {
0902:
0903: if (inCtchar.getValueLength() > 0) {
0904: inBuffer.append(" ");
0905: inBuffer.append(inCtchar.getConstraintName());
0906: inBuffer.append(": ");
0907: inBuffer.append(XMLHelper.fromStringToCData(inCtchar
0908: .getValueAsString(0, null)));
0909: inBuffer.append(";");
0910: }
0911: }
0912:
0913: private void appendNode(Document inDoc, Node inBase,
0914: Element inChild, String inConstraint) {
0915:
0916: if (this .ctp.isConstraintUsed()) {
0917: if (inConstraint.equals(this .ctp.getUnconstrainedSymbol())) {
0918: //don't write unconstrained attribute
0919: inBase.appendChild(inChild);
0920: } else if (this .ctp.getConstraintNamespace() != null) {
0921: Attr conAttr = inDoc.createAttributeNS(this .ctp
0922: .getConstraintNamespace(), this .ctp
0923: .getConstraintName());
0924: conAttr.setNodeValue(inConstraint);
0925: inChild.setAttributeNode(conAttr);
0926:
0927: inBase.appendChild(inChild);
0928: } else {
0929: Attr conAttr = inDoc.createAttribute(this .ctp
0930: .getConstraintName());
0931: conAttr.setNodeValue(inConstraint);
0932: inChild.setAttributeNode(conAttr);
0933:
0934: inBase.appendChild(inChild);
0935: }
0936: } else {
0937: UnexpectedSystemException.unknownState();
0938: }
0939: }
0940:
0941: //
0942: public boolean isCharacteristicPresent(String inCode) {
0943:
0944: boolean outValue = false;
0945:
0946: //
0947: ConceptTagCharacteristic localCtchar[] = new ConceptTagCharacteristic[1];
0948: ConceptTagCharacteristicHolder localCtcharh[] = new ConceptTagCharacteristicHolder[1];
0949: ConceptTagConstraint localCtcon[] = new ConceptTagConstraint[1];
0950: ConceptTagConstraintHolder localCtconh[] = new ConceptTagConstraintHolder[1];
0951: //
0952: getEndComponent(inCode, localCtchar, localCtcon, localCtcharh,
0953: localCtconh);
0954:
0955: //
0956: outValue = (localCtchar[0] != null);
0957:
0958: return outValue;
0959: }
0960:
0961: public ConceptTagCharacteristic getCharacteristic(String inCode) {
0962:
0963: ConceptTagCharacteristic outValue = null;
0964:
0965: //
0966: ConceptTagCharacteristic localCtchar[] = new ConceptTagCharacteristic[1];
0967: ConceptTagCharacteristicHolder localCtcharh[] = new ConceptTagCharacteristicHolder[1];
0968: ConceptTagConstraint localCtcon[] = new ConceptTagConstraint[1];
0969: ConceptTagConstraintHolder localCtconh[] = new ConceptTagConstraintHolder[1];
0970: //
0971: getEndComponent(inCode, localCtchar, localCtcon, localCtcharh,
0972: localCtconh);
0973:
0974: //
0975: if (localCtchar[0] != null) {
0976: outValue = localCtchar[0];
0977: } else {
0978: throw new UnexpectedSystemException(
0979: "No ConceptTagCharacteristic found for code '"
0980: + inCode + "'");
0981: }
0982:
0983: return outValue;
0984: }
0985:
0986: public ConceptTagConstraint getConstraint(String inCode) {
0987:
0988: ConceptTagConstraint outValue = null;
0989:
0990: //
0991: ConceptTagCharacteristic localCtchar[] = new ConceptTagCharacteristic[1];
0992: ConceptTagCharacteristicHolder localCtcharh[] = new ConceptTagCharacteristicHolder[1];
0993: ConceptTagConstraint localCtcon[] = new ConceptTagConstraint[1];
0994: ConceptTagConstraintHolder localCtconh[] = new ConceptTagConstraintHolder[1];
0995: //
0996: getEndComponent(inCode, localCtchar, localCtcon, localCtcharh,
0997: localCtconh);
0998:
0999: //
1000: if (localCtcon[0] != null) {
1001: outValue = localCtcon[0];
1002: } else {
1003: throw new UnexpectedSystemException(
1004: "No ConceptTagConstraint found for code '" + inCode
1005: + "'");
1006: }
1007:
1008: return outValue;
1009: }
1010:
1011: //
1012: private void getEndComponent(String inCode,
1013: ConceptTagCharacteristic inCtchar[],
1014: ConceptTagConstraint inCtcon[],
1015: ConceptTagCharacteristicHolder inCtcharh[],
1016: ConceptTagConstraintHolder inCtconh[]) {
1017: //
1018: inCtchar[0] = null;
1019: inCtcon[0] = null;
1020: inCtcharh[0] = this .rootCtcharh;
1021: inCtconh[0] = this .rootCtconh;
1022: //
1023: StringTokenizer st = new StringTokenizer(inCode, this .ctp
1024: .getHandleConjunction());
1025: while (st.hasMoreTokens()) {
1026: String nextComp = st.nextToken();
1027: if (inCtchar[0] != null) {
1028: if (MiscHelper.isStringNonNegativeNumber(nextComp)) {
1029: int nextIndex = Integer.parseInt(nextComp);
1030: if (nextIndex < (inCtchar[0]).getValueLength()) {
1031: if ((inCtchar[0]).isHolder(nextIndex, null)) {
1032: //
1033: inCtcharh[0] = (inCtchar[0])
1034: .getValueAsHolder(nextIndex, null);
1035: inCtchar[0] = null;
1036: //
1037: if (inCtcon[0] != null) {
1038: inCtconh[0] = (inCtcon[0])
1039: .getConstraintHolder(nextIndex);
1040: inCtcon[0] = null;
1041: } else {
1042: inCtcon[0] = null;
1043: }
1044: //
1045: //if (inCtconh[0] != null) {
1046: // if (nextIndex <
1047: // (inCtconh[0]).getConstraintCount()) {
1048: // //
1049: // inCtcon[0] = (inCtconh[0]).getConstraint(
1050: // nextIndex);
1051: // }
1052: // else {
1053: // inCtcon[0] = null;
1054: // }
1055: // inCtconh[0] = null;
1056: //}
1057: //else {
1058: // inCtcon[0] = null;
1059: //}
1060: } else {
1061: //
1062: inCtchar[0] = null;
1063: inCtcon[0] = null;
1064: inCtcharh[0] = null;
1065: inCtconh[0] = null;
1066: //
1067: break;
1068: }
1069: } else {
1070: //
1071: inCtchar[0] = null;
1072: inCtcon[0] = null;
1073: inCtcharh[0] = null;
1074: inCtconh[0] = null;
1075: //
1076: break;
1077: }
1078: } else {
1079: //
1080: inCtchar[0] = null;
1081: inCtcon[0] = null;
1082: inCtcharh[0] = null;
1083: inCtconh[0] = null;
1084: //
1085: break;
1086: }
1087: } else if (inCtcharh[0] != null) {
1088: if (nextComp.equals(this .ctp.getUnconstrainedSymbol())) {
1089: //
1090: if (this .ctp.isUnconstrainedAllowed()) {
1091: //
1092: inCtchar[0] = (inCtcharh[0])
1093: .getUndefinedCharacteristic();
1094: inCtcharh[0] = null;
1095: //
1096: if (inCtconh[0] != null) {
1097: inCtcon[0] = (inCtconh[0])
1098: .getUndefinedConstraint();
1099: inCtconh[0] = null;
1100: } else {
1101: inCtcon[0] = null;
1102: }
1103: } else {
1104: //
1105: inCtchar[0] = null;
1106: inCtcon[0] = null;
1107: inCtcharh[0] = null;
1108: inCtconh[0] = null;
1109: //
1110: break;
1111: }
1112: } else if (nextComp.equals(this .ctp
1113: .getAttributeConstraintName())) {
1114: //
1115: if (!this .ctp.isAttributeAndStyleForbidden()) {
1116: //
1117: inCtcharh[0] = (inCtcharh[0]).getAttributes();
1118: inCtchar[0] = null;
1119: //
1120: if (inCtconh[0] != null) {
1121: inCtconh[0] = (inCtconh[0]).getAttributes();
1122: inCtcon[0] = null;
1123: } else {
1124: inCtcon[0] = null;
1125: }
1126: } else {
1127: //
1128: inCtchar[0] = null;
1129: inCtcon[0] = null;
1130: inCtcharh[0] = null;
1131: inCtconh[0] = null;
1132: //
1133: break;
1134: }
1135: } else if (nextComp.equals(this .ctp
1136: .getStyleConstraintName())) {
1137: //
1138: if (!this .ctp.isAttributeAndStyleForbidden()) {
1139: //
1140: inCtcharh[0] = (inCtcharh[0]).getStyles();
1141: inCtchar[0] = null;
1142: //
1143: if (inCtconh[0] != null) {
1144: inCtconh[0] = (inCtconh[0]).getStyles();
1145: inCtcon[0] = null;
1146: } else {
1147: inCtcon[0] = null;
1148: }
1149: } else {
1150: //
1151: inCtchar[0] = null;
1152: inCtcon[0] = null;
1153: inCtcharh[0] = null;
1154: inCtconh[0] = null;
1155: //
1156: break;
1157: }
1158: } else if ((inCtcharh[0])
1159: .isCharacteristicPresent(nextComp)) {
1160: //
1161: inCtchar[0] = (inCtcharh[0])
1162: .getCharacteristic(nextComp);
1163: inCtcharh[0] = null;
1164: //
1165: if (inCtconh[0] != null) {
1166: if ((inCtconh[0]).isConstraintPresent(nextComp)) {
1167: inCtcon[0] = (inCtconh[0])
1168: .getConstraint(nextComp);
1169: }
1170: inCtconh[0] = null;
1171: } else {
1172: inCtcon[0] = null;
1173: }
1174: } else {
1175: //
1176: inCtchar[0] = null;
1177: inCtcon[0] = null;
1178: inCtcharh[0] = null;
1179: inCtconh[0] = null;
1180: //
1181: break;
1182: }
1183: } else {
1184: throw UnexpectedSystemException.unknownState();
1185: }
1186: }
1187: }
1188: }
|