001: /*
002: * Hammurapi
003: * Automated Java code review system.
004: * Copyright (C) 2004 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU General Public License as published by
008: * the Free Software Foundation; either version 2 of the License, or
009: * (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.org
021: * e-Mail: support@hammurapi.biz
022: */
023: package org.hammurapi.inspectors.techstack;
024:
025: import java.util.ArrayList;
026: import java.util.Collection;
027: import java.util.Iterator;
028:
029: import javax.xml.transform.TransformerException;
030:
031: import org.apache.xpath.CachedXPathAPI;
032: import org.w3c.dom.Element;
033: import org.w3c.dom.Node;
034: import org.w3c.dom.traversal.NodeIterator;
035:
036: import com.pavelvlasov.xml.dom.DomSerializable;
037:
038: /**
039: * @author Pavel Vlasov
040: * @version $Revision: 1.1 $
041: */
042: public class Publisher extends Licensor {
043:
044: public Publisher(TechStack stack) {
045: super (stack);
046: }
047:
048: public Publisher(TechStack stack, Element holder,
049: CachedXPathAPI cxpa) throws TransformerException {
050: super (stack, holder, cxpa);
051: NodeIterator nit = cxpa.selectNodeIterator(holder, "product");
052: Node n;
053: while ((n = nit.nextNode()) != null) {
054: new Product(stack, this , (Element) n, cxpa);
055: }
056: }
057:
058: private Collection products = new ArrayList();
059:
060: void addProduct(Product product) {
061: products.add(product);
062:
063: }
064:
065: public void toDom(Element holder) {
066: super .toDom(holder);
067: Iterator it = products.iterator();
068: while (it.hasNext()) {
069: ((DomSerializable) it.next()).toDom(addElement(holder,
070: "product"));
071: }
072: }
073:
074: /**
075: * Matches package name and adds clients if matched
076: * @param packageName
077: * @param clients
078: * @return true if matched.
079: */
080: public boolean match(String packageName, Collection clients) {
081: Iterator it = products.iterator();
082: while (it.hasNext()) {
083: Product product = (Product) it.next();
084: if (product.match(packageName, clients)) {
085: return true;
086: }
087: }
088: return false;
089: }
090:
091: /**
092: * Matches package with publisher prefix and returns 'product' part of package.
093: * @param packageName
094: * @return
095: */
096: public String[] match(String packageName) {
097: Iterator it = packages.iterator();
098: while (it.hasNext()) {
099: String pn = (String) it.next();
100: if (pn.equals(packageName)) {
101: return new String[] { packageName, null };
102: } else if (packageName.startsWith(pn + ".")) {
103: int idx = packageName.indexOf('.', pn.length() + 1);
104: if (idx == -1) {
105: return new String[] { pn,
106: packageName.substring(pn.length() + 1) };
107: } else {
108: return new String[] { pn,
109: packageName.substring(pn.length() + 1, idx) };
110: }
111: }
112: }
113: return null;
114: }
115: }
|