This source file includes following definitions.
- Parse
- ClassifyField
- Parse
- ClassifyField
- ParseSpecificName
- ParseComponentNames
- Parse
- middle_initial_
- ClassifyField
#include "components/autofill/core/browser/name_field.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "components/autofill/core/browser/autofill_regex_constants.h"
#include "components/autofill/core/browser/autofill_scanner.h"
#include "components/autofill/core/browser/autofill_type.h"
#include "ui/base/l10n/l10n_util.h"
using base::UTF8ToUTF16;
namespace autofill {
namespace {
class FullNameField : public NameField {
public:
static FullNameField* Parse(AutofillScanner* scanner);
protected:
virtual bool ClassifyField(ServerFieldTypeMap* map) const OVERRIDE;
private:
explicit FullNameField(const AutofillField* field);
const AutofillField* field_;
DISALLOW_COPY_AND_ASSIGN(FullNameField);
};
class FirstLastNameField : public NameField {
public:
static FirstLastNameField* ParseSpecificName(AutofillScanner* scanner);
static FirstLastNameField* ParseComponentNames(AutofillScanner* scanner);
static FirstLastNameField* Parse(AutofillScanner* scanner);
protected:
virtual bool ClassifyField(ServerFieldTypeMap* map) const OVERRIDE;
private:
FirstLastNameField();
const AutofillField* first_name_;
const AutofillField* middle_name_;
const AutofillField* last_name_;
bool middle_initial_;
DISALLOW_COPY_AND_ASSIGN(FirstLastNameField);
};
}
FormField* NameField::Parse(AutofillScanner* scanner) {
if (scanner->IsEnd())
return NULL;
NameField* field = FirstLastNameField::Parse(scanner);
if (!field)
field = FullNameField::Parse(scanner);
return field;
}
bool NameField::ClassifyField(ServerFieldTypeMap* map) const {
return false;
}
FullNameField* FullNameField::Parse(AutofillScanner* scanner) {
scanner->SaveCursor();
bool should_ignore = ParseField(scanner,
UTF8ToUTF16(autofill::kNameIgnoredRe), NULL);
scanner->Rewind();
if (should_ignore)
return NULL;
const AutofillField* field = NULL;
if (ParseField(scanner, UTF8ToUTF16(autofill::kNameRe), &field))
return new FullNameField(field);
return NULL;
}
bool FullNameField::ClassifyField(ServerFieldTypeMap* map) const {
return AddClassification(field_, NAME_FULL, map);
}
FullNameField::FullNameField(const AutofillField* field)
: field_(field) {
}
FirstLastNameField* FirstLastNameField::ParseSpecificName(
AutofillScanner* scanner) {
scoped_ptr<FirstLastNameField> v(new FirstLastNameField);
scanner->SaveCursor();
const AutofillField* next = NULL;
if (ParseField(scanner,
UTF8ToUTF16(autofill::kNameSpecificRe), &v->first_name_) &&
ParseEmptyLabel(scanner, &next)) {
if (ParseEmptyLabel(scanner, &v->last_name_)) {
v->middle_name_ = next;
v->middle_initial_ = true;
} else {
v->last_name_ = next;
}
return v.release();
}
scanner->Rewind();
return NULL;
}
FirstLastNameField* FirstLastNameField::ParseComponentNames(
AutofillScanner* scanner) {
scoped_ptr<FirstLastNameField> v(new FirstLastNameField);
scanner->SaveCursor();
while (!scanner->IsEnd()) {
if (ParseFieldSpecifics(scanner, UTF8ToUTF16(autofill::kNameIgnoredRe),
MATCH_DEFAULT | MATCH_SELECT, NULL)) {
continue;
}
if (!v->first_name_ &&
ParseField(scanner, UTF8ToUTF16(autofill::kFirstNameRe),
&v->first_name_)) {
continue;
}
if (!v->middle_name_ &&
ParseField(scanner, UTF8ToUTF16(autofill::kMiddleInitialRe),
&v->middle_name_)) {
v->middle_initial_ = true;
continue;
}
if (!v->middle_name_ &&
ParseField(scanner, UTF8ToUTF16(autofill::kMiddleNameRe),
&v->middle_name_)) {
continue;
}
if (!v->last_name_ &&
ParseField(scanner, UTF8ToUTF16(autofill::kLastNameRe),
&v->last_name_)) {
continue;
}
break;
}
if (v->first_name_ && v->last_name_)
return v.release();
scanner->Rewind();
return NULL;
}
FirstLastNameField* FirstLastNameField::Parse(AutofillScanner* scanner) {
FirstLastNameField* field = ParseSpecificName(scanner);
if (!field)
field = ParseComponentNames(scanner);
return field;
}
FirstLastNameField::FirstLastNameField()
: first_name_(NULL),
middle_name_(NULL),
last_name_(NULL),
middle_initial_(false) {
}
bool FirstLastNameField::ClassifyField(ServerFieldTypeMap* map) const {
bool ok = AddClassification(first_name_, NAME_FIRST, map);
ok = ok && AddClassification(last_name_, NAME_LAST, map);
ServerFieldType type = middle_initial_ ? NAME_MIDDLE_INITIAL : NAME_MIDDLE;
ok = ok && AddClassification(middle_name_, type, map);
return ok;
}
}