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:
042: package org.netbeans.modules.vmd.midpnb.components.svg.util;
043:
044: import org.xml.sax.*;
045: import org.xml.sax.helpers.XMLReaderFactory;
046: import java.io.ByteArrayInputStream;
047: import java.io.IOException;
048: import java.io.InputStream;
049: import java.util.ArrayList;
050: import java.util.Collections;
051: import java.util.List;
052: import java.util.regex.Pattern;
053: import org.netbeans.modules.vmd.api.model.Debug;
054: import org.netbeans.modules.vmd.api.model.DesignComponent;
055: import org.netbeans.modules.vmd.api.model.PropertyValue;
056: import org.netbeans.modules.vmd.midp.components.MidpTypes;
057: import org.netbeans.modules.vmd.midpnb.components.sources.SVGMenuElementEventSourceCD;
058: import org.netbeans.modules.vmd.midpnb.components.svg.SVGMenuCD;
059:
060: /**
061: *
062: * @author Anton Chechel
063: */
064: public final class SVGUtils {
065:
066: private static final String MENU_ELEMENT_SEARCH_PATTERN = "menuItem_.*"; // NOI18N
067:
068: private SVGUtils() {
069: }
070:
071: public static void parseSVGMenu(final InputStream svgInputStream,
072: final DesignComponent svgComponent) {
073: final String[] menuItems = SVGUtils
074: .getMenuItems(svgInputStream);
075: if (menuItems != null) {
076: svgComponent.getDocument().getTransactionManager()
077: .writeAccess(new Runnable() {
078:
079: public void run() {
080: List<PropertyValue> list = new ArrayList<PropertyValue>(
081: menuItems.length);
082: for (String item : menuItems) {
083: DesignComponent es = svgComponent
084: .getDocument()
085: .createComponent(
086: SVGMenuElementEventSourceCD.TYPEID);
087: es
088: .writeProperty(
089: SVGMenuElementEventSourceCD.PROP_STRING,
090: MidpTypes
091: .createStringValue(item));
092: list.add(PropertyValue
093: .createComponentReference(es));
094: svgComponent.addComponent(es);
095: }
096: svgComponent
097: .writeProperty(
098: SVGMenuCD.PROP_ELEMENTS,
099: PropertyValue
100: .createArray(
101: SVGMenuElementEventSourceCD.TYPEID,
102: list));
103: }
104: });
105: }
106: }
107:
108: private static String[] getMenuItems(
109: final InputStream svgInputStream) {
110: NamedElementsContentHandler ch = new NamedElementsContentHandler(
111: MENU_ELEMENT_SEARCH_PATTERN);
112: try {
113: XMLReader parser = XMLReaderFactory.createXMLReader();
114: parser.setContentHandler(ch);
115: parser.setEntityResolver(ch);
116: parser.parse(new InputSource(svgInputStream));
117: } catch (IOException ex) {
118: Debug.warning(ex);
119: } catch (SAXException ex) {
120: Debug.warning(ex);
121: }
122: ch.sortNamedElements();
123: return ch.getFoundElements();
124: }
125:
126: private static class NamedElementsContentHandler implements
127: ContentHandler, EntityResolver {
128:
129: private ArrayList<String> foundElements;
130: private Pattern regex;
131:
132: public NamedElementsContentHandler(String regex) {
133: this .foundElements = new ArrayList<String>();
134: this .regex = Pattern.compile(regex);
135: }
136:
137: public void sortNamedElements() {
138: Collections.sort(foundElements);
139: }
140:
141: public String[] getFoundElements() {
142: return foundElements.toArray(new String[foundElements
143: .size()]);
144: }
145:
146: public final void resetFoundElements() {
147: foundElements.clear();
148: }
149:
150: public final void endDocument() throws SAXException {
151: }
152:
153: public final void startDocument() throws SAXException {
154: }
155:
156: public final void characters(char[] ch, int start, int length)
157: throws SAXException {
158: }
159:
160: public final void ignorableWhitespace(char[] ch, int start,
161: int length) throws SAXException {
162: }
163:
164: public final void endPrefixMapping(String prefix)
165: throws SAXException {
166: }
167:
168: public final void skippedEntity(String name)
169: throws SAXException {
170: }
171:
172: public final void setDocumentLocator(Locator locator) {
173: }
174:
175: public final void processingInstruction(String target,
176: String data) throws SAXException {
177: }
178:
179: public final void startPrefixMapping(String prefix, String uri)
180: throws SAXException {
181: }
182:
183: public final void endElement(String namespaceURI,
184: String localName, String qName) throws SAXException {
185: }
186:
187: public final void startElement(String namespaceURI,
188: String localName, String qName, Attributes atts)
189: throws SAXException {
190: // get id attribute value
191: final String value = atts.getValue("id"); // NOI18N
192: if ((value != null) && regex.matcher(value).matches()) {
193: foundElements.add(value);
194: }
195: }
196:
197: public InputSource resolveEntity(String publicId,
198: String systemId) throws SAXException, IOException {
199: return new InputSource(
200: new ByteArrayInputStream(new byte[0]));
201: }
202: }
203: }
|