| | |
| | | * |
| | | * |
| | | * Copyright 2006-2009 Sun Microsystems, Inc. |
| | | * Portions Copyright 2013 ForgeRock AS. |
| | | */ |
| | | package org.opends.server.tools.makeldif; |
| | | import org.opends.messages.Message; |
| | |
| | | { |
| | | String line = lines[lineNumber]; |
| | | |
| | | // See if there are any constant definitions in the line that need to be |
| | | // replaced. We'll do that first before any further processing. |
| | | int closePos = line.lastIndexOf(']'); |
| | | if (closePos > 0) |
| | | { |
| | | StringBuilder lineBuffer = new StringBuilder(line); |
| | | int openPos = line.lastIndexOf('[', closePos); |
| | | if (openPos >= 0) |
| | | { |
| | | String constantName = |
| | | toLowerCase(line.substring(openPos+1, closePos)); |
| | | String constantValue = templateFileConstants.get(constantName); |
| | | if (constantValue == null) |
| | | { |
| | | Message message = WARN_MAKELDIF_WARNING_UNDEFINED_CONSTANT.get( |
| | | constantName, lineNumber); |
| | | warnings.add(message); |
| | | } |
| | | else |
| | | { |
| | | lineBuffer.replace(openPos, closePos+1, constantValue); |
| | | } |
| | | } |
| | | |
| | | line = lineBuffer.toString(); |
| | | } |
| | | |
| | | line = replaceConstants(line, lineNumber, |
| | | templateFileConstants, warnings); |
| | | |
| | | String lowerLine = toLowerCase(line); |
| | | if ((line.length() == 0) || line.startsWith("#")) |
| | |
| | | } |
| | | else |
| | | { |
| | | // See if there are any constant definitions in the line that need |
| | | // to be replaced. We'll do that first before any further |
| | | // processing. |
| | | closePos = line.lastIndexOf(']'); |
| | | if (closePos > 0) |
| | | { |
| | | StringBuilder lineBuffer = new StringBuilder(line); |
| | | int openPos = line.lastIndexOf('[', closePos); |
| | | if (openPos >= 0) |
| | | { |
| | | String constantName = |
| | | toLowerCase(line.substring(openPos+1, closePos)); |
| | | String constantValue = templateFileConstants.get(constantName); |
| | | if (constantValue == null) |
| | | { |
| | | Message message = |
| | | WARN_MAKELDIF_WARNING_UNDEFINED_CONSTANT.get( |
| | | constantName, lineNumber); |
| | | warnings.add(message); |
| | | } |
| | | else |
| | | { |
| | | lineBuffer.replace(openPos, closePos+1, constantValue); |
| | | } |
| | | } |
| | | |
| | | line = lineBuffer.toString(); |
| | | } |
| | | |
| | | line = replaceConstants(line, lineNumber, |
| | | templateFileConstants, warnings); |
| | | lineList.add(line); |
| | | } |
| | | } |
| | |
| | | |
| | | Branch b = parseBranchDefinition(branchLines, lineNumber, |
| | | templateFileIncludeTags, |
| | | templateFileConstants, warnings); |
| | | warnings); |
| | | DN branchDN = b.getBranchDN(); |
| | | if (templateFileBranches.containsKey(branchDN)) |
| | | { |
| | |
| | | } |
| | | else |
| | | { |
| | | // See if there are any constant definitions in the line that need |
| | | // to be replaced. We'll do that first before any further |
| | | // processing. |
| | | closePos = line.lastIndexOf(']'); |
| | | if (closePos > 0) |
| | | { |
| | | StringBuilder lineBuffer = new StringBuilder(line); |
| | | int openPos = line.lastIndexOf('[', closePos); |
| | | if (openPos >= 0) |
| | | { |
| | | String constantName = |
| | | toLowerCase(line.substring(openPos+1, closePos)); |
| | | String constantValue = templateFileConstants.get(constantName); |
| | | if (constantValue == null) |
| | | { |
| | | Message message = |
| | | WARN_MAKELDIF_WARNING_UNDEFINED_CONSTANT.get( |
| | | constantName, lineNumber); |
| | | warnings.add(message); |
| | | } |
| | | else |
| | | { |
| | | lineBuffer.replace(openPos, closePos+1, constantValue); |
| | | } |
| | | } |
| | | |
| | | line = lineBuffer.toString(); |
| | | } |
| | | |
| | | line = replaceConstants(line, lineNumber, |
| | | templateFileConstants, warnings); |
| | | lineList.add(line); |
| | | } |
| | | } |
| | |
| | | |
| | | Template t = parseTemplateDefinition(templateLines, startLineNumber, |
| | | templateFileIncludeTags, |
| | | templateFileConstants, |
| | | templateFileTemplates, warnings); |
| | | String lowerName = toLowerCase(t.getName()); |
| | | if (templateFileTemplates.containsKey(lowerName)) |
| | |
| | | } |
| | | |
| | | |
| | | /** |
| | | * Parse a line and replace all constants within [ ] with their |
| | | * values. |
| | | * |
| | | * @param line The line to parse. |
| | | * @param lineNumber The line number in the template file. |
| | | * @param constants The set of constants defined in the template file. |
| | | * @param warnings A list into which any warnings identified may be |
| | | * placed. |
| | | * @return The line in which all constant variables have been replaced |
| | | * with their value |
| | | */ |
| | | private String replaceConstants(String line, int lineNumber, |
| | | Map<String,String> constants, |
| | | List<Message> warnings) |
| | | { |
| | | int closePos = line.lastIndexOf(']'); |
| | | // Loop until we've scanned all closing brackets |
| | | do |
| | | { |
| | | // Skip escaped closing brackets |
| | | while (closePos > 0 && |
| | | line.charAt(closePos - 1) == '\\') |
| | | { |
| | | closePos = line.lastIndexOf(']', closePos - 1); |
| | | } |
| | | if (closePos > 0) |
| | | { |
| | | StringBuilder lineBuffer = new StringBuilder(line); |
| | | int openPos = line.lastIndexOf('[', closePos); |
| | | // Find the opening bracket. If it's escaped, then it's not a constant |
| | | if ((openPos > 0 && line.charAt(openPos - 1) != '\\') || |
| | | (openPos == 0)) |
| | | { |
| | | String constantName = |
| | | toLowerCase(line.substring(openPos+1, closePos)); |
| | | String constantValue = constants.get(constantName); |
| | | if (constantValue == null) |
| | | { |
| | | Message message = WARN_MAKELDIF_WARNING_UNDEFINED_CONSTANT.get( |
| | | constantName, lineNumber); |
| | | warnings.add(message); |
| | | } |
| | | else |
| | | { |
| | | lineBuffer.replace(openPos, closePos+1, constantValue); |
| | | } |
| | | } |
| | | if (openPos >= 0) |
| | | { |
| | | closePos = openPos; |
| | | } |
| | | line = lineBuffer.toString(); |
| | | closePos = line.lastIndexOf(']', closePos); |
| | | } |
| | | } while (closePos > 0); |
| | | return line; |
| | | } |
| | | |
| | | /** |
| | | * Parses the information contained in the provided set of lines as a MakeLDIF |
| | | * branch definition. |
| | | * |
| | | * |
| | | * @param branchLines The set of lines containing the branch definition. |
| | | * @param startLineNumber The line number in the template file on which the |
| | | * first of the branch lines appears. |
| | | * @param tags The set of defined tags from the template file. |
| | | * Note that this does not include the tags that are |
| | | * always registered by default. |
| | | * @param constants The set of constants defined in the template file. |
| | | * @param warnings A list into which any warnings identified may be |
| | | * placed. |
| | | * |
| | |
| | | */ |
| | | private Branch parseBranchDefinition(String[] branchLines, |
| | | int startLineNumber, |
| | | LinkedHashMap<String,Tag> tags, |
| | | LinkedHashMap<String,String> constants, |
| | | Map<String, Tag> tags, |
| | | List<Message> warnings) |
| | | throws InitializationException, MakeLDIFException |
| | | { |
| | |
| | | * Parses the information contained in the provided set of lines as a MakeLDIF |
| | | * template definition. |
| | | * |
| | | * |
| | | * @param templateLines The set of lines containing the template |
| | | * definition. |
| | | * @param startLineNumber The line number in the template file on which the |
| | |
| | | * @param tags The set of defined tags from the template file. |
| | | * Note that this does not include the tags that are |
| | | * always registered by default. |
| | | * @param constants The set of constants defined in the template |
| | | * file. |
| | | * @param definedTemplates The set of templates already defined in the |
| | | * template file. |
| | | * @param warnings A list into which any warnings identified may be |
| | |
| | | */ |
| | | private Template parseTemplateDefinition(String[] templateLines, |
| | | int startLineNumber, |
| | | LinkedHashMap<String,Tag> tags, |
| | | LinkedHashMap<String,String> |
| | | constants, |
| | | LinkedHashMap<String,Template> |
| | | Map<String, Tag> tags, |
| | | Map<String, Template> |
| | | definedTemplates, |
| | | List<Message> warnings) |
| | | throws InitializationException, MakeLDIFException |
| | |
| | | private TemplateLine parseTemplateLine(String line, String lowerLine, |
| | | int lineNumber, Branch branch, |
| | | Template template, |
| | | LinkedHashMap<String,Tag> tags, |
| | | Map<String,Tag> tags, |
| | | List<Message> warnings) |
| | | throws InitializationException, MakeLDIFException |
| | | { |
| | |
| | | final int PARSING_STATIC_TEXT = 0; |
| | | final int PARSING_REPLACEMENT_TAG = 1; |
| | | final int PARSING_ATTRIBUTE_TAG = 2; |
| | | final int PARSING_ESCAPED_CHAR = 3; |
| | | |
| | | int phase = PARSING_STATIC_TEXT; |
| | | |
| | | int previousPhase = PARSING_STATIC_TEXT; |
| | | |
| | | ArrayList<Tag> tagList = new ArrayList<Tag>(); |
| | | StringBuilder buffer = new StringBuilder(); |
| | |
| | | case PARSING_STATIC_TEXT: |
| | | switch (c) |
| | | { |
| | | case '\\': |
| | | phase = PARSING_ESCAPED_CHAR; |
| | | previousPhase = PARSING_STATIC_TEXT; |
| | | break; |
| | | case '<': |
| | | if (buffer.length() > 0) |
| | | { |
| | |
| | | case PARSING_REPLACEMENT_TAG: |
| | | switch (c) |
| | | { |
| | | case '\\': |
| | | phase = PARSING_ESCAPED_CHAR; |
| | | previousPhase = PARSING_REPLACEMENT_TAG; |
| | | break; |
| | | case '>': |
| | | Tag t = parseReplacementTag(buffer.toString(), branch, template, |
| | | lineNumber, tags, warnings); |
| | |
| | | case PARSING_ATTRIBUTE_TAG: |
| | | switch (c) |
| | | { |
| | | case '\\': |
| | | phase = PARSING_ESCAPED_CHAR; |
| | | previousPhase = PARSING_ATTRIBUTE_TAG; |
| | | break; |
| | | case '}': |
| | | Tag t = parseAttributeTag(buffer.toString(), branch, template, |
| | | lineNumber, warnings); |
| | |
| | | break; |
| | | } |
| | | break; |
| | | |
| | | case PARSING_ESCAPED_CHAR: |
| | | buffer.append(c); |
| | | phase = previousPhase; |
| | | break; |
| | | } |
| | | } |
| | | |
| | |
| | | */ |
| | | private Tag parseReplacementTag(String tagString, Branch branch, |
| | | Template template, int lineNumber, |
| | | LinkedHashMap<String,Tag> tags, |
| | | Map<String,Tag> tags, |
| | | List<Message> warnings) |
| | | throws InitializationException, MakeLDIFException |
| | | { |