|
What this is
Other links
The source code/* * Sun Public License Notice * * The contents of this file are subject to the Sun Public License * Version 1.0 (the "License"). You may not use this file except in * compliance with the License. A copy of the License is available at * http://www.sun.com/ * * The Original Code is NetBeans. The Initial Developer of the Original * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun * Microsystems, Inc. All Rights Reserved. */ package org.netbeans.modules.vcs.advanced.variables; import java.io.FileNotFoundException; import java.io.IOException; import java.util.*; import java.text.MessageFormat; import org.w3c.dom.*; import org.xml.sax.*; import org.openide.filesystems.FileObject; import org.openide.loaders.DataObject; import org.openide.loaders.XMLDataObject; import org.openide.loaders.DataObjectNotFoundException; import org.openide.util.NbBundle; import org.openide.xml.XMLUtil; import org.netbeans.modules.vcscore.VcsConfigVariable; import org.netbeans.modules.vcscore.util.VcsUtilities; /** * This class provides input/output of variables from/to xml file. * * @author Martin Entlicher */ public class VariableIO extends Object { public static final String CONFIG_FILE_EXT = "xml"; // NOI18N public static final String CONFIG_ROOT_ELEM = "configuration"; // NOI18N public static final String RESOURCE_BUNDLE_TAG = "resourceBundle"; // NOI18N public static final String LABEL_TAG = "label"; // NOI18N public static final String OS_TAG = "os"; // NOI18N public static final String OS_COMPATIBLE_TAG = "compatible"; // NOI18N public static final String OS_UNCOMPATIBLE_TAG = "uncompatible"; // NOI18N public static final String VARIABLES_TAG = "variables"; // NOI18N public static final String VARIABLE_TAG = "variable"; // NOI18N public static final String VARIABLE_SELECTOR_TAG = "selector"; // NOI18N public static final String VARIABLE_NAME_ATTR = "name"; // NOI18N public static final String VARIABLE_LABEL_ATTR = "label"; // NOI18N public static final String VARIABLE_LABEL_MNEMONIC_ATTR = "labelMnemonic"; // NOI18N public static final String VARIABLE_A11Y_NAME_ATTR = "a11yName"; // NOI18N public static final String VARIABLE_A11Y_DESCRIPTION_ATTR = "a11yDescription";// NOI18N public static final String VARIABLE_BASIC_ATTR = "basic"; // NOI18N public static final String VARIABLE_LOCAL_FILE_ATTR = "localFile"; // NOI18N public static final String VARIABLE_LOCAL_DIR_ATTR = "localDir"; // NOI18N public static final String VARIABLE_EXECUTABLE_ATTR = "executable"; // NOI18N public static final String VARIABLE_ORDER_ATTR = "order"; // NOI18N public static final String VARIABLE_IF_ATTR = "if"; // NOI18N public static final String VARIABLE_UNLESS_ATTR = "unless"; // NOI18N public static final String VARIABLE_VALUE_TAG = "value"; // NOI18N public static final String BOOLEAN_VARIABLE_TRUE = "true"; // NOI18N public static final String BOOLEAN_VARIABLE_FALSE = "false"; // NOI18N /** The DTD for a configuration profile. */ public static final String PUBLIC_ID = "-//NetBeans//DTD VCS Configuration 1.0//EN"; // NOI18N public static final String SYSTEM_ID = "http://www.netbeans.org/dtds/vcs-configuration-1_0.dtd"; // NOI18N public static final String TEMPORARY_CONFIG_FILE_NAME = "tmp"; // NOI18N /** Whether to validate XML files. Safer, but slows down. */ private static final boolean VALIDATE_XML = false; private static LabelContentHandler labelContentHandler = null; /** Creates new VariableIO */ private VariableIO() { } private static boolean isResourceBundle(FileObject fo) { return fo.getExt().equalsIgnoreCase("properties") && (fo.getName().startsWith("Bundle") || fo.getName().endsWith("Bundle") || fo.getName().indexOf("Bundle_") > 0); } /** Read list of available confugurations from the directory. * All files with extension ".properties" are considered to be configurations. * However only properties with current localization are read. * @return the available configurations. */ public static ArrayList readConfigurations(FileObject file) { //ArrayList res = new ArrayList(); FileObject[] ch = file.getChildren(); ArrayList list = new ArrayList(Arrays.asList(ch)); for(int i = 0; i < list.size(); i++) { FileObject fo = (FileObject) list.get(i); String ext = fo.getExt(); if (!ext.equalsIgnoreCase(CONFIG_FILE_EXT) && !ext.equalsIgnoreCase(VariableIOCompat.CONFIG_FILE_EXT) || isTemporaryConfig(fo.getName()) || isResourceBundle(fo)) { list.remove(i); i--; } } ch = (FileObject[]) list.toArray(new FileObject[0]); ArrayList res = getLocalizedConfigurations(ch); hideOlderConfs(res); return res; } /** * Finds out, whether the configuration file name is a temporary configuration. * Temporary configurations are saved during serialization of the FS. */ public static boolean isTemporaryConfig(String configFileName) { if (configFileName.startsWith(TEMPORARY_CONFIG_FILE_NAME)) { String tempNo = configFileName.substring(TEMPORARY_CONFIG_FILE_NAME.length()); if (tempNo.length() == 5) { try { Integer.parseInt(tempNo); } catch (NumberFormatException exc) { return false; } return true; } } return false; } private static void hideOlderConfs(ArrayList list) { for(int i = 0; i < list.size(); i++) { String fullName = (String) list.get(i); int dotIndex = fullName.lastIndexOf('.'); String ext = fullName.substring(dotIndex + 1); if (VariableIOCompat.CONFIG_FILE_EXT.equals(ext)) { String name = fullName.substring(0, dotIndex); String newName = name + "." + CONFIG_FILE_EXT; if (list.contains(newName)) { list.remove(i); i--; } } } } /** * Pick the right localized configurations. */ public static final ArrayList getLocalizedConfigurations(FileObject[] ch) { ArrayList res = new ArrayList(); Locale locale = Locale.getDefault(); String language = locale.getLanguage(); String country = locale.getCountry(); String variant = locale.getVariant(); String localeStr = locale.toString(); String languageCountry = language+"_"+country; String baseLocaleStr = "_"+localeStr; String baseLanguage = "_"+language; String baseLanguageCountry = baseLanguage+"_"+country; String baseLanguageCountryVariant = baseLanguageCountry+variant; String[] languages = Locale.getISOLanguages(); List languagesList = Arrays.asList(languages); HashSet bundleList = new HashSet(); for (int i = 0; i < ch.length; i++) { bundleList.add(ch[i].getName()); } for(int i = 0; i < ch.length; i++) { String name = ch[i].getName(); //System.out.println("name = "+name+", locale = "+locale.toString()); int nameIndex = name.indexOf('_'); String baseName = name; String localeExt = ""; if (nameIndex > 0) { baseName = name.substring(0, nameIndex); localeExt = name.substring(nameIndex + 1); } if (localeExt.equals(locale.toString())) ; // OK else if (localeExt.equals(languageCountry)) { if (bundleList.contains(baseName+baseLocaleStr)) continue; // current variant is somewhere } else if (localeExt.equals(locale.getLanguage())) { if (bundleList.contains(baseName+baseLanguageCountry) || bundleList.contains(baseName+baseLanguageCountryVariant)) continue; } else if (localeExt.length() == 0) { if (bundleList.contains(baseName+baseLanguage) || bundleList.contains(baseName+baseLanguageCountry) || bundleList.contains(baseName+baseLanguageCountryVariant)) continue; } else if (localeExt.length() > 0) { String lang; int index = localeExt.indexOf('_'); if (index > 0) lang = localeExt.substring(0, index); else lang = localeExt; if (languagesList.contains(lang)) continue; } //System.out.println("adding: "+name+"."+ch[i].getExt()); res.add(name+"."+ch[i].getExt()); } return res; } /** Get the label and OS info. * @return three items in String array: - label, which needs to be processed * with VcsUtilities.getBundleString(), * - compatible operating systems * - uncompatible operating systems * The rest of the items are the resource bundles */ public static synchronized String[] getConfigurationLabelAndOS(FileObject configRoot, final String name) { FileObject config = configRoot.getFileObject(name); if (config == null) { org.openide.util.RequestProcessor.getDefault().post(new Runnable() { public void run() { org.openide.ErrorManager.getDefault().notify(new FileNotFoundException("Problems while reading predefined properties.") { public String getLocalizedMessage() { return g("EXC_Problems_while_reading_predefined_properties", name); } }); } }); //E.err(g("EXC_Problems_while_reading_predefined_properties",name)); // NOI18N return null; } if (labelContentHandler == null) { labelContentHandler = new LabelContentHandler(); } //System.out.println("VariableIO.getConfigurationLabel("+config+")"); try { XMLReader reader = XMLUtil.createXMLReader(false, false); reader.setContentHandler(labelContentHandler); reader.setEntityResolver(labelContentHandler); // Not to parse the DTD //System.out.println(" parsing..."); InputSource source = new InputSource(config.getInputStream()); reader.parse(source); //System.out.println(" parsing done."); } catch (SAXException exc) { //System.out.println(" parsing done. ("+exc.getMessage()+")"); if (!"End of label.".equals(exc.getMessage())) { org.openide.ErrorManager.getDefault().notify( org.openide.ErrorManager.getDefault().annotate( exc, g("EXC_Problems_while_reading_predefined_properties", name))); } } catch (java.io.FileNotFoundException fnfExc) { } catch (java.io.IOException ioExc) { } //System.out.println(" --> label = "+labelContentHandler.getLabel()); if (labelContentHandler.getLabel() == null) { return null; } String[] resourceBundles = labelContentHandler.getResourceBundles(); String[] labelAndOS; if (resourceBundles != null) { labelAndOS = new String[3 + resourceBundles.length]; } else { labelAndOS = new String[3]; } labelAndOS[0] = labelContentHandler.getLabel(); labelAndOS[1] = labelContentHandler.getCompatibleOSs(); labelAndOS[2] = labelContentHandler.getUncompatibleOSs(); labelContentHandler.reset(); if (resourceBundles != null) { System.arraycopy(resourceBundles, 0, labelAndOS, 3, resourceBundles.length); } return labelAndOS; } /** Read list of VCS variables from the document. * If there is only value specified, label is an empty string and basic, * localFile and localDir are false. */ public static ConditionedVariables readVariables(Document doc) throws DOMException { Element rootElem = doc.getDocumentElement(); if (!CONFIG_ROOT_ELEM.equals(rootElem.getNodeName())) throw new DOMException((short) 0, "Wrong root element: "+rootElem.getNodeName()); ConditionedVariables vars; NodeList varList = rootElem.getElementsByTagName(VARIABLES_TAG); if (varList.getLength() > 0) { Node varsNode = varList.item(0); vars = getVariables(varsNode.getChildNodes()); } else { vars = new ConditionedVariables(Collections.EMPTY_LIST, Collections.EMPTY_MAP, Collections.EMPTY_MAP); } return vars; } public static ConditionedVariables getVariables(NodeList varList) throws DOMException { List vars = new ArrayList(); Map conditionsByVariables = new TreeMap(); Map varsByConditions = new HashMap(); int n = varList.getLength(); for (int i = 0; i < n; i++) { Node varNode = varList.item(i); if (VARIABLE_TAG.equals(varNode.getNodeName())) { String name = ""; String if_attr = ""; String unless_attr = ""; //boolean isBasic = false; String value = ""; NamedNodeMap varAttrs = varNode.getAttributes(); Node nameAttr = varAttrs.getNamedItem(VARIABLE_NAME_ATTR); if (nameAttr == null) continue; name = nameAttr.getNodeValue(); Node ifAttr = varAttrs.getNamedItem(VARIABLE_IF_ATTR); if (ifAttr != null) if_attr = ifAttr.getNodeValue(); Node unlessAttr = varAttrs.getNamedItem(VARIABLE_UNLESS_ATTR); if (unlessAttr != null) unless_attr = unlessAttr.getNodeValue(); Map valuesByConditions = new HashMap(); Condition cond = createCondition(name, if_attr, unless_attr); NodeList valueList = varNode.getChildNodes(); //System.out.println("valueList.length = "+valueList.getLength()); int m = valueList.getLength(); for (int j = 0; j < m; j++) { Node valueNode = valueList.item(j); //System.out.println("value("+j+") name = "+valueNode.getNodeName()); if (VARIABLE_VALUE_TAG.equals(valueNode.getNodeName())) { NodeList textList = valueNode.getChildNodes(); for (int itl = 0; itl < textList.getLength(); itl++) { Node subNode = textList.item(itl); //System.out.println(" subNode = "+subNode); if (subNode instanceof Text) { Text textNode = (Text) subNode; value += textNode.getData(); } if (subNode instanceof EntityReference) { EntityReference entityNode = (EntityReference) subNode; //System.out.println("Have EntityReference = "+entityNode+", value = "+entityNode.getNodeValue()); NodeList entityList = entityNode.getChildNodes(); for (int iel = 0; iel < entityList.getLength(); iel++) { Node entitySubNode = entityList.item(iel); //System.out.println(" entitySubNode = "+entitySubNode); if (entitySubNode instanceof Text) { Text textEntityNode = (Text) entitySubNode; value += textEntityNode.getData(); } } } //System.out.println(" propertyValue = "+propertyValue); } /* if (textList.getLength() > 0) { Node subNode = textList.item(0); //System.out.println("subNode = "+subNode.getNodeName()); if (subNode instanceof Text) { Text textNode = (Text) subNode; value = textNode.getData(); //System.out.println("value = '"+value+"'"); } } */ String value_if_attr = ""; String value_unless_attr = ""; NamedNodeMap valueAttrs = valueNode.getAttributes(); if (valueAttrs != null) { Node valueIfAttr = valueAttrs.getNamedItem(VARIABLE_IF_ATTR); if (valueIfAttr != null) value_if_attr = valueIfAttr.getNodeValue(); Node valueUnlessAttr = valueAttrs.getNamedItem(VARIABLE_UNLESS_ATTR); if (valueUnlessAttr != null) value_unless_attr = valueUnlessAttr.getNodeValue(); } Condition vc = createCondition(name, value_if_attr, value_unless_attr); valuesByConditions.put(vc, value); value = ""; } } Condition[] conditions = new Condition[valuesByConditions.size()]; int ci = 0; for (Iterator it = valuesByConditions.keySet().iterator(); it.hasNext(); ) { Condition vc = (Condition) it.next(); if (vc == null) continue; String cvalue = (String) valuesByConditions.get(vc); VcsConfigVariable var = readVariable(name, cvalue, varNode, varAttrs); if (cond != null) vc.addCondition(cond, true); conditions[ci] = vc; varsByConditions.put(vc, var); ci++; } value = (String) valuesByConditions.get(null); if (value != null) { VcsConfigVariable var = readVariable(name, value, varNode, varAttrs); //System.out.println(" var = "+var); if (valuesByConditions.size() > 1) { cond = createComplementaryCondition(name, cond, valuesByConditions.keySet()); } if (cond != null) { varsByConditions.put(cond, var); conditions[conditions.length - 1] = cond; } else { vars.add(var); conditions = null; } } if (conditions != null) { Condition[] oldConditions = (Condition[]) conditionsByVariables.get(name); if (oldConditions != null) { Condition[] newConditions = new Condition[oldConditions.length + conditions.length]; System.arraycopy(oldConditions, 0, newConditions, 0, oldConditions.length); System.arraycopy(conditions, 0, newConditions, oldConditions.length, conditions.length); conditions = newConditions; } conditionsByVariables.put(name, conditions); } } } return new ConditionedVariables(vars, conditionsByVariables, varsByConditions); } private static VcsConfigVariable readVariable(String name, String value, Node varNode, NamedNodeMap varAttrs) { VcsConfigVariable var; Node basicAttr = varAttrs.getNamedItem(VARIABLE_BASIC_ATTR); value = VcsUtilities.getBundleString(value); value = translateVariableValue(name, value); if (basicAttr != null && BOOLEAN_VARIABLE_TRUE.equalsIgnoreCase(basicAttr.getNodeValue())) { var = getBasicVariable(name, value, varNode, varAttrs); } else { var = new VcsConfigVariable(name, "", value, false, false, false, ""); } return var; } /** * Create a condition of given name, that is true when varaible |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.