001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.xml.retriever.catalog.impl;
042:
043: import java.io.IOException;
044: import java.util.WeakHashMap;
045: import java.util.logging.Logger;
046: import org.netbeans.api.project.FileOwnerQuery;
047: import org.netbeans.api.project.Project;
048: import org.netbeans.modules.xml.retriever.catalog.CatalogWriteModel;
049: import org.netbeans.modules.xml.retriever.catalog.CatalogWriteModelFactory;
050: import org.netbeans.modules.xml.retriever.catalog.Utilities;
051: import org.netbeans.modules.xml.xam.ModelSource;
052: import org.netbeans.modules.xml.xam.locator.CatalogModelException;
053: import org.netbeans.modules.xml.xam.locator.CatalogModel;
054: import org.openide.filesystems.FileObject;
055: import org.w3c.dom.ls.LSResourceResolver;
056:
057: /**
058: *
059: * @author girix
060: */
061: public class CatalogModelFactoryImpl extends CatalogWriteModelFactory {
062: private static Logger logger = Logger
063: .getLogger(CatalogModelFactoryImpl.class.getName());
064:
065: private static WeakHashMap<Project, CatalogWriteModel> projcat = new WeakHashMap<Project, CatalogWriteModel>();
066:
067: private static WeakHashMap<FileObject, CatalogWriteModel> foCat = new WeakHashMap<FileObject, CatalogWriteModel>();
068:
069: private static int count = 0;
070:
071: public CatalogWriteModel getCatalogWriteModelForProject(
072: FileObject anyFileObjectExistingInAProject)
073: throws CatalogModelException {
074: logger.entering("CatalogModelFactoryImpl",
075: "getCatalogModelForProject");
076: Project project = FileOwnerQuery
077: .getOwner(anyFileObjectExistingInAProject);
078: assert (project != null);
079: CatalogWriteModel result = null;
080: try {
081: CatalogWriteModel cwm = foCat.get(Utilities
082: .getProjectCatalogFileObject(project));
083: if (cwm != null) {
084: return cwm;
085: }
086: result = new XAMCatalogWriteModelImpl(project);
087: foCat.put(result.getCatalogFileObject(), result);
088: return result;
089: } catch (IOException ex) {
090: throw new CatalogModelException(ex);
091: }
092: }
093:
094: private static WeakHashMap<Project, CatalogModel> proj2cm = new WeakHashMap<Project, CatalogModel>();
095:
096: public CatalogModel getCatalogModel(ModelSource modelSource)
097: throws CatalogModelException {
098: if (modelSource == null)
099: throw new IllegalArgumentException(
100: "modelSource arg is null.");
101: CatalogModel catalogModel = modelSource.getLookup().lookup(
102: CatalogModel.class);
103: if (catalogModel == null) {
104: FileObject fo = modelSource.getLookup().lookup(
105: FileObject.class);
106: if (fo == null)
107: throw new IllegalArgumentException(
108: "ModelSource must have FileObject in its lookup");
109: return getCatalogModel(fo);
110: }
111: return catalogModel;
112: }
113:
114: public CatalogModel getCatalogModel(FileObject fo)
115: throws CatalogModelException {
116: CatalogModel catalogModel = null;
117: Project project = FileOwnerQuery.getOwner(fo);
118: if (project != null) {
119: catalogModel = proj2cm.get(fo);
120: if (catalogModel != null)
121: return catalogModel;
122: try {
123: catalogModel = new CatalogModelImpl(project);
124: } catch (IOException ex) {
125: throw new CatalogModelException(ex);
126: }
127: return catalogModel;
128: }
129: catalogModel = new CatalogModelImpl();
130: return catalogModel;
131: }
132:
133: public LSResourceResolver getLSResourceResolver() {
134: return new LSResourceResolverImpl();
135: }
136:
137: public CatalogWriteModel getCatalogWriteModelForCatalogFile(
138: FileObject fileObjectOfCatalogFile)
139: throws CatalogModelException {
140: try {
141: return new XAMCatalogWriteModelImpl(fileObjectOfCatalogFile);
142: } catch (IOException ex) {
143: throw new CatalogModelException(ex);
144: }
145: }
146: }
|